49 lines
832 B
Ruby
49 lines
832 B
Ruby
# frozen_string_literal: true
|
|
|
|
require 'auth/token_service'
|
|
|
|
# Manages tokens for this App
|
|
module Auth
|
|
class AuthTokenService
|
|
include Singleton
|
|
|
|
def refresh
|
|
@tokens = mutex.synchronize { obtain_tokens }
|
|
end
|
|
|
|
def access_token
|
|
ensure_tokens
|
|
@tokens[:access_token]
|
|
end
|
|
|
|
private
|
|
def ensure_tokens
|
|
@tokens ||= mutex.synchronize { obtain_tokens }
|
|
end
|
|
|
|
def obtain_tokens
|
|
token_source.fetch('client_credentials', client_id, client_secret)
|
|
end
|
|
|
|
def mutex
|
|
@mutex ||= Mutex.new
|
|
end
|
|
|
|
def token_source
|
|
@token_source ||= Auth::TokenService.new(auth_url)
|
|
end
|
|
|
|
def client_id
|
|
AppConfig.auth.client_id
|
|
end
|
|
|
|
def client_secret
|
|
AppConfig.auth.client_secret
|
|
end
|
|
|
|
def auth_url
|
|
URI(AppConfig.auth.url)
|
|
end
|
|
end
|
|
end
|