Sha256: f766574b3147b9ca957473db59e297414aebeb82e7b5d8af3a04cf4656d3f365

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'net/http'

module AccessTokenAgent
  class Connector
    FAKE_TOKEN = 'FakeAuthToken'.freeze

    class << self
      attr_accessor :instance
    end

    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

    def http_auth_header
      { 'Authorization' => "Bearer #{token}" }
    end

    def token
      return FAKE_TOKEN if @fake_auth
      @known_token = fetch_token unless @known_token && @known_token.valid?

      @known_token.value
    end

    def authenticate
      warn "[DEPRECATION] `#{self.class}.authenticate` is deprecated. " \
           'Use `token` instead.'
      token
    end

    private

    def fetch_token
      Token.new(fetch_token_hash)
    end

    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

    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

    def auth_uri
      @auth_uri ||= URI("#{@host}#{@access_token_path}")
    end

    def form_data
      result = { 'grant_type' => 'client_credentials' }
      result['scope'] = @scopes.join(' ') if @scopes
      result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
access_token_agent-3.5.0 lib/access_token_agent/connector.rb