Class: Zitadel::Client::Auth::WebTokenAuthenticator
- Inherits:
-
OAuthAuthenticator
- Object
- Authenticator
- OAuthAuthenticator
- Zitadel::Client::Auth::WebTokenAuthenticator
- Defined in:
- lib/zitadel/client/auth/web_token_authenticator.rb
Overview
OAuth authenticator implementing the JWT bearer flow.
This implementation builds a JWT assertion dynamically in get_grant().
Defined Under Namespace
Classes: WebTokenAuthenticatorBuilder
Class Method Summary collapse
-
.builder(host, user_id, private_key) ⇒ WebTokenAuthenticatorBuilder
Returns a builder for constructing a WebTokenAuthenticator.
-
.from_json(host, json_path) ⇒ WebTokenAuthenticator
Creates a WebTokenAuthenticator instance from a JSON configuration file.
Instance Method Summary collapse
-
#initialize(open_id, auth_scopes, jwt_issuer, jwt_subject, jwt_audience, private_key, jwt_lifetime: 3600, jwt_algorithm: 'RS256', key_id: nil) ⇒ WebTokenAuthenticator
constructor
Constructs a WebTokenAuthenticator.
Constructor Details
#initialize(open_id, auth_scopes, jwt_issuer, jwt_subject, jwt_audience, private_key, jwt_lifetime: 3600, jwt_algorithm: 'RS256', key_id: nil) ⇒ WebTokenAuthenticator
Constructs a WebTokenAuthenticator.
rubocop:disable Metrics/ParameterLists,Metrics/MethodLength
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/zitadel/client/auth/web_token_authenticator.rb', line 29 def initialize(open_id, auth_scopes, jwt_issuer, jwt_subject, jwt_audience, private_key, jwt_lifetime: 3600, jwt_algorithm: 'RS256', key_id: nil) # noinspection RubyArgCount,RubyMismatchedArgumentType super(open_id, auth_scopes, OAuth2::Client.new('zitadel', 'zitadel', { site: open_id.host_endpoint, token_url: open_id.token_endpoint })) @jwt_issuer = jwt_issuer @jwt_subject = jwt_subject @jwt_audience = jwt_audience @jwt_lifetime = jwt_lifetime @jwt_algorithm = jwt_algorithm @key_id = key_id # noinspection RubyMismatchedVariableType @private_key = if private_key.is_a?(String) OpenSSL::PKey::RSA.new(private_key) else private_key end end |
Class Method Details
.builder(host, user_id, private_key) ⇒ WebTokenAuthenticatorBuilder
Returns a builder for constructing a WebTokenAuthenticator.
88 89 90 |
# File 'lib/zitadel/client/auth/web_token_authenticator.rb', line 88 def self.builder(host, user_id, private_key) WebTokenAuthenticatorBuilder.new(host, user_id, user_id, host, private_key) end |
.from_json(host, json_path) ⇒ WebTokenAuthenticator
Creates a WebTokenAuthenticator instance from a JSON configuration file.
The JSON file must be formatted as follows:
{
"type": "serviceaccount",
"keyId": "<key-id>",
"key": "<private-key>",
"userId": "<user-id>"
}
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/zitadel/client/auth/web_token_authenticator.rb', line 67 def self.from_json(host, json_path) config = JSON.parse(File.read(json_path)) rescue Errno::ENOENT => e raise "Unable to read JSON file at #{json_path}: #{e.}" rescue JSON::ParserError => e raise "Invalid JSON in file at #{json_path}: #{e.}" else raise "Expected a JSON object, got #{config.class}" unless config.is_a?(Hash) user_id, private_key, key_id = config.values_at('userId', 'key', 'keyId') raise "Missing required keys 'userId', 'keyId' or 'key'" unless user_id && key_id && private_key WebTokenAuthenticator.builder(host, user_id, private_key).key_identifier(key_id).build end |