Skip to content

Commit

Permalink
Some changes based on a bots comments
Browse files Browse the repository at this point in the history
  • Loading branch information
anakinj committed Dec 1, 2018
1 parent c589499 commit 0080ffb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
17 changes: 9 additions & 8 deletions lib/jwt/decode.rb
Expand Up @@ -66,23 +66,24 @@ def find_key(&keyfinder)
end

def find_from_jwk(jwks)
raise JWT::DecodeError, 'No key id (kid) found from token headers' unless header['kid']
kid = header['kid']
raise JWT::DecodeError, 'No key id (kid) found from token headers' unless kid

lazy = jwks.respond_to?(:call)
keys = if lazy
jwks.call({})
else
jwks
end
jwks.call({})
else
jwks
end

jwk = keys[:keys].find { |key| key[:kid] == header['kid'] }
jwk = keys[:keys].find { |key| key[:kid] == kid }

if lazy && !jwk
keys = jwks.call(invalidate: true)
jwk = keys[:keys].find { |key| key[:kid] == header['kid'] }
jwk = keys[:keys].find { |key| key[:kid] == kid }
end

raise JWT::DecodeError, "Could not find public key for kid #{header['kid']}" unless jwk
raise JWT::DecodeError, "Could not find public key for kid #{kid}" unless jwk

JWT::JWK.import(jwk).keypair
end
Expand Down
18 changes: 7 additions & 11 deletions lib/jwt/jwk.rb
@@ -1,7 +1,11 @@
module JWT
class JWK
extend Forwardable

attr_reader :keypair

def_delegators :keypair, :private?, :public_key

BINARY = 2

def initialize(keypair)
Expand All @@ -14,14 +18,6 @@ def supported_key!(keypair)
raise JWT::JWKError, "Key of type #{keypair.class.name} not supported"
end

def private?
keypair.private?
end

def public_key
keypair.public_key
end

def kid
sequence = OpenSSL::ASN1::Sequence([OpenSSL::ASN1::Integer.new(public_key.n),
OpenSSL::ASN1::Integer.new(public_key.e)])
Expand All @@ -45,9 +41,9 @@ def import(jwk_data)
case jwk_data[:kty]
when 'RSA'
imported_key = OpenSSL::PKey::RSA.new
n = OpenSSL::BN.new(from_base64(jwk_data[:n]), BINARY)
e = OpenSSL::BN.new(from_base64(jwk_data[:e]), BINARY)
imported_key.set_key(n, e, nil)
imported_key.set_key(OpenSSL::BN.new(from_base64(jwk_data[:n]), BINARY),
OpenSSL::BN.new(from_base64(jwk_data[:e]), BINARY),
nil)
self.new(imported_key)
else
raise JWT::JWKError, "Key type #{jwk_data[:kty]} not supported"
Expand Down

0 comments on commit 0080ffb

Please sign in to comment.