Class: Zitadel::Client::Auth::OAuthAuthenticator
- Inherits:
-
Authenticator
- Object
- Authenticator
- Zitadel::Client::Auth::OAuthAuthenticator
- Defined in:
- lib/zitadel/client/auth/o_auth_authenticator.rb,
sig/lib.rbs
Overview
Base class for OAuth-based authentication using an OAuth2 client.
Attributes:
open_id: An object providing OAuth endpoint information.
auth_session: An OAuth2Session instance used for fetching tokens.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Authenticator
Instance Method Summary collapse
-
#auth_headers ⇒ Hash{String => String}
Retrieves authentication headers.
-
#auth_token ⇒ String
Returns the current access token, refreshing it if necessary.
-
#get_grant(auth_client, auth_scopes) ⇒ OAuth2::AccessToken
Builds and returns a hash of grant parameters required for the token request.
-
#initialize(open_id, auth_scopes, auth_session, transport_options: nil) ⇒ OAuthAuthenticator
constructor
Constructs an OAuthAuthenticator.
-
#refresh_token ⇒ OAuth2::AccessToken
Refreshes the access token using the OAuth flow.
Constructor Details
#initialize(open_id, auth_scopes, auth_session, transport_options: nil) ⇒ OAuthAuthenticator
Constructs an OAuthAuthenticator.
32 33 34 35 36 37 38 39 40 |
# File 'lib/zitadel/client/auth/o_auth_authenticator.rb', line 32 def initialize(open_id, auth_scopes, auth_session, transport_options: nil) super(open_id.host_endpoint) @open_id = open_id @transport_options = || TransportOptions.defaults @token = nil @auth_session = auth_session @auth_scopes = auth_scopes.to_a.join(' ') @mutex = Thread::Mutex.new end |
Instance Method Details
#auth_headers ⇒ Hash{String => String}
Retrieves authentication headers.
62 63 64 |
# File 'lib/zitadel/client/auth/o_auth_authenticator.rb', line 62 def auth_headers { 'Authorization' => "Bearer #{auth_token}" } end |
#auth_token ⇒ String
Returns the current access token, refreshing it if necessary.
47 48 49 50 51 52 53 54 55 |
# File 'lib/zitadel/client/auth/o_auth_authenticator.rb', line 47 def auth_token @mutex.synchronize do refresh_token if @token.nil? || @token.expired? raise 'Token is nil after refresh' if @token.nil? return @token.token end end |
#get_grant(auth_client, auth_scopes) ⇒ OAuth2::AccessToken
Builds and returns a hash of grant parameters required for the token request.
The base class will invoke this method by passing its OAuth2 client. The subclass implementation should return the result of either:
client.client_credentials.get_token(scope: scopes)
or client.assertion.get_token(claims)
79 80 81 82 83 |
# File 'lib/zitadel/client/auth/o_auth_authenticator.rb', line 79 def get_grant(auth_client, auth_scopes) # :nocov: raise NotImplementedError, "#{self.class}#get_grant must be implemented" # :nocov: end |
#refresh_token ⇒ OAuth2::AccessToken
Refreshes the access token using the OAuth flow.
It uses get_grant to obtain all necessary parameters for the token request.
93 94 95 96 97 |
# File 'lib/zitadel/client/auth/o_auth_authenticator.rb', line 93 def refresh_token @token = get_grant(@auth_session, @auth_scopes) rescue StandardError => e raise ZitadelError.new("Failed to refresh token: #{e.}"), cause: e end |