@@ -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
0 commit comments