Class: Google::Auth::OAuth2::STSClient
- Inherits:
-
Object
- Object
- Google::Auth::OAuth2::STSClient
- Includes:
- Helpers::Connection
- Defined in:
- lib/googleauth/oauth2/sts_client.rb
Overview
OAuth 2.0 Token Exchange Spec. This module defines a token exchange utility based on the OAuth 2.0 Token Exchange spec. This will be mainly used to exchange external credentials for GCP access tokens in workload identity pools to access Google APIs. The implementation will support various types of client authentication as allowed in the spec.
A deviation on the spec will be for additional Google specific options that cannot be easily mapped to parameters defined in the RFC. The returned dictionary response will be based on the rfc8693 section 2.2.1 spec JSON response.
Constant Summary collapse
- URLENCODED_HEADERS =
{ "Content-Type": "application/x-www-form-urlencoded" }.freeze
Instance Method Summary collapse
-
#exchange_token(options = {}) ⇒ Hash
Exchanges the provided token for another type of token based on the rfc8693 spec.
-
#initialize(options = {}) ⇒ STSClient
constructor
Create a new instance of the STSClient.
Methods included from Helpers::Connection
Constructor Details
#initialize(options = {}) ⇒ STSClient
Create a new instance of the STSClient.
41 42 43 44 45 |
# File 'lib/googleauth/oauth2/sts_client.rb', line 41 def initialize = {} raise "Token exchange endpoint can not be nil" if [:token_exchange_endpoint].nil? self.default_connection = [:connection] @token_exchange_endpoint = [:token_exchange_endpoint] end |
Instance Method Details
#exchange_token(options = {}) ⇒ Hash
Exchanges the provided token for another type of token based on the rfc8693 spec
A callable faraday instance used to make HTTP requests.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/googleauth/oauth2/sts_client.rb', line 70 def exchange_token = {} missing_required_opts = [:grant_type, :subject_token, :subject_token_type] - .keys unless missing_required_opts.empty? raise ArgumentError, "Missing required options: #{missing_required_opts.join ', '}" end # TODO: Add the ability to add authentication to the headers headers = URLENCODED_HEADERS.dup.merge([:additional_headers] || {}) request_body = make_request response = connection.post @token_exchange_endpoint, URI.encode_www_form(request_body), headers if response.status != 200 raise "Token exchange failed with status #{response.status}" end MultiJson.load response.body end |