Class: Yes::Core::ProcessManagers::AccessTokenClient

Inherits:
Object
  • Object
show all
Defined in:
lib/yes/core/process_managers/access_token_client.rb

Overview

Client for obtaining access tokens using client credentials.

Examples:

client = Yes::Core::ProcessManagers::AccessTokenClient.new
access_token = client.call(client_id: 'id', client_secret: 'secret')

Defined Under Namespace

Classes: Error, Response

Constant Summary collapse

AUTH_URL =

Returns the authentication service URL.

Returns:

  • (String)

    the authentication service URL

ENV.fetch('AUTH_URL', 'http://auth-cluster-ip-service:3000/v1')
GRANT_TYPE =

Returns the OAuth2 grant type.

Returns:

  • (String)

    the OAuth2 grant type

'client_credentials'

Instance Method Summary collapse

Constructor Details

#initializeAccessTokenClient

Initializes the AccessTokenClient with a Faraday connection.



29
30
31
32
33
# File 'lib/yes/core/process_managers/access_token_client.rb', line 29

def initialize
  @connection = Faraday.new(AUTH_URL) do |f|
    f.request :json
  end
end

Instance Method Details

#call(client_id:, client_secret:) ⇒ String

Requests an access token using client credentials.

Parameters:

  • client_id (String)

    the client ID

  • client_secret (String)

    the client secret

Returns:

  • (String)

    the access token

Raises:

  • (Error)

    if the access token cannot be obtained



41
42
43
44
45
46
47
48
49
50
# File 'lib/yes/core/process_managers/access_token_client.rb', line 41

def call(client_id:, client_secret:)
  payload = { client_id:, client_secret:, grant_type: GRANT_TYPE }

  response = perform_request(payload)
  access_token_error!(response) unless response.response.success?

  return response.parsed_body['access_token'] if response.parsed_body&.dig('access_token')

  access_token_error!(response)
end