class Bigid::Auth::AuthenticatedConnection

Constants

Public Class Methods

new(authentication: Bigid::Auth::Authentication.new, cache: defined?(Rails) ? Rails.cache : nil, request_class: Faraday, base_url: Bigid::BASE_URL, token_expiration_time_in_seconds: Bigid::Auth::TOKEN_EXPIRATION) click to toggle source
Calls superclass method
# File lib/bigid/auth/authenticated_connection.rb, line 8
def initialize(authentication: Bigid::Auth::Authentication.new,
                cache: defined?(Rails) ? Rails.cache : nil,
                request_class: Faraday,
                base_url: Bigid::BASE_URL,
                token_expiration_time_in_seconds: Bigid::Auth::TOKEN_EXPIRATION)
  super(request_class: request_class, base_url: base_url)
  @authentication = authentication
  @cache = cache
  @token_expiration_time_in_seconds = token_expiration_time_in_seconds
end

Public Instance Methods

default_headers() click to toggle source
Calls superclass method
# File lib/bigid/auth/authenticated_connection.rb, line 19
def default_headers
  super.merge(Authorization: (@cache ? cached_token : auth_token))
end

Private Instance Methods

auth_token() click to toggle source
# File lib/bigid/auth/authenticated_connection.rb, line 24
def auth_token
  response = @authentication.login
  extract_token(response.body)
end
cached_token() click to toggle source
# File lib/bigid/auth/authenticated_connection.rb, line 29
def cached_token
  @cache.fetch(COOKIE_CACHE_KEY, expires_in: @token_expiration_time_in_seconds.seconds) do
    auth_token
  end
end
extract_token(value) click to toggle source
# File lib/bigid/auth/authenticated_connection.rb, line 35
def extract_token(value)
  raise Bigid::Auth::AuthenticationError unless value

  value_json = JSON.parse(value)
  "Bearer #{value_json['token']}"
end