Skip to content

Commit 250f3e0

Browse files
committed
Refactor JWT::JWK::Set#initialize
The two construction paths that build keys from hashes had drifted: the JWKS document form skips a key whose kty is unsupported, the array form raises. That is easy to miss when the two are nested blocks in the middle of a case expression, and it is how the gap in jwt#744 came about. Give each branch a named method, so what differs between them is visible in the branch itself. initialize now fits the metrics limits without the Metrics/CyclomaticComplexity exemption it has carried until now. No behaviour change.
1 parent 6ab1be7 commit 250f3e0

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
**Fixes and enhancements:**
1414

15+
- Refactor `JWT::JWK::Set#initialize` so each construction path is a named method [#758](https://github.com/jwt/ruby-jwt/pull/758) ([@anakinj](https://github.com/anakinj))
1516
- Fix rejection of unknown algorithms from JWKs for RFC compliance and pquip [#728](https://github.com/jwt/ruby-jwt/pull/728)
1617
- Fix the `Style/DirectiveScope` RuboCop offense failing the build [#752](https://github.com/jwt/ruby-jwt/pull/752)
1718
- Fix `JWT::JWK::Set` sharing its key collection with the set it was copied from [#751](https://github.com/jwt/ruby-jwt/pull/751)

lib/jwt/jwk/set.rb

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,14 @@ class Set
1212

1313
attr_reader :keys
1414

15-
def initialize(jwks = nil, options = {}) # rubocop:disable Metrics/CyclomaticComplexity
16-
jwks ||= {}
17-
15+
def initialize(jwks = nil, options = {})
1816
@keys = case jwks
19-
when JWT::JWK::Set # Simple duplication
20-
jwks.keys.dup
21-
when JWT::JWK::KeyBase # Singleton
22-
[jwks]
23-
when Hash
24-
jwks = jwks.transform_keys(&:to_sym)
25-
[*jwks[:keys]].each_with_object([]) do |k, arr|
26-
arr << JWT::JWK.new(k, nil, options)
27-
rescue JWT::UnsupportedKeyType
28-
nil
29-
end
30-
when Array
31-
jwks.map { |k| JWT::JWK.new(k, nil, options) }
32-
else
33-
raise ArgumentError, 'Can only create new JWKS from Hash, Array and JWK'
17+
when nil then []
18+
when JWT::JWK::Set then jwks.keys.dup # Simple duplication
19+
when JWT::JWK::KeyBase then [jwks] # Singleton
20+
when Hash then build_supported_keys(jwks.transform_keys(&:to_sym)[:keys], options)
21+
when Array then build_keys(jwks, options)
22+
else raise ArgumentError, 'Can only create new JWKS from Hash, Array and JWK'
3423
end
3524
end
3625

@@ -88,6 +77,23 @@ def ==(other)
8877
alias | union
8978
alias + union
9079
alias << add
80+
81+
private
82+
83+
def build_keys(keys, options)
84+
[*keys].map { |key| JWT::JWK.new(key, nil, options) }
85+
end
86+
87+
# A key whose kty this library does not implement is skipped rather than
88+
# failing the whole set, as RFC 7517 section 5 requires. Only the JWKS
89+
# document form does this today; see #744 for the array form.
90+
def build_supported_keys(keys, options)
91+
[*keys].each_with_object([]) do |key, arr|
92+
arr << JWT::JWK.new(key, nil, options)
93+
rescue JWT::UnsupportedKeyType
94+
nil
95+
end
96+
end
9197
end
9298
end
9399
end

0 commit comments

Comments
 (0)