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 840a1e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/jwt/jwk/key_abstract.rb
Expand Up @@ -3,7 +3,8 @@
module JWT
module JWK
class KeyAbstract
attr_reader :keypair, :kid
attr_reader :keypair
attr_accessor :kid

def initialize(keypair, kid = nil)
@keypair = keypair
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
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 840a1e8

Please sign in to comment.