22
33module 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
0 commit comments