Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

**Fixes and enhancements:**

- 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))
- Fix rejection of unknown algorithms from JWKs for RFC compliance and pquip [#728](https://github.com/jwt/ruby-jwt/pull/728)
- Fix the `Style/DirectiveScope` RuboCop offense failing the build [#752](https://github.com/jwt/ruby-jwt/pull/752)
- Fix `JWT::JWK::Set` sharing its key collection with the set it was copied from [#751](https://github.com/jwt/ruby-jwt/pull/751)
Expand Down
39 changes: 21 additions & 18 deletions lib/jwt/jwk/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,14 @@ class Set

attr_reader :keys

def initialize(jwks = nil, options = {}) # rubocop:disable Metrics/CyclomaticComplexity
jwks ||= {}

def initialize(jwks = nil, options = {})
@keys = case jwks
when JWT::JWK::Set # Simple duplication
jwks.keys.dup
when JWT::JWK::KeyBase # Singleton
[jwks]
when Hash
jwks = jwks.transform_keys(&:to_sym)
[*jwks[:keys]].each_with_object([]) do |k, arr|
arr << JWT::JWK.new(k, nil, options)
rescue JWT::UnsupportedKeyType
nil
end
when Array
jwks.map { |k| JWT::JWK.new(k, nil, options) }
else
raise ArgumentError, 'Can only create new JWKS from Hash, Array and JWK'
when nil then []
when JWT::JWK::Set then jwks.keys.dup
when JWT::JWK::KeyBase then [jwks]
when Hash then build_supported_keys(jwks.transform_keys(&:to_sym)[:keys], options)
when Array then build_keys(jwks, options)
else raise ArgumentError, 'Can only create new JWKS from Hash, Array and JWK'
end
end

Expand Down Expand Up @@ -88,6 +77,20 @@ def ==(other)
alias | union
alias + union
alias << add

private

def build_keys(keys, options)
[*keys].map { |key| JWT::JWK.new(key, nil, options) }
end

def build_supported_keys(keys, options)
[*keys].each_with_object([]) do |key, arr|
arr << JWT::JWK.new(key, nil, options)
rescue JWT::UnsupportedKeyType
nil
end
end
end
end
end
Loading