Skip to content

Commit

Permalink
Set generated kid via accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
anakinj committed Oct 2, 2020
1 parent 7e10b20 commit 7edbec0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/jwt/jwk/key_abstract.rb
Expand Up @@ -22,6 +22,10 @@ def export(_options = {})
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end

protected

attr_writer :kid

class << self
def import(_jwk_data)
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
Expand Down
15 changes: 8 additions & 7 deletions lib/jwt/jwk/rsa.rb
Expand Up @@ -9,7 +9,8 @@ class RSA < KeyAbstract

def initialize(keypair, kid = nil)
raise ArgumentError, 'keypair must be of type OpenSSL::PKey::RSA' unless keypair.is_a?(OpenSSL::PKey::RSA)
super(keypair, kid || self.class.generate_kid(keypair.public_key))
super(keypair, kid)
self.kid ||= generate_kid
end

def private?
Expand All @@ -35,6 +36,12 @@ def export(options = {})

private

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 append_private_parts(the_hash)
the_hash.merge(
d: encode_open_ssl_bn(keypair.d),
Expand All @@ -59,12 +66,6 @@ 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 7edbec0

Please sign in to comment.