Files
old-vendor-scheduler-service/app/services/auth/auth_token_service.rb
2022-03-23 05:44:42 +01:00

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