Skip to content

Commit 8ff01c2

Browse files
bellebaumanakinj
authored andcommitted
JWKS: Bugfixes and Rubocop
1 parent 5c03189 commit 8ff01c2

File tree

6 files changed

+54
-38
lines changed

6 files changed

+54
-38
lines changed

lib/jwt/jwk/ec.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,7 @@ def initialize(key, params = nil, options = {})
2121
# For backwards compatibility when kid was a String
2222
params = { kid: params } if params.is_a?(String)
2323

24-
key_params = case key
25-
when JWT::JWK::EC
26-
key.export(include_private: true)
27-
when OpenSSL::PKey::EC # Accept OpenSSL key as input
28-
@keypair = key # Preserve the object to avoid recreation
29-
parse_ec_key(key)
30-
when Hash
31-
key.transform_keys(&:to_sym)
32-
else
33-
raise ArgumentError, 'key must be of type OpenSSL::PKey::EC or Hash with key parameters'
34-
end
24+
key_params = extract_key_params(key)
3525

3626
params = params.transform_keys(&:to_sym)
3727
check_jwk(key_params, params)
@@ -74,6 +64,20 @@ def []=(key, value)
7464

7565
private
7666

67+
def extract_key_params(key)
68+
case key
69+
when JWT::JWK::EC
70+
key.export(include_private: true)
71+
when OpenSSL::PKey::EC # Accept OpenSSL key as input
72+
@keypair = key # Preserve the object to avoid recreation
73+
parse_ec_key(key)
74+
when Hash
75+
key.transform_keys(&:to_sym)
76+
else
77+
raise ArgumentError, 'key must be of type OpenSSL::PKey::EC or Hash with key parameters'
78+
end
79+
end
80+
7781
def check_jwk(keypair, params)
7882
raise ArgumentError, 'cannot overwrite cryptographic key attributes' unless (EC_KEY_ELEMENTS & params.keys).empty?
7983
raise JWT::JWKError, "Incorrect 'kty' value: #{keypair[:kty]}, expected #{KTY}" unless keypair[:kty] == KTY

lib/jwt/jwk/hmac.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,7 @@ def initialize(key, params = nil, options = {})
1515
# For backwards compatibility when kid was a String
1616
params = { kid: params } if params.is_a?(String)
1717

18-
key_params = case key
19-
when JWT::JWK::HMAC
20-
key.export(include_private: true)
21-
when String # Accept String key as input
22-
{ kty: KTY, k: key }
23-
when Hash
24-
key.transform_keys(&:to_sym)
25-
else
26-
raise ArgumentError, 'key must be of type String or Hash with key parameters'
27-
end
18+
key_params = extract_key_params(key)
2819

2920
params = params.transform_keys(&:to_sym)
3021
check_jwk(key_params, params)
@@ -73,6 +64,19 @@ def []=(key, value)
7364

7465
private
7566

67+
def extract_key_params(key)
68+
case key
69+
when JWT::JWK::HMAC
70+
key.export(include_private: true)
71+
when String # Accept String key as input
72+
{ kty: KTY, k: key }
73+
when Hash
74+
key.transform_keys(&:to_sym)
75+
else
76+
raise ArgumentError, 'key must be of type String or Hash with key parameters'
77+
end
78+
end
79+
7680
def check_jwk(keypair, params)
7781
raise ArgumentError, 'cannot overwrite cryptographic key attributes' unless (HMAC_KEY_ELEMENTS & params.keys).empty?
7882
raise JWT::JWKError, "Incorrect 'kty' value: #{keypair[:kty]}, expected #{KTY}" unless keypair[:kty] == KTY

lib/jwt/jwk/key_base.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def kid
2525
self[:kid]
2626
end
2727

28+
def hash
29+
self[:kid].hash
30+
end
31+
2832
def [](key)
2933
@parameters[key.to_sym]
3034
end

lib/jwt/jwk/rsa.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module JWT
44
module JWK
5-
class RSA < KeyBase
5+
class RSA < KeyBase # rubocop:disable Metrics/ClassLength
66
BINARY = 2
77
KTY = 'RSA'
88
KTYS = [KTY, OpenSSL::PKey::RSA, JWT::JWK::RSA].freeze
@@ -16,17 +16,7 @@ def initialize(key, params = nil, options = {})
1616
# For backwards compatibility when kid was a String
1717
params = { kid: params } if params.is_a?(String)
1818

19-
key_params = case key
20-
when JWT::JWK::RSA
21-
key.export(include_private: true)
22-
when OpenSSL::PKey::RSA # Accept OpenSSL key as input
23-
@keypair = key # Preserve the object to avoid recreation
24-
parse_rsa_key(key)
25-
when Hash
26-
key.transform_keys(&:to_sym)
27-
else
28-
raise ArgumentError, 'key must be of type OpenSSL::PKey::RSA or Hash with key parameters'
29-
end
19+
key_params = extract_key_params(key)
3020

3121
params = params.transform_keys(&:to_sym)
3222
check_jwk(key_params, params)
@@ -72,6 +62,20 @@ def []=(key, value)
7262

7363
private
7464

65+
def extract_key_params(key)
66+
case key
67+
when JWT::JWK::RSA
68+
key.export(include_private: true)
69+
when OpenSSL::PKey::RSA # Accept OpenSSL key as input
70+
@keypair = key # Preserve the object to avoid recreation
71+
parse_rsa_key(key)
72+
when Hash
73+
key.transform_keys(&:to_sym)
74+
else
75+
raise ArgumentError, 'key must be of type OpenSSL::PKey::RSA or Hash with key parameters'
76+
end
77+
end
78+
7579
def check_jwk(keypair, params)
7680
raise ArgumentError, 'cannot overwrite cryptographic key attributes' unless (RSA_KEY_ELEMENTS & params.keys).empty?
7781
raise JWT::JWKError, "Incorrect 'kty' value: #{keypair[:kty]}, expected #{KTY}" unless keypair[:kty] == KTY

lib/jwt/jwk/set.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Set
1010

1111
attr_reader :keys
1212

13-
def initialize(jwks = nil, options = {})
13+
def initialize(jwks = nil, options = {}) # rubocop:disable Metrics/CyclomaticComplexity
1414
jwks ||= {}
1515

1616
@keys = case jwks

spec/integration/readme_examples_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,17 @@
278278
# ---------- ENCODE ----------
279279
optional_parameters = { kid: 'my-kid', use: 'sig', alg: 'RS512' }
280280
jwk = JWT::JWK.new(OpenSSL::PKey::RSA.new(2048), optional_parameters)
281-
281+
282282
# Encoding
283283
payload = { data: 'data' }
284284
token = JWT.encode(payload, jwk.keypair, jwk[:alg], kid: jwk[:kid])
285-
285+
286286
# JSON Web Key Set for advertising your signing keys
287287
jwks_hash = JWT::JWK::Set.new(jwk).export
288288

289289
# ---------- DECODE ----------
290290
jwks = JWT::JWK::Set.new(jwks_hash)
291-
jwks.filter! {|key| key[:use] == 'sig' } # Signing keys only!
291+
jwks.filter! { |key| key[:use] == 'sig' } # Signing keys only!
292292
algorithms = jwks.map { |key| key[:alg] }.compact.uniq
293293
JWT.decode(token, nil, true, algorithms: algorithms, jwks: jwks)
294294
end
@@ -369,7 +369,7 @@
369369
jwks
370370
end
371371
end
372-
372+
373373
begin
374374
JWT.decode(token, nil, true, { algorithms: ['RS512'], jwks: jwks_loader })
375375
rescue JWT::JWKError

0 commit comments

Comments
 (0)