class AccessTokenAgent::Connector
Constants
- FAKE_TOKEN
Attributes
instance[RW]
Public Class Methods
new(host:, client_id:, client_secret:, fake_auth: false, access_token_path: '/oauth/token', scopes: nil)
click to toggle source
# File lib/access_token_agent/connector.rb, line 11 def initialize(host:, client_id:, client_secret:, fake_auth: false, access_token_path: '/oauth/token', scopes: nil) @host = host @client_id = client_id @client_secret = client_secret @fake_auth = fake_auth @access_token_path = access_token_path @scopes = scopes end
Public Instance Methods
authenticate()
click to toggle source
# File lib/access_token_agent/connector.rb, line 36 def authenticate warn "[DEPRECATION] `#{self.class}.authenticate` is deprecated. " \ 'Use `token` instead.' token end
http_auth_header()
click to toggle source
# File lib/access_token_agent/connector.rb, line 25 def http_auth_header { 'Authorization' => "Bearer #{token}" } end
token()
click to toggle source
# File lib/access_token_agent/connector.rb, line 29 def token return FAKE_TOKEN if @fake_auth @known_token = fetch_token unless @known_token && @known_token.valid? @known_token.value end
Private Instance Methods
auth_uri()
click to toggle source
# File lib/access_token_agent/connector.rb, line 72 def auth_uri @auth_uri ||= URI("#{@host}#{@access_token_path}") end
fetch_token()
click to toggle source
# File lib/access_token_agent/connector.rb, line 44 def fetch_token Token.new(fetch_token_hash) end
fetch_token_hash()
click to toggle source
# File lib/access_token_agent/connector.rb, line 48 def fetch_token_hash response = perform_request case response.code when '200' then JSON.parse(response.body) when '401' then raise UnauthorizedError else raise Error, "status: #{response.code}, body: #{response.body}" end rescue Errno::ECONNREFUSED raise ConnectionError end
form_data()
click to toggle source
# File lib/access_token_agent/connector.rb, line 76 def form_data result = { 'grant_type' => 'client_credentials' } result['scope'] = @scopes.join(' ') if @scopes result end
perform_request()
click to toggle source
# File lib/access_token_agent/connector.rb, line 60 def perform_request request = Net::HTTP::Post.new(auth_uri) request.basic_auth @client_id, @client_secret request.form_data = form_data use_tls = auth_uri.scheme == 'https' Net::HTTP.start(auth_uri.hostname, auth_uri.port, use_ssl: use_tls) do |http| http.request(request) end end