module MangoApi::AuthTokenManager

Manages acquisition and storage of API authorization token.

Constants

LOG

Public Class Methods

token() click to toggle source

Provides a valid authorization token for API calls.

# File lib/mangopay/api/auth_token_manager.rb, line 13
def token
  client_id = MangoPay.configuration.client_id
  token_valid? && storage.retrieve_for(client_id) || refresh_token
end

Private Class Methods

append_data(token) click to toggle source

Appends some useful data to the token object.

@param token the token object

# File lib/mangopay/api/auth_token_manager.rb, line 79
def append_data(token)
  token['expire_time'] = Time.now + token['expires_in'].to_i
  token['environment_key'] = environment_key
end
environment_key() click to toggle source

Provides a configuration-specific hash key.

# File lib/mangopay/api/auth_token_manager.rb, line 56
def environment_key
  config = MangoPay.configuration
  key = [config.root_url, config.client_id, config.client_apiKey]
        .join('|')
  Digest::MD5.hexdigest(key)
end
init_storage() click to toggle source

Initializes storage strategy for OAuth tokens. Returns the storage object.

# File lib/mangopay/api/auth_token_manager.rb, line 28
def init_storage
  client_id = MangoPay.configuration.client_id
  dir = MangoPay.configuration.temp_dir
  @storage = dir ? FileStorage.new : MemoryStorage.new
  strategy = @storage.class.name.split('::').last
  location_if_file = dir ? " at '/#{dir}'" : ''
  LOG.info 'Using {}{} for {} client\'s OAuth tokens',
           strategy, location_if_file, client_id
  @storage
end
refresh_token() click to toggle source

Refreshes the stored authorization token.

# File lib/mangopay/api/auth_token_manager.rb, line 64
def refresh_token
  LOG.info 'Refreshing OAuth token'
  config = MangoPay.configuration
  unless config.client_id && config.client_apiKey
    raise 'You must specify your client ID and apiKey'
  end
  token = MangoApi::OAuthTokens.create(config)
  append_data token
  store_token token
  token
end
storage() click to toggle source

Retrieves authorization token storage object.

# File lib/mangopay/api/auth_token_manager.rb, line 21
def storage
  return @storage if @storage
  init_storage
end
store_token(token) click to toggle source

Stores the OAuth token.

@param token the OAuth token to be stored

# File lib/mangopay/api/auth_token_manager.rb, line 87
def store_token(token)
  client_id = MangoPay.configuration.client_id
  storage.store_for client_id, token
  LOG.info 'OAuth token stored in {} for client {}',
           storage.class.name.split('::').last, client_id
end
token_invalid?() click to toggle source

Returns true if the current client's stored token is no longer valid.

# File lib/mangopay/api/auth_token_manager.rb, line 47
def token_invalid?
  token = storage.retrieve_for MangoPay.configuration.client_id
  token.nil? ||
    token['expire_time'].nil? ||
    token['expire_time'] <= Time.now ||
    token['environment_key'] != environment_key
end
token_valid?() click to toggle source

Returns true if the current client's authorization token is valid

# File lib/mangopay/api/auth_token_manager.rb, line 41
def token_valid?
  !token_invalid?
end