Initial commit
This commit is contained in:
48
app/services/auth/auth_token_service.rb
Normal file
48
app/services/auth/auth_token_service.rb
Normal file
@@ -0,0 +1,48 @@
|
||||
# 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
|
||||
Reference in New Issue
Block a user