Skip to content

Commit

Permalink
Use kid and keypair accessors from the abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
anakinj committed Oct 2, 2020
1 parent 7055538 commit a3843e3
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions lib/jwt/jwk/rsa.rb
Expand Up @@ -3,17 +3,14 @@
module JWT
module JWK
class RSA < KeyAbstract
attr_reader :keypair

BINARY = 2
KTY = 'RSA'.freeze
RSA_KEY_ELEMENTS = %i[n e d p q dp dq qi].freeze

def initialize(keypair, kid = nil)
raise ArgumentError, 'keypair must be of type OpenSSL::PKey::RSA' unless keypair.is_a?(OpenSSL::PKey::RSA)

@keypair = keypair
@kid = kid
super(keypair, kid || self.class.generate_kid(keypair.public_key))
end

def private?
Expand All @@ -24,10 +21,6 @@ def public_key
keypair.public_key
end

def kid
@kid ||= generate_kid
end

def export(options = {})
exported_hash = {
kty: KTY,
Expand All @@ -54,12 +47,6 @@ def append_private_parts(the_hash)
)
end

def generate_kid
sequence = OpenSSL::ASN1::Sequence([OpenSSL::ASN1::Integer.new(public_key.n),
OpenSSL::ASN1::Integer.new(public_key.e)])
OpenSSL::Digest::SHA256.hexdigest(sequence.to_der)
end

def encode_open_ssl_bn(key_part)
::Base64.urlsafe_encode64(key_part.to_s(BINARY), padding: false)
end
Expand All @@ -73,6 +60,12 @@ def import(jwk_data)
self.new(rsa_pkey(pkey_params), kid)
end

def generate_kid(public_key)
sequence = OpenSSL::ASN1::Sequence([OpenSSL::ASN1::Integer.new(public_key.n),
OpenSSL::ASN1::Integer.new(public_key.e)])
OpenSSL::Digest::SHA256.hexdigest(sequence.to_der)
end

private

def jwk_attributes(jwk_data, *attributes)
Expand Down

0 comments on commit a3843e3

Please sign in to comment.