omniauth_openid_federation 2.1.0 → 2.2.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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -15
  3. data/README.md +36 -11
  4. data/app/controllers/omniauth_openid_federation/federation_controller.rb +21 -55
  5. data/config/polyrun_coverage.yml +1 -1
  6. data/config/routes.rb +3 -0
  7. data/examples/README_INTEGRATION_TESTING.md +3 -3
  8. data/examples/README_MOCK_OP.md +11 -11
  9. data/examples/integration_test_flow.rb +5 -5
  10. data/examples/jobs/README.md +11 -11
  11. data/examples/mock_op_server.rb +1 -1
  12. data/examples/relying_party_key_rotation.rb.example +29 -0
  13. data/lib/omniauth_openid_federation/client_keys.rb +38 -0
  14. data/lib/omniauth_openid_federation/constants.rb +8 -0
  15. data/lib/omniauth_openid_federation/controller.rb +34 -0
  16. data/lib/omniauth_openid_federation/engine.rb +7 -6
  17. data/lib/omniauth_openid_federation/jwks/normalizer.rb +8 -2
  18. data/lib/omniauth_openid_federation/jwt_response_decoder.rb +9 -4
  19. data/lib/omniauth_openid_federation/key_resolver.rb +36 -0
  20. data/lib/omniauth_openid_federation/oidc_client.rb +17 -2
  21. data/lib/omniauth_openid_federation/rack_endpoint.rb +5 -5
  22. data/lib/omniauth_openid_federation/rails.rb +25 -0
  23. data/lib/omniauth_openid_federation/relying_party_key_store.rb +110 -0
  24. data/lib/omniauth_openid_federation/strategy/authorization_request.rb +5 -3
  25. data/lib/omniauth_openid_federation/strategy/client_construction.rb +14 -7
  26. data/lib/omniauth_openid_federation/strategy/id_token_decoding.rb +7 -4
  27. data/lib/omniauth_openid_federation/strategy/userinfo_decoding.rb +7 -4
  28. data/lib/omniauth_openid_federation/strategy.rb +4 -0
  29. data/lib/omniauth_openid_federation/tasks/callback_processor.rb +7 -7
  30. data/lib/omniauth_openid_federation/tasks/local_endpoint_tester.rb +3 -3
  31. data/lib/omniauth_openid_federation/utils.rb +21 -14
  32. data/lib/omniauth_openid_federation/validators.rb +8 -14
  33. data/lib/omniauth_openid_federation/version.rb +1 -1
  34. data/lib/omniauth_openid_federation.rb +5 -1
  35. data/lib/tasks/omniauth_openid_federation.rake +96 -96
  36. metadata +9 -3
@@ -114,8 +114,13 @@ module OmniauthOpenidFederation
114
114
  raw_client_options = @strategy_options[:client_options] || @strategy_options["client_options"] || {}
115
115
  client_options = OmniauthOpenidFederation::Validators.normalize_hash(raw_client_options)
116
116
 
117
- private_key = client_options[:private_key] ||
118
- ((@client.respond_to?(:private_key) ? @client.private_key : nil))
117
+ local_encryption_key = OmniauthOpenidFederation::ClientKeys.encryption_key(
118
+ client_options,
119
+ {
120
+ signing_key: @strategy_options[:signing_key],
121
+ encryption_key: @strategy_options[:encryption_key]
122
+ }
123
+ ) || ((@client.respond_to?(:private_key) ? @client.private_key : nil))
119
124
  jwks = client_options[:jwks] || client_options["jwks"]
120
125
 
121
126
  metadata = nil
@@ -135,8 +140,8 @@ module OmniauthOpenidFederation
135
140
  key = OmniauthOpenidFederation::KeyExtractor.extract_encryption_key(
136
141
  jwks: jwks,
137
142
  metadata: metadata,
138
- private_key: private_key
139
- ) || private_key
143
+ private_key: local_encryption_key
144
+ ) || local_encryption_key
140
145
 
141
146
  OmniauthOpenidFederation::Validators.validate_private_key!(key)
142
147
  key
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+ require_relative "errors"
5
+
6
+ module OmniauthOpenidFederation
7
+ # Resolves RP key material at use time (object, PEM string, or callable).
8
+ module KeyResolver
9
+ MAX_DEPTH = 5
10
+
11
+ module_function
12
+
13
+ # @param value [OpenSSL::PKey::RSA, String, #call, nil]
14
+ # @return [OpenSSL::PKey::RSA, nil]
15
+ def resolve(value, depth: 0)
16
+ return nil if value.nil?
17
+ raise ConfigurationError, "Key resolver recursion limit exceeded" if depth > MAX_DEPTH
18
+
19
+ case value
20
+ when OpenSSL::PKey::RSA
21
+ value
22
+ when String
23
+ OpenSSL::PKey::RSA.new(value)
24
+ else
25
+ if value.respond_to?(:call)
26
+ resolve(value.call, depth: depth + 1)
27
+ else
28
+ raise ConfigurationError,
29
+ "Private key must be an OpenSSL::PKey::RSA instance, PEM string, or callable (got #{value.class})"
30
+ end
31
+ end
32
+ rescue OpenSSL::PKey::PKeyError, ArgumentError => error
33
+ raise ConfigurationError, "Invalid private key format: #{error.message}"
34
+ end
35
+ end
36
+ end
@@ -5,13 +5,15 @@ require "uri"
5
5
 
6
6
  require_relative "errors"
7
7
  require_relative "access_token"
8
+ require_relative "key_resolver"
9
+ require_relative "validators"
8
10
 
9
11
  module OmniauthOpenidFederation
10
12
  class OidcClient < OAuth2::Client
11
13
  CLIENT_ASSERTION_TYPE = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
12
14
  CLIENT_ASSERTION_EXPIRATION_SECONDS = 180
13
15
 
14
- attr_accessor :private_key, :authorization_code, :jwks_uri
16
+ attr_accessor :authorization_code, :jwks_uri
15
17
  attr_reader :authorization_endpoint, :token_endpoint, :userinfo_endpoint
16
18
 
17
19
  def initialize(identifier:, redirect_uri:, authorization_endpoint:, token_endpoint:, secret: nil,
@@ -34,6 +36,7 @@ module OmniauthOpenidFederation
34
36
  @userinfo_endpoint = userinfo_endpoint
35
37
  @jwks_uri = jwks_uri
36
38
  @authorization_code = nil
39
+ @private_key_source = nil
37
40
  end
38
41
 
39
42
  def identifier
@@ -56,6 +59,15 @@ module OmniauthOpenidFederation
56
59
  URI.parse(site.to_s).host
57
60
  end
58
61
 
62
+ # Stores key material or a callable resolved on each use (rotation without restart).
63
+ def private_key=(value)
64
+ @private_key_source = value
65
+ end
66
+
67
+ def private_key
68
+ KeyResolver.resolve(@private_key_source)
69
+ end
70
+
59
71
  def access_token!(client_auth_method = :jwt_bearer)
60
72
  unless client_auth_method == :jwt_bearer
61
73
  raise ConfigurationError, "Unsupported client_auth_method: #{client_auth_method}"
@@ -85,6 +97,9 @@ module OmniauthOpenidFederation
85
97
  end
86
98
 
87
99
  def build_client_assertion
100
+ key = private_key
101
+ OmniauthOpenidFederation::Validators.validate_private_key!(key)
102
+
88
103
  now = Time.now.to_i
89
104
  payload = {
90
105
  iss: identifier,
@@ -95,7 +110,7 @@ module OmniauthOpenidFederation
95
110
  exp: now + CLIENT_ASSERTION_EXPIRATION_SECONDS
96
111
  }
97
112
 
98
- JWT.encode(payload, private_key, "RS256")
113
+ JWT.encode(payload, key, "RS256")
99
114
  end
100
115
  end
101
116
  end
@@ -48,7 +48,7 @@ module OmniauthOpenidFederation
48
48
  handle_fetch(request)
49
49
  when "/jwks.json"
50
50
  handle_jwks
51
- when "/signed-jwks.json"
51
+ when "/signed-jwks.json", "/signed-jwks.jwt", "/signed-jwks.jose"
52
52
  handle_signed_jwks
53
53
  else
54
54
  not_found
@@ -73,7 +73,7 @@ module OmniauthOpenidFederation
73
73
  entity_statement = OmniauthOpenidFederation::FederationEndpoint.generate_entity_statement
74
74
 
75
75
  headers = {
76
- "Content-Type" => "application/jwt",
76
+ "Content-Type" => Constants::CONTENT_TYPE_ENTITY_STATEMENT_JWT,
77
77
  "Cache-Control" => "public, max-age=3600"
78
78
  }
79
79
 
@@ -100,7 +100,7 @@ module OmniauthOpenidFederation
100
100
  end
101
101
 
102
102
  headers = {
103
- "Content-Type" => "application/json",
103
+ "Content-Type" => Constants::CONTENT_TYPE_JSON,
104
104
  "Cache-Control" => "public, max-age=3600"
105
105
  }
106
106
 
@@ -144,7 +144,7 @@ module OmniauthOpenidFederation
144
144
  end
145
145
 
146
146
  headers = {
147
- "Content-Type" => "application/entity-statement+jwt",
147
+ "Content-Type" => Constants::CONTENT_TYPE_ENTITY_STATEMENT_JWT,
148
148
  "Cache-Control" => "public, max-age=3600"
149
149
  }
150
150
 
@@ -171,7 +171,7 @@ module OmniauthOpenidFederation
171
171
  end
172
172
 
173
173
  headers = {
174
- "Content-Type" => "application/jwt",
174
+ "Content-Type" => Constants::CONTENT_TYPE_JWT,
175
175
  "Cache-Control" => "public, max-age=3600"
176
176
  }
177
177
 
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Explicit Rails entrypoint for Gemfile `require: false` consumers.
4
+ #
5
+ # # Gemfile
6
+ # gem "omniauth_openid_federation", require: false
7
+ #
8
+ # # config/application.rb or an early initializer
9
+ # require "omniauth_openid_federation/rails"
10
+ # OmniauthOpenidFederation.base_controller = "ActionController::API" # optional
11
+ #
12
+ # Prefer mounting OmniauthOpenidFederation::Engine, or use RackEndpoint alone for
13
+ # hosts that must stay outside Action Controller filters.
14
+ require "omniauth_openid_federation"
15
+
16
+ unless defined?(Rails)
17
+ raise LoadError, "omniauth_openid_federation/rails requires Rails"
18
+ end
19
+
20
+ require_relative "controller"
21
+ require_relative "engine"
22
+ require_relative "railtie"
23
+
24
+ controller_path = File.expand_path("../../app/controllers/omniauth_openid_federation/federation_controller.rb", __dir__)
25
+ require controller_path if File.file?(controller_path)
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+ require "base64"
5
+ require_relative "errors"
6
+ require_relative "constants"
7
+ require_relative "cache_adapter"
8
+
9
+ module OmniauthOpenidFederation
10
+ # Optional RP private-key storage. Hosts may keep keys in file, ENV, or cache.
11
+ class RelyingPartyKeyStore
12
+ Record = Data.define(:signing_key, :encryption_key)
13
+
14
+ def load
15
+ raise NotImplementedError, "#{self.class}#load"
16
+ end
17
+
18
+ def store(signing_key:, encryption_key:)
19
+ raise NotImplementedError, "#{self.class}#store"
20
+ end
21
+
22
+ def rotate
23
+ signing_key = OpenSSL::PKey::RSA.new(Constants::MIN_RSA_KEY_BITS)
24
+ encryption_key = OpenSSL::PKey::RSA.new(Constants::MIN_RSA_KEY_BITS)
25
+ store(signing_key: signing_key, encryption_key: encryption_key)
26
+ Record.new(signing_key: signing_key, encryption_key: encryption_key)
27
+ end
28
+
29
+ class FileStore < RelyingPartyKeyStore
30
+ def initialize(signing_path:, encryption_path: nil)
31
+ @signing_path = signing_path
32
+ @encryption_path = encryption_path || signing_path
33
+ end
34
+
35
+ def load
36
+ Record.new(
37
+ signing_key: OpenSSL::PKey::RSA.new(File.read(@signing_path)),
38
+ encryption_key: OpenSSL::PKey::RSA.new(File.read(@encryption_path))
39
+ )
40
+ end
41
+
42
+ def store(signing_key:, encryption_key:)
43
+ File.write(@signing_path, signing_key.to_pem)
44
+ File.chmod(0o600, @signing_path)
45
+ File.write(@encryption_path, encryption_key.to_pem)
46
+ File.chmod(0o600, @encryption_path)
47
+ Record.new(signing_key: signing_key, encryption_key: encryption_key)
48
+ end
49
+ end
50
+
51
+ class EnvStore < RelyingPartyKeyStore
52
+ def initialize(signing_env:, encryption_env: nil, base64: true)
53
+ @signing_env = signing_env
54
+ @encryption_env = encryption_env || signing_env
55
+ @base64 = base64
56
+ end
57
+
58
+ def load
59
+ Record.new(
60
+ signing_key: decode(ENV.fetch(@signing_env)),
61
+ encryption_key: decode(ENV.fetch(@encryption_env))
62
+ )
63
+ end
64
+
65
+ def store(signing_key:, encryption_key:)
66
+ raise ConfigurationError, "EnvStore is read-only; set #{@signing_env} / #{@encryption_env}"
67
+ end
68
+
69
+ private
70
+
71
+ def decode(value)
72
+ pem = @base64 ? Base64.decode64(value) : value
73
+ OpenSSL::PKey::RSA.new(pem)
74
+ end
75
+ end
76
+
77
+ class CacheStore < RelyingPartyKeyStore
78
+ SIGNING_SUFFIX = "signing_pem"
79
+ ENCRYPTION_SUFFIX = "encryption_pem"
80
+
81
+ def initialize(cache_key_prefix:, expires_in: nil)
82
+ @prefix = cache_key_prefix
83
+ @expires_in = expires_in
84
+ end
85
+
86
+ def load
87
+ signing_pem = CacheAdapter.read(key(SIGNING_SUFFIX))
88
+ encryption_pem = CacheAdapter.read(key(ENCRYPTION_SUFFIX))
89
+ raise ConfigurationError, "RP keys missing from cache (#{@prefix})" if signing_pem.nil? || encryption_pem.nil?
90
+
91
+ Record.new(
92
+ signing_key: OpenSSL::PKey::RSA.new(signing_pem),
93
+ encryption_key: OpenSSL::PKey::RSA.new(encryption_pem)
94
+ )
95
+ end
96
+
97
+ def store(signing_key:, encryption_key:)
98
+ CacheAdapter.write(key(SIGNING_SUFFIX), signing_key.to_pem, expires_in: @expires_in)
99
+ CacheAdapter.write(key(ENCRYPTION_SUFFIX), encryption_key.to_pem, expires_in: @expires_in)
100
+ Record.new(signing_key: signing_key, encryption_key: encryption_key)
101
+ end
102
+
103
+ private
104
+
105
+ def key(suffix)
106
+ "#{@prefix}:#{suffix}"
107
+ end
108
+ end
109
+ end
110
+ end
@@ -54,8 +54,10 @@ module OmniauthOpenidFederation
54
54
  # Enforce signed request objects (RFC 9101) - unsigned requests are not allowed
55
55
  client_options_hash = options.client_options || {}
56
56
  normalized_options = OmniauthOpenidFederation::Validators.normalize_hash(client_options_hash)
57
- private_key = normalized_options[:private_key]
58
- OmniauthOpenidFederation::Validators.validate_private_key!(private_key)
57
+ private_key = OmniauthOpenidFederation::ClientKeys.validate_signing_key!(
58
+ normalized_options,
59
+ {signing_key: options.signing_key, encryption_key: options.encryption_key}
60
+ )
59
61
 
60
62
  resolved_issuer = options.issuer
61
63
  unless OmniauthOpenidFederation::StringHelpers.present?(resolved_issuer)
@@ -151,7 +153,7 @@ module OmniauthOpenidFederation
151
153
  hd: validated_hd,
152
154
  acr_values: normalized_acr_values,
153
155
  extra_params: options.extra_authorize_params || {},
154
- private_key: normalized_options[:private_key],
156
+ private_key: private_key,
155
157
  jwks: jwks,
156
158
  entity_statement_path: options.entity_statement_path,
157
159
  key_source: signing_key_source,
@@ -74,21 +74,22 @@ module OmniauthOpenidFederation
74
74
  jwks_uri: jwks_uri_endpoint
75
75
  )
76
76
 
77
- # Store private key for client assertion (private_key_jwt authentication)
78
- oidc_client.private_key = merged_options[:private_key] || merged_options["private_key"]
77
+ # Store key source (object, PEM, or callable) - resolved on each assertion
78
+ oidc_client.private_key = merged_options[:signing_key] ||
79
+ merged_options["signing_key"] ||
80
+ options.signing_key ||
81
+ merged_options[:private_key] ||
82
+ merged_options["private_key"]
79
83
 
80
- # Store strategy options on client for AccessToken to access later
81
- # This allows AccessToken to get configuration without relying on Devise
82
- # Ensure all entity statement options are included (to_h might not include all options)
83
84
  strategy_options_hash = options.to_h.dup
84
- # Explicitly include entity statement options that AccessToken needs
85
85
  strategy_options_hash[:entity_statement_path] = options.entity_statement_path if options.entity_statement_path
86
86
  strategy_options_hash[:entity_statement_url] = options.entity_statement_url if options.entity_statement_url
87
87
  strategy_options_hash[:entity_statement_fingerprint] = options.entity_statement_fingerprint if options.entity_statement_fingerprint
88
88
  strategy_options_hash[:issuer] = options.issuer if options.issuer
89
+ strategy_options_hash[:signing_key] = options.signing_key if options.signing_key
90
+ strategy_options_hash[:encryption_key] = options.encryption_key if options.encryption_key
89
91
  oidc_client.instance_variable_set(:@strategy_options, strategy_options_hash)
90
92
 
91
- # OidcClient extends OAuth2::Client, so it's compatible with OmniAuth::Strategies::OAuth2
92
93
  oidc_client
93
94
  end
94
95
  end
@@ -96,6 +97,12 @@ module OmniauthOpenidFederation
96
97
  def oidc_client
97
98
  client
98
99
  end
100
+
101
+ # Drop memoized OAuth/OIDC client so the next request rebuilds endpoints.
102
+ # Key material on the client is already resolved per use when callables are configured.
103
+ def reset_client!
104
+ @client = nil
105
+ end
99
106
  end
100
107
  end
101
108
  end
@@ -14,7 +14,10 @@ module OmniauthOpenidFederation
14
14
 
15
15
  if encrypted_token?(id_token)
16
16
  decryption_key_source = options.decryption_key_source || options.key_source || :local
17
- private_key = normalized_options[:private_key]
17
+ local_encryption_key = OmniauthOpenidFederation::ClientKeys.encryption_key(
18
+ normalized_options,
19
+ {signing_key: options.signing_key, encryption_key: options.encryption_key}
20
+ )
18
21
  jwks = normalized_options[:jwks] || normalized_options["jwks"]
19
22
  metadata = load_metadata_for_key_extraction
20
23
 
@@ -23,10 +26,10 @@ module OmniauthOpenidFederation
23
26
  OmniauthOpenidFederation::KeyExtractor.extract_encryption_key(
24
27
  jwks: jwks,
25
28
  metadata: metadata,
26
- private_key: private_key
27
- )
29
+ private_key: local_encryption_key
30
+ ) || local_encryption_key
28
31
  when :local
29
- private_key
32
+ local_encryption_key
30
33
  else
31
34
  raise OmniauthOpenidFederation::ConfigurationError, "Unknown decryption key source: #{decryption_key_source}"
32
35
  end
@@ -42,7 +42,10 @@ module OmniauthOpenidFederation
42
42
  normalized_options = OmniauthOpenidFederation::Validators.normalize_hash(client_options_hash)
43
43
 
44
44
  decryption_key_source = options.decryption_key_source || options.key_source || :local
45
- private_key = normalized_options[:private_key]
45
+ local_encryption_key = OmniauthOpenidFederation::ClientKeys.encryption_key(
46
+ normalized_options,
47
+ {signing_key: options.signing_key, encryption_key: options.encryption_key}
48
+ )
46
49
  jwks = normalized_options[:jwks] || normalized_options["jwks"]
47
50
  metadata = load_metadata_for_key_extraction
48
51
 
@@ -50,10 +53,10 @@ module OmniauthOpenidFederation
50
53
  OmniauthOpenidFederation::KeyExtractor.extract_encryption_key(
51
54
  jwks: jwks,
52
55
  metadata: metadata,
53
- private_key: private_key
54
- ) || private_key
56
+ private_key: local_encryption_key
57
+ ) || local_encryption_key
55
58
  else
56
- private_key
59
+ local_encryption_key
57
60
  end
58
61
 
59
62
  OmniauthOpenidFederation::Validators.validate_private_key!(encryption_key)
@@ -88,6 +88,10 @@ module OmniAuth
88
88
  option :key_source, :local # Key source: :local (use local static private_key) or :federation (use federation/JWKS) - used as default for both signing and decryption
89
89
  option :signing_key_source, nil # Signing key source: :local, :federation, or nil (uses key_source)
90
90
  option :decryption_key_source, nil # Decryption key source: :local, :federation, or nil (uses key_source)
91
+ # First-class RP keys (OpenSSL::PKey::RSA, PEM string, or callable). Prefer these over a single private_key.
92
+ # When key_source/signing_key_source is :federation, JWKS must include private material (`d`) for local signing.
93
+ option :signing_key, nil
94
+ option :encryption_key, nil
91
95
  option :entity_statement_path, nil # Path to provider entity statement JWT file (cached copy)
92
96
  option :entity_statement_url, nil # URL to provider entity statement (source of truth, Section 9)
93
97
  option :entity_statement_fingerprint, nil # Expected SHA-256 fingerprint for verification
@@ -182,27 +182,27 @@ module OmniauthOpenidFederation
182
182
  # Validate OpenID Federation compliance
183
183
  results[:compliance_checks] = {
184
184
  "Signed Request Objects" => {
185
- status: "✅ MANDATORY",
185
+ status: "MANDATORY",
186
186
  description: "All requests use signed request objects (RFC 9101)",
187
187
  verified: true
188
188
  },
189
189
  "ID Token Encryption" => {
190
- status: "✅ MANDATORY",
190
+ status: "MANDATORY",
191
191
  description: "ID tokens are encrypted (RSA-OAEP + A128CBC-HS256)",
192
192
  verified: OmniauthOpenidFederation::Jwe.encrypted?(id_token_raw)
193
193
  },
194
194
  "Client Assertion (private_key_jwt)" => {
195
- status: "✅ MANDATORY",
195
+ status: "MANDATORY",
196
196
  description: "Token endpoint uses private_key_jwt authentication",
197
197
  verified: true
198
198
  },
199
199
  "Entity Statement JWKS" => {
200
- status: "✅ MANDATORY",
200
+ status: "MANDATORY",
201
201
  description: "JWKS extracted from entity statement",
202
202
  verified: StringHelpers.present?(entity_statement_path) || StringHelpers.present?(entity_statement_url)
203
203
  },
204
204
  "Signed JWKS Support" => {
205
- status: "✅ MANDATORY",
205
+ status: "MANDATORY",
206
206
  description: "Supports OpenID Federation signed JWKS for key rotation",
207
207
  verified: true
208
208
  }
@@ -211,7 +211,7 @@ module OmniauthOpenidFederation
211
211
  # Check for client entity statement (optional but recommended)
212
212
  if StringHelpers.present?(client_entity_statement_path) || StringHelpers.present?(client_entity_statement_url)
213
213
  results[:compliance_checks]["Client Entity Statement"] = {
214
- status: "✅ RECOMMENDED",
214
+ status: "RECOMMENDED",
215
215
  description: "Client entity statement for federation-based key management",
216
216
  verified: true
217
217
  }
@@ -220,7 +220,7 @@ module OmniauthOpenidFederation
220
220
  # Check registration type (automatic if client entity statement is provided)
221
221
  if StringHelpers.present?(client_entity_statement_path) || StringHelpers.present?(client_entity_statement_url)
222
222
  results[:compliance_checks]["Automatic Registration"] = {
223
- status: "✅ ENABLED",
223
+ status: "ENABLED",
224
224
  description: "Automatic client registration using entity statement",
225
225
  verified: true
226
226
  }
@@ -162,19 +162,19 @@ module OmniauthOpenidFederation
162
162
  {
163
163
  type: :single,
164
164
  count: keys.length,
165
- recommendation: "⚠️ Single key detected (duplicate Key IDs). This is NOT RECOMMENDED for production. Use separate signing and encryption keys for better security. Generate with: rake openid_federation:prepare_client_keys[separate]"
165
+ recommendation: "Warning: Single key detected (duplicate Key IDs). NOT RECOMMENDED for production. Prefer separate signing and encryption keys. Generate with: rake openid_federation:prepare_client_keys[separate]"
166
166
  }
167
167
  elsif has_both_uses && keys.length >= 2
168
168
  {
169
169
  type: :separate,
170
170
  count: keys.length,
171
- recommendation: "✅ Separate keys detected (recommended for production)"
171
+ recommendation: "Separate keys detected (recommended for production)"
172
172
  }
173
173
  elsif keys.length == 1
174
174
  {
175
175
  type: :single,
176
176
  count: 1,
177
- recommendation: "⚠️ Single key detected. This is NOT RECOMMENDED for production. Use separate signing and encryption keys for better security. Generate with: rake openid_federation:prepare_client_keys[separate]"
177
+ recommendation: "Warning: Single key detected. NOT RECOMMENDED for production. Prefer separate signing and encryption keys. Generate with: rake openid_federation:prepare_client_keys[separate]"
178
178
  }
179
179
  else
180
180
  {
@@ -120,25 +120,32 @@ module OmniauthOpenidFederation
120
120
  # @param key [OpenSSL::PKey::RSA] RSA private or public key
121
121
  # @param use [String, nil] Key use ("sig" for signing, "enc" for encryption, nil for both)
122
122
  # @return [Hash] JWK hash with kty, kid, n, e, and optionally use
123
- def self.rsa_key_to_jwk(key, use: "sig")
123
+ # @param include_private [Boolean] when true, include private RSA fields (`d`, ...).
124
+ # Public JWKS serving must keep this false so private material is not published.
125
+ def self.rsa_key_to_jwk(key, use: "sig", include_private: false)
124
126
  require "digest"
125
127
  require "base64"
128
+ require "jwt"
126
129
 
127
- n = Base64.urlsafe_encode64(key.n.to_s(2), padding: false)
128
- e = Base64.urlsafe_encode64(key.e.to_s(2), padding: false)
129
-
130
- public_key_pem = key.public_key.to_pem
131
- kid = Digest::SHA256.hexdigest(public_key_pem)[0, 16]
132
-
133
- jwk = {
134
- kty: "RSA",
135
- kid: kid,
136
- n: n,
137
- e: e
138
- }
130
+ jwk = if include_private
131
+ JWT::JWK.new(key).export(include_private: true)
132
+ else
133
+ public_key = key.respond_to?(:public_key) ? key.public_key : key
134
+ n = Base64.urlsafe_encode64(public_key.n.to_s(2), padding: false)
135
+ e = Base64.urlsafe_encode64(public_key.e.to_s(2), padding: false)
136
+ public_key_pem = public_key.to_pem
137
+ kid = Digest::SHA256.hexdigest(public_key_pem)[0, 16]
138
+ {
139
+ kty: "RSA",
140
+ kid: kid,
141
+ n: n,
142
+ e: e
143
+ }
144
+ end
139
145
 
146
+ jwk = jwk.transform_keys(&:to_sym)
140
147
  jwk[:use] = use if use
141
-
148
+ jwk.delete(:d) unless include_private
142
149
  jwk
143
150
  end
144
151
 
@@ -2,30 +2,24 @@
2
2
  require_relative "constants"
3
3
  require_relative "configuration"
4
4
  require_relative "string_helpers"
5
+ require_relative "key_resolver"
5
6
 
6
7
  module OmniauthOpenidFederation
7
8
  module Validators
8
9
  # Validate that a private key is present and valid
9
10
  #
10
- # @param private_key [OpenSSL::PKey::RSA, String, nil] The private key to validate
11
+ # @param private_key [OpenSSL::PKey::RSA, String, #call, nil]
11
12
  # @raise [ConfigurationError] If private key is missing or invalid
12
13
  def self.validate_private_key!(private_key, min_bits: Constants::MIN_RSA_KEY_BITS)
13
- if private_key.nil?
14
+ normalized_key = KeyResolver.resolve(private_key)
15
+
16
+ if normalized_key.nil?
14
17
  raise ConfigurationError, "Private key is required for signed request objects"
15
18
  end
16
19
 
17
- normalized_key =
18
- if private_key.is_a?(String)
19
- begin
20
- OpenSSL::PKey::RSA.new(private_key)
21
- rescue => error
22
- raise ConfigurationError, "Invalid private key format: #{error.message}"
23
- end
24
- elsif private_key.is_a?(OpenSSL::PKey::RSA)
25
- private_key
26
- else
27
- raise ConfigurationError, "Private key must be an OpenSSL::PKey::RSA instance or PEM string"
28
- end
20
+ unless normalized_key.is_a?(OpenSSL::PKey::RSA)
21
+ raise ConfigurationError, "Private key must be an OpenSSL::PKey::RSA instance, PEM string, or callable"
22
+ end
29
23
 
30
24
  if min_bits.to_i.positive? && normalized_key.n.num_bits < min_bits.to_i
31
25
  raise ConfigurationError,
@@ -1,3 +1,3 @@
1
1
  module OmniauthOpenidFederation
2
- VERSION = "2.1.0".freeze
2
+ VERSION = "2.2.0".freeze
3
3
  end
@@ -33,11 +33,15 @@ require_relative "omniauth_openid_federation/logger"
33
33
  require_relative "omniauth_openid_federation/configuration"
34
34
  require_relative "omniauth_openid_federation/errors"
35
35
  require_relative "omniauth_openid_federation/instrumentation"
36
+ require_relative "omniauth_openid_federation/constants"
37
+ require_relative "omniauth_openid_federation/key_resolver"
36
38
  require_relative "omniauth_openid_federation/validators"
37
39
  require_relative "omniauth_openid_federation/key_extractor"
38
- require_relative "omniauth_openid_federation/constants"
40
+ require_relative "omniauth_openid_federation/client_keys"
41
+ require_relative "omniauth_openid_federation/controller"
39
42
  require_relative "omniauth_openid_federation/cache"
40
43
  require_relative "omniauth_openid_federation/cache_adapter"
44
+ require_relative "omniauth_openid_federation/relying_party_key_store"
41
45
  require_relative "omniauth_openid_federation/utils"
42
46
  require_relative "omniauth_openid_federation/jwe"
43
47
  require_relative "omniauth_openid_federation/jws"