Skip to content

Commit 7221480

Browse files
bellebaumanakinj
authored andcommitted
JWK: Initialization optimizations
1 parent 8fd469d commit 7221480

File tree

5 files changed

+41
-30
lines changed

5 files changed

+41
-30
lines changed

lib/jwt/jwk.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def import(jwk_data)
1414
end.import(jwk_data)
1515
end
1616

17-
def create_from(keypair, params = nil, options = {})
17+
def create_from(key, params = nil, options = {})
1818
mappings.fetch(keypair.class) do |klass|
1919
raise JWT::JWKError, "Cannot create JWK from a #{klass.name}"
20-
end.new(keypair, params, options)
20+
end.new(key, params, options)
2121
end
2222

2323
def classes

lib/jwt/jwk/ec.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,26 @@ class EC < KeyBase # rubocop:disable Metrics/ClassLength
1515
EC_PRIVATE_KEY_ELEMENTS = %i[d].freeze
1616
EC_KEY_ELEMENTS = (EC_PRIVATE_KEY_ELEMENTS + EC_PUBLIC_KEY_ELEMENTS).freeze
1717

18-
def initialize(keypair, params = nil, options = {})
18+
def initialize(key, params = nil, options = {})
1919
params ||= {}
2020

2121
# For backwards compatibility when kid was a String
2222
params = { kid: params } if params.is_a?(String)
2323

24-
# Accept OpenSSL key as input
25-
keypair = parse_ec_key(keypair) if keypair.is_a?(OpenSSL::PKey::EC)
26-
27-
raise ArgumentError, 'keypair must be of type OpenSSL::PKey::EC' unless keypair.is_a?(Hash)
24+
key_params = case key
25+
when OpenSSL::PKey::EC # Accept OpenSSL key as input
26+
@keypair = key # Preserve the object to avoid recreation
27+
parse_ec_key(key)
28+
when Hash
29+
key.transform_keys(&:to_sym)
30+
else
31+
raise ArgumentError, 'key must be of type OpenSSL::PKey::EC or Hash with key parameters'
32+
end
2833

29-
keypair = keypair.transform_keys(&:to_sym)
30-
params = params.transform_keys(&:to_sym)
31-
check_jwk(keypair, params)
34+
params = params.transform_keys(&:to_sym)
35+
check_jwk(key_params, params)
3236

33-
super(options, keypair.merge(params))
37+
super(options, key_params.merge(params))
3438
end
3539

3640
def keypair

lib/jwt/jwk/hmac.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@ class HMAC < KeyBase
99
HMAC_PRIVATE_KEY_ELEMENTS = %i[k].freeze
1010
HMAC_KEY_ELEMENTS = (HMAC_PRIVATE_KEY_ELEMENTS + HMAC_PUBLIC_KEY_ELEMENTS).freeze
1111

12-
def initialize(keypair, params = nil, options = {})
12+
def initialize(key, params = nil, options = {})
1313
params ||= {}
1414

1515
# For backwards compatibility when kid was a String
1616
params = { kid: params } if params.is_a?(String)
1717

18-
# Accept String key as input
19-
keypair = { kty: KTY, k: keypair } if keypair.is_a?(String)
20-
21-
raise ArgumentError, 'keypair must be of type String' unless keypair.is_a?(Hash)
18+
key_params = case key
19+
when String # Accept String key as input
20+
{ kty: KTY, k: key }
21+
when Hash
22+
key.transform_keys(&:to_sym)
23+
else
24+
raise ArgumentError, 'key must be of type String or Hash with key parameters'
25+
end
2226

23-
keypair = keypair.transform_keys(&:to_sym)
24-
params = params.transform_keys(&:to_sym)
25-
check_jwk(keypair, params)
27+
params = params.transform_keys(&:to_sym)
28+
check_jwk(key_params, params)
2629

27-
super(options, keypair.merge(params))
30+
super(options, key_params.merge(params))
2831
end
2932

3033
def keypair

lib/jwt/jwk/rsa.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,26 @@ class RSA < KeyBase # rubocop:disable Metrics/ClassLength
1010
RSA_PRIVATE_KEY_ELEMENTS = %i[d p q dp dq qi].freeze
1111
RSA_KEY_ELEMENTS = (RSA_PRIVATE_KEY_ELEMENTS + RSA_PUBLIC_KEY_ELEMENTS).freeze
1212

13-
def initialize(keypair, params = nil, options = {})
13+
def initialize(key, params = nil, options = {})
1414
params ||= {}
1515

1616
# For backwards compatibility when kid was a String
1717
params = { kid: params } if params.is_a?(String)
1818

19-
# Accept OpenSSL key as input
20-
keypair = parse_rsa_key(keypair) if keypair.is_a?(OpenSSL::PKey::RSA)
21-
22-
raise ArgumentError, 'keypair must be of type OpenSSL::PKey::RSA' unless keypair.is_a?(Hash)
19+
key_params = case key
20+
when OpenSSL::PKey::RSA # Accept OpenSSL key as input
21+
@keypair = key # Preserve the object to avoid recreation
22+
parse_rsa_key(key)
23+
when Hash
24+
key.transform_keys(&:to_sym)
25+
else
26+
raise ArgumentError, 'key must be of type OpenSSL::PKey::RSA or Hash with key parameters'
27+
end
2328

24-
keypair = keypair.transform_keys(&:to_sym)
25-
params = params.transform_keys(&:to_sym)
26-
check_jwk(keypair, params)
29+
params = params.transform_keys(&:to_sym)
30+
check_jwk(key_params, params)
2731

28-
super(options, keypair.merge(params))
32+
super(options, key_params.merge(params))
2933
end
3034

3135
def keypair

spec/jwk/rsa_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
context 'when unsupported keypair is given' do
4848
let(:keypair) { 'key' }
4949
it 'raises an error' do
50-
expect { subject }.to raise_error(ArgumentError, 'keypair must be of type OpenSSL::PKey::RSA')
50+
expect { subject }.to raise_error(ArgumentError)
5151
end
5252
end
5353

0 commit comments

Comments
 (0)