Class: CandidApiClient::OauthTokenProvider
- Inherits:
-
Object
- Object
- CandidApiClient::OauthTokenProvider
- Defined in:
- lib/core/oauth.rb
Overview
Utility class for managing token refresh.
Constant Summary collapse
- EXPIRY_BUFFER_MINUTES =
2
Instance Attribute Summary collapse
- #auth_client ⇒ String readonly
- #client_id ⇒ String readonly
- #client_secret ⇒ String readonly
Instance Method Summary collapse
- #initialize(client_id:, client_secret:, request_client:) ⇒ CandidApiClient::OauthTokenProvider constructor
- #refresh_token ⇒ CandidApiClient::AccessToken
-
#token ⇒ String
Returns a cached access token retrieved from the provided client credentials, refreshing if necessary.
Constructor Details
#initialize(client_id:, client_secret:, request_client:) ⇒ CandidApiClient::OauthTokenProvider
38 39 40 41 42 |
# File 'lib/core/oauth.rb', line 38 def initialize(client_id:, client_secret:, request_client:) @client_id = client_id @client_secret = client_secret @auth_client = CandidApiClient::Auth::V2::V2Client.new(request_client: request_client) end |
Instance Attribute Details
#auth_client ⇒ String (readonly)
30 31 32 |
# File 'lib/core/oauth.rb', line 30 def auth_client @auth_client end |
#client_id ⇒ String (readonly)
26 27 28 |
# File 'lib/core/oauth.rb', line 26 def client_id @client_id end |
#client_secret ⇒ String (readonly)
28 29 30 |
# File 'lib/core/oauth.rb', line 28 def client_secret @client_secret end |
Instance Method Details
#refresh_token ⇒ CandidApiClient::AccessToken
56 57 58 59 60 |
# File 'lib/core/oauth.rb', line 56 def refresh_token token_response = @auth_client.get_token(client_id: @client_id, client_secret: @client_secret) CandidApiClient::AccessToken.new(access_token: token_response.access_token, expires_at: Time.now + token_response.expires_in - (EXPIRY_BUFFER_MINUTES * 60)) end |
#token ⇒ String
Returns a cached access token retrieved from the provided client credentials,
refreshing if necessary.
48 49 50 51 52 53 |
# File 'lib/core/oauth.rb', line 48 def token return @token.access_token if !@token.nil? && (@token.expires_at.nil? || @token.expires_at > Time.now) @token = refresh_token @token.access_token end |