Class: Worldline::Acquiring::SDK::Authentication::OAuth2Authenticator

Inherits:
Authenticator
  • Object
show all
Defined in:
lib/worldline/acquiring/sdk/authentication/oauth2_authenticator.rb

Overview

Authenticates requests made to the Worldline Acquiring platform using OAuth2.

Defined Under Namespace

Classes: AccessToken, TokenType

Instance Method Summary collapse

Constructor Details

#initialize(communicator_configuration) ⇒ OAuth2Authenticator

Constructs a new OAuth2Authenticator instance using the provided CommunicatorConfiguration.

Parameters:

  • communicator_configuration (Worldline::Acquiring::SDK::CommunicatorConfiguration)

    The configuration object containing the OAuth2 client id, client secret and token URI, connection timeout, and socket timeout. None of these can be nil or empty, and the timeout values must be positive.

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/worldline/acquiring/sdk/authentication/oauth2_authenticator.rb', line 56

def initialize(communicator_configuration)
  raise ArgumentError unless communicator_configuration.oauth2_client_id and not communicator_configuration.oauth2_client_id.strip.empty?
  raise ArgumentError unless communicator_configuration.oauth2_client_secret and not communicator_configuration.oauth2_client_secret.strip.empty?
  raise ArgumentError unless communicator_configuration.oauth2_token_uri and not communicator_configuration.oauth2_token_uri.strip.empty?
  raise ArgumentError unless communicator_configuration.connect_timeout > 0
  raise ArgumentError unless communicator_configuration.socket_timeout > 0

  @access_tokens = Concurrent::Map.new
  @client_id = communicator_configuration.oauth2_client_id
  @client_secret = communicator_configuration.oauth2_client_secret
  @token_uri = communicator_configuration.oauth2_token_uri
  @connect_timeout = communicator_configuration.connect_timeout
  @socket_timeout = communicator_configuration.socket_timeout
  @proxy_configuration = communicator_configuration.proxy_configuration
end

Instance Method Details

#get_authorization(http_method, resource_uri, request_headers) ⇒ Object

Parameters:

  • http_method (String, nil)

    ‘GET’, ‘PUT’, ‘POST’ or ‘DELETE’ indicating which HTTP method will be used with the request

  • resource_uri (URI::HTTP, nil)

    URI object that includes #path and #query of the URL that will be used, #query may be nil

  • request_headers (Array<Worldline::Acquiring::SDK::Communication::RequestHeader>, nil)

    all headers used by the request



75
76
77
78
79
# File 'lib/worldline/acquiring/sdk/authentication/oauth2_authenticator.rb', line 75

def get_authorization(http_method, resource_uri, request_headers)
  token_type = get_token_type(resource_uri&.path)
  access_token = @access_tokens.compute(token_type.path) { |existing_token| get_valid_access_token(existing_token, token_type) }
  "Bearer #{access_token.token}"
end