Class: CandidApiClient::OauthTokenProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/core/oauth.rb

Overview

Utility class for managing token refresh.

Constant Summary collapse

EXPIRY_BUFFER_MINUTES =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, request_client:) ⇒ CandidApiClient::OauthTokenProvider

Parameters:



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_clientString (readonly)

Returns:

  • (String)


30
31
32
# File 'lib/core/oauth.rb', line 30

def auth_client
  @auth_client
end

#client_idString (readonly)

Returns:

  • (String)


26
27
28
# File 'lib/core/oauth.rb', line 26

def client_id
  @client_id
end

#client_secretString (readonly)

Returns:

  • (String)


28
29
30
# File 'lib/core/oauth.rb', line 28

def client_secret
  @client_secret
end

Instance Method Details

#refresh_tokenCandidApiClient::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

#tokenString

Returns a cached access token retrieved from the provided client credentials,

refreshing if necessary.

Returns:

  • (String)


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