keycardai-oauth 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ccdb651e530f4a1ad780531eef59ed0d02f30a96bebb4104f9010ea4f784b24
4
- data.tar.gz: 8942a274d0a490f8faadb19348b547b74f09f03ba90252d2bc3ee77e12715dd1
3
+ metadata.gz: b621681ef4ce18e982ba67929dd03d87d77d4d192b5f033f41c757216fe5a5f2
4
+ data.tar.gz: b72fee9e5428766e744fba55c4e3d6472d78295d4cf821813bce7c2c6cd8582f
5
5
  SHA512:
6
- metadata.gz: 8d6292413c8a8efbf361a3bbed96ebdfb22f53a60cd27b367a16b234cfbd7872a2f2401091ba4acfe97633b0658939c600158a829dc4c3bf880323706f374415
7
- data.tar.gz: 4e788c4f977841dea9409c7001e79d64cc2fa340a48ffa7e4c01ca24c076423a09c1005038859de39107d269234df78017bffc89ee1aca8b0c41800cb7893612
6
+ metadata.gz: 6b4f0d9bfcaac6d44f753aba01d1fcba3ace14d292821e7ed00c0f0fa3678f90ce06af66ca49764c0dd5fc5dbc484c48b1abd3f1fff9453d5b25e7ec12cd3207
7
+ data.tar.gz: '09f952deebbc83e695d33216a3a8c81b47b6860d85266df3ec166655d5cd71a2e154185021a884cf185134dce7a24776d50a6542ecf82e194fa793780553c640'
data/CHANGELOG.md CHANGED
@@ -15,6 +15,20 @@ the loopback flow (RFC 8252), JWT signing and verification with a caching JWKS
15
15
  keyring, the three application credentials (ClientSecret with multi-zone,
16
16
  WebIdentity, WorkloadIdentity with pluggable token sources), and AccessContext.
17
17
 
18
+ ## 0.13.0-keycardai-oauth (2026-11-26)
19
+
20
+
21
+ - feat(keycardai-oauth): discover the application credential from the environment (SDK-6) (#99)
22
+ - * feat(keycardai-oauth): discover the application credential from the environment (SDK-6)
23
+ - Implements keycardai/keycard-sdk-spec credential-discovery.md v2
24
+ - Refs SDK-6
25
+ - Co-Authored-By: Larry Osakwe <larry@keycard.ai>
26
+ - * docs: count token revocation in the conformance summary
27
+ - ---------
28
+ - Co-authored-by: devin-ai-keycard <devin-ai@keycard.ai>
29
+ Co-authored-by: Larry Osakwe <larry@keycard.ai>
30
+ Co-authored-by: Larry Osakwe <23560354+Larry-Osakwe@users.noreply.github.com>
31
+
18
32
  ## 0.12.0-keycardai-oauth (2026-11-25)
19
33
 
20
34
 
data/README.md CHANGED
@@ -23,11 +23,37 @@ Capabilities:
23
23
  - UserInfo: the signed-in user's identity claims (OIDC Core 1.0 §5.3)
24
24
  - JWT signing and verification, JWKS keyring with caching
25
25
  - Application credentials: ClientSecret (incl. multi-zone), WebIdentity (RFC 7523),
26
- WorkloadIdentity with pluggable identity token sources
26
+ WorkloadIdentity with pluggable identity token sources, and opt-in discovery
27
27
  - AccessContext: the non-throwing per-request container for delegated tokens
28
28
 
29
29
  ## Quickstart
30
30
 
31
+ ### Discover an application credential
32
+
33
+ ```ruby
34
+ require "keycardai/oauth"
35
+
36
+ credential = Keycardai::OAuth.discover_application_credential
37
+ ```
38
+
39
+ Discovery runs only when called. It reads the process environment by default,
40
+ or an explicit string-keyed mapping passed as `env:`. It discovers a client
41
+ secret from `KEYCARD_CLIENT_ID` and `KEYCARD_CLIENT_SECRET`, workload identity
42
+ from the first non-empty variable in the `FileTokenSource` discovery list, or
43
+ web identity from `KEYCARD_WEB_IDENTITY_KEY_STORAGE_DIR`.
44
+
45
+ Set `KEYCARD_APPLICATION_CREDENTIAL_TYPE` to `client_secret`,
46
+ `workload_identity`, or `web_identity` to choose one when multiple types are
47
+ configured. `eks_workload_identity` is a deprecated alias for
48
+ `workload_identity`. Selecting `web_identity` without a storage directory uses
49
+ the WebIdentity default, and `web_identity_server_name:` supplies its key name.
50
+ A discovered WebIdentity has no registered client id; construct it explicitly
51
+ with `client_id:` for operations that sign client assertions.
52
+
53
+ Discovery configuration errors raise `Keycardai::OAuth::CredentialDiscoveryError`
54
+ with a symbolic `reason` and a `resolvable` list. Pass the zone URL separately;
55
+ credential discovery does not read `KEYCARD_ZONE_URL`.
56
+
31
57
  ### Verify an inbound token
32
58
 
33
59
  ```ruby
@@ -43,7 +43,7 @@ module Keycardai
43
43
  # client_authentication, or when neither credential nor a non-empty
44
44
  # client_id is set. A credential that responds to
45
45
  # check_client_authentication is checked with it, so a WorkloadIdentity
46
- # without client_id raises here. Call it before any request.
46
+ # or WebIdentity without client_id raises here. Call it before any request.
47
47
  #
48
48
  # @raise [ConfigurationError]
49
49
  def validate(credential:, client_id:, client_secret: nil)
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Keycardai
4
+ # OAuth primitives and application credentials for Keycard.
5
+ module OAuth
6
+ # Builds the application credential described by the environment.
7
+ #
8
+ # @param env [#[]] mapping with environment variable names as string keys
9
+ # @param web_identity_server_name [String, nil] name used for the WebIdentity key id
10
+ # @return [ClientSecret, WorkloadIdentity, WebIdentity]
11
+ # @raise [CredentialDiscoveryError] the configuration does not select a usable credential
12
+ # @raise [WorkloadIdentityConfigurationError] the selected token file is missing or empty
13
+ def self.discover_application_credential(env: ENV, web_identity_server_name: nil)
14
+ CredentialDiscovery.new(env).discover(web_identity_server_name)
15
+ end
16
+
17
+ # Resolves and constructs a credential from one environment mapping.
18
+ class CredentialDiscovery
19
+ SELECTOR = "KEYCARD_APPLICATION_CREDENTIAL_TYPE"
20
+ STORAGE_DIR = "KEYCARD_WEB_IDENTITY_KEY_STORAGE_DIR"
21
+ TYPES = %w[client_secret workload_identity web_identity].freeze
22
+
23
+ def initialize(env)
24
+ @client_id = value(env, "KEYCARD_CLIENT_ID")
25
+ @client_secret = value(env, "KEYCARD_CLIENT_SECRET")
26
+ @token_file = FileTokenSource::DEFAULT_ENV_VARS.lazy.filter_map { |name| value(env, name) }.first
27
+ @storage_dir = value(env, STORAGE_DIR)
28
+ @selector = value(env, SELECTOR)
29
+ @resolvable = []
30
+ @resolvable << "client_secret" if @client_id && @client_secret
31
+ @resolvable << "workload_identity" if @token_file
32
+ @resolvable << "web_identity" if @storage_dir
33
+ end
34
+
35
+ def discover(web_identity_server_name)
36
+ case @selector ? selected_type : inferred_type
37
+ when "client_secret"
38
+ ClientSecret.new(@client_id, @client_secret)
39
+ when "workload_identity"
40
+ WorkloadIdentity.new(source: FileTokenSource.new(token_file_path: @token_file), client_id: @client_id)
41
+ when "web_identity"
42
+ WebIdentity.new(server_name: web_identity_server_name, storage_dir: @storage_dir)
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def value(env, name)
49
+ value = env[name]
50
+ value if value && !value.empty?
51
+ end
52
+
53
+ def selected_type
54
+ type = @selector == "eks_workload_identity" ? "workload_identity" : @selector
55
+ fail_discovery(:unknown_type, "Unknown #{SELECTOR}; use one of #{TYPES.join(", ")}") unless TYPES.include?(type)
56
+ return type if type == "web_identity" || @resolvable.include?(type)
57
+
58
+ inputs = if type == "client_secret"
59
+ "both KEYCARD_CLIENT_ID and KEYCARD_CLIENT_SECRET"
60
+ else
61
+ "a token file path in one of #{FileTokenSource::DEFAULT_ENV_VARS.join(", ")}"
62
+ end
63
+ fail_discovery(:not_satisfiable, "#{SELECTOR}=#{type} requires #{inputs}")
64
+ end
65
+
66
+ def inferred_type
67
+ if @client_secret && !@client_id
68
+ fail_discovery(:incomplete, "KEYCARD_CLIENT_SECRET is set without KEYCARD_CLIENT_ID")
69
+ end
70
+ if @resolvable.empty?
71
+ fail_discovery(:absent, "No application credential found in the environment. Set KEYCARD_CLIENT_ID " \
72
+ "and KEYCARD_CLIENT_SECRET, a token file variable, or #{STORAGE_DIR}")
73
+ end
74
+ if @resolvable.size > 1
75
+ fail_discovery(:ambiguous, "The environment configures #{@resolvable.join(" and ")}. " \
76
+ "Set #{SELECTOR} to choose one")
77
+ end
78
+ @resolvable.first
79
+ end
80
+
81
+ def fail_discovery(reason, message)
82
+ raise CredentialDiscoveryError.new(message, reason: reason, resolvable: @resolvable)
83
+ end
84
+ end
85
+ private_constant :CredentialDiscovery
86
+ end
87
+ end
@@ -14,6 +14,20 @@ module Keycardai
14
14
  end
15
15
  end
16
16
 
17
+ # The environment does not describe a single usable application credential.
18
+ class CredentialDiscoveryError < ConfigurationError
19
+ # @return [Symbol] absent, ambiguous, incomplete, unknown_type, or not_satisfiable
20
+ attr_reader :reason
21
+ # @return [Array<String>] canonical credential types the environment configures
22
+ attr_reader :resolvable
23
+
24
+ def initialize(message, reason:, resolvable:)
25
+ super(message)
26
+ @reason = reason
27
+ @resolvable = resolvable.dup.freeze
28
+ end
29
+ end
30
+
17
31
  # A transport-level failure (DNS, TLS, timeout) wrapping the underlying
18
32
  # exception as +cause+.
19
33
  class NetworkError < Keycardai::Error
@@ -10,11 +10,10 @@ module Keycardai
10
10
  # Covers EKS (AWS_* env vars), AKS (AZURE_FEDERATED_TOKEN_FILE), any
11
11
  # Kubernetes projected service-account token, and file-mode CI providers.
12
12
  #
13
- # Path discovery through the well-known env vars below is the one blessed
14
- # environment read in the SDK (a deliberate platform convention). An
15
- # explicit token_file_path wins; env_var_name is consulted first when
16
- # given. The resolved file is validated at construction (exists,
17
- # non-empty), preserving fail-fast behavior.
13
+ # Path discovery through the well-known env vars follows a platform
14
+ # convention. An explicit token_file_path wins; env_var_name is consulted
15
+ # first when given. The resolved file must exist and be non-empty at
16
+ # construction.
18
17
  class FileTokenSource
19
18
  DEFAULT_ENV_VARS = %w[
20
19
  KEYCARD_EKS_WORKLOAD_IDENTITY_TOKEN_FILE
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Keycardai
4
4
  module OAuth
5
- VERSION = "0.12.0"
5
+ VERSION = "0.13.0"
6
6
  end
7
7
  end
@@ -14,8 +14,8 @@ module Keycardai
14
14
  # Holds no shared secret and contributes no Basic header. Never reads
15
15
  # environment variables; the caller supplies storage configuration.
16
16
  class WebIdentity
17
- # @param client_id [String] the registered OAuth client id, signed as
18
- # the assertion's iss and sub
17
+ # @param client_id [String, nil] the registered OAuth client id, signed
18
+ # as the assertion's iss and sub; required for client authentication
19
19
  # @param server_name [String, nil] sanitized into the key id when no
20
20
  # key_id is given
21
21
  # @param key_id [String, nil] explicit key id (the JWT kid); otherwise
@@ -23,11 +23,8 @@ module Keycardai
23
23
  # @param storage [#load, #store, nil] pluggable keypair storage
24
24
  # @param storage_dir [String, nil] directory for the default file store
25
25
  # @param clock [#call] returns the current Time; override in tests
26
- # @raise [ConfigurationError] missing client_id
27
- def initialize(client_id:, server_name: nil, key_id: nil, storage: nil, storage_dir: nil,
26
+ def initialize(client_id: nil, server_name: nil, key_id: nil, storage: nil, storage_dir: nil,
28
27
  clock: -> { Time.now })
29
- raise ConfigurationError, "WebIdentity requires a client_id" if client_id.nil? || client_id.empty?
30
-
31
28
  @client_id = client_id
32
29
  storage ||= FilePrivateKeyStorage.new(dir: storage_dir || FilePrivateKeyStorage::DEFAULT_DIR)
33
30
  @key_manager = PrivateKeyManager.new(
@@ -59,7 +56,7 @@ module Keycardai
59
56
  # @param scope [String, nil]
60
57
  # @param issuer [String, nil] unused; part of the Credential interface
61
58
  # @return [Hash]
62
- # @raise [ConfigurationError] when token_endpoint is missing
59
+ # @raise [ConfigurationError] when the client_id or token_endpoint is missing
63
60
  def prepare_token_exchange_request(subject_token:, token_endpoint: nil, resource: nil,
64
61
  audience: nil, scope: nil, issuer: nil)
65
62
  if token_endpoint.nil? || token_endpoint.empty?
@@ -83,17 +80,25 @@ module Keycardai
83
80
  # @param issuer [String, nil] unused; part of the Credential interface
84
81
  # @param endpoint [String] the assertion audience URL
85
82
  # @return [ClientAuthentication]
86
- # @raise [ConfigurationError] when endpoint is missing
83
+ # @raise [ConfigurationError] when the client_id or endpoint is missing
87
84
  def client_authentication(issuer:, endpoint:)
88
85
  if endpoint.nil? || endpoint.empty?
89
86
  raise ConfigurationError, "WebIdentity requires the endpoint URL as the assertion audience"
90
87
  end
91
88
 
89
+ check_client_authentication
92
90
  assertion = @key_manager.create_client_assertion(client_id: @client_id, audience: endpoint)
93
91
  ClientAuthentication.new(params: { "client_assertion" => assertion,
94
92
  "client_assertion_type" => PrivateKeyManager::ASSERTION_TYPE })
95
93
  end
96
94
 
95
+ # @raise [ConfigurationError] when the client_id is missing
96
+ def check_client_authentication
97
+ return unless @client_id.nil? || @client_id.empty?
98
+
99
+ raise ConfigurationError, "WebIdentity requires a client_id for client authentication"
100
+ end
101
+
97
102
  # The public JWKS the authorization server verifies assertions against.
98
103
  #
99
104
  # @return [Hash] {"keys" => [...]}
@@ -20,6 +20,7 @@ require_relative "oauth/private_key"
20
20
  require_relative "oauth/web_identity"
21
21
  require_relative "oauth/workload_identity"
22
22
  require_relative "oauth/token_sources"
23
+ require_relative "oauth/credential_discovery"
23
24
  require_relative "oauth/access_context"
24
25
  require_relative "oauth/exchange_tokens"
25
26
  require_relative "oauth/token_verifier"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keycardai-oauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keycard
@@ -43,6 +43,7 @@ files:
43
43
  - lib/keycardai/oauth/client_authentication.rb
44
44
  - lib/keycardai/oauth/client_credentials_client.rb
45
45
  - lib/keycardai/oauth/client_secret.rb
46
+ - lib/keycardai/oauth/credential_discovery.rb
46
47
  - lib/keycardai/oauth/device_authorization.rb
47
48
  - lib/keycardai/oauth/discovery.rb
48
49
  - lib/keycardai/oauth/errors.rb