Sha256: 7cbc8f32109683f1ecb6adde9cbf78707235b560d76b037f441f86f210fa78ba

Contents?: true

Size: 1.58 KB

Versions: 12

Compression:

Stored size: 1.58 KB

Contents

module ResoTransport
  module Authentication
    class FetchTokenAuth < AuthStrategy
      attr_reader :connection, :endpoint, :client_id, :client_secret, :grant_type, :scope
      
      def initialize(options)
        @grant_type    = options.fetch(:grant_type, "client_credentials")
        @scope         = options.fetch(:scope, "api")
        @client_id     = options.fetch(:client_id)
        @client_secret = options.fetch(:client_secret)
        @endpoint      = options.fetch(:endpoint)

        @connection = Faraday.new(@endpoint) do |faraday|
          faraday.request  :url_encoded
          faraday.response :logger, ResoTransport.configuration.logger if ResoTransport.configuration.logger
          faraday.adapter Faraday.default_adapter
          faraday.basic_auth @client_id, @client_secret
        end
      end

      def authenticate
        response = connection.post nil, auth_params
        json = JSON.parse response.body

        unless response.success?
          message = "#{response.reason_phrase}: #{json['error'] || response.body}"
          raise ResoTransport::AccessDenied, response: response, message: message
        end

        Access.new({
          access_token: json.fetch('access_token'),
          expires_in: json.fetch('expires_in', 1 << (1.size * 8 - 2) - 1),
          token_type: json.fetch('token_type', "Bearer")
        })
      end

      private

      def auth_params
        {
          client_id:     client_id,
          client_secret: client_secret,
          grant_type:    grant_type,
          scope:         scope
        }
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
reso_transport-1.5.5 lib/reso_transport/authentication/fetch_token_auth.rb
reso_transport-1.5.4 lib/reso_transport/authentication/fetch_token_auth.rb
reso_transport-1.5.3 lib/reso_transport/authentication/fetch_token_auth.rb
reso_transport-1.5.2 lib/reso_transport/authentication/fetch_token_auth.rb
reso_transport-1.5.1 lib/reso_transport/authentication/fetch_token_auth.rb
reso_transport-1.4.0 lib/reso_transport/authentication/fetch_token_auth.rb
reso_transport-1.3.2 lib/reso_transport/authentication/fetch_token_auth.rb
reso_transport-1.3.1 lib/reso_transport/authentication/fetch_token_auth.rb
reso_transport-1.3.0 lib/reso_transport/authentication/fetch_token_auth.rb
reso_transport-1.2.0 lib/reso_transport/authentication/fetch_token_auth.rb
reso_transport-1.1.0 lib/reso_transport/authentication/fetch_token_auth.rb
reso_transport-1.0.0 lib/reso_transport/authentication/fetch_token_auth.rb