-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ba0cca
commit 7100801
Showing
7 changed files
with
276 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# SharePoint Access | ||
|
||
SP_CLIENT_ID= foo | ||
SP_CLIENT_SECRET= foo | ||
SP_TENANT_ID= foo | ||
SP_CERT_NAME= foo | ||
SP_AUTH_SCOPE= https://foobar.ifad.org | ||
SP_URL= https://foobar.ifad.org | ||
SP_AUTHENTICATION=token | ||
SP_CLIENT_ID=foo | ||
SP_CLIENT_SECRET=foo | ||
SP_TENANT_ID=foo | ||
SP_CERT_NAME=foo | ||
SP_AUTH_SCOPE=https://foobar.ifad.org | ||
SP_URL=https://foobar.ifad.org |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module Sharepoint | ||
class Client | ||
class Token | ||
class InvalidTokenError < StandardError | ||
end | ||
|
||
attr_accessor :expires_in | ||
attr_accessor :access_token | ||
attr_accessor :fetched_at | ||
attr_reader :config | ||
|
||
def initialize(config) | ||
@config = config | ||
end | ||
|
||
def get_or_fetch | ||
return access_token unless access_token.nil? || expired? | ||
fetch | ||
end | ||
|
||
def to_s | ||
access_token | ||
end | ||
|
||
def fetch | ||
response = request_new_token | ||
|
||
details = response["Token"] | ||
self.fetched_at = Time.now.utc.to_i | ||
self.expires_in = details["expires_in"] | ||
self.access_token = details["access_token"] | ||
end | ||
|
||
private | ||
|
||
def expired? | ||
return true unless fetched_at && expires_in | ||
|
||
(fetched_at + expires_in) < Time.now.utc.to_i | ||
end | ||
|
||
def | ||
def request_new_token | ||
auth_request = { | ||
client_id: config.client_id, | ||
client_secret: config.client_secret, | ||
tenant_id: config.tenant_id, | ||
cert_name: config.cert_name, | ||
auth_scope: config.auth_scope | ||
}.to_json | ||
|
||
headers = {'Content-Type' => 'application/json'} | ||
|
||
ethon = Ethon::Easy.new(followlocation: true) | ||
ethon.http_request(config.token_url, :post, body: auth_request, headers: headers) | ||
ethon.perform | ||
|
||
raise InvalidTokenError.new(ethon.response_body.to_s) unless ethon.response_code == 200 | ||
|
||
JSON.parse(ethon.response_body) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.