Skip to content

Commit

Permalink
improve JWT#sign signature auto-detection feature
Browse files Browse the repository at this point in the history
so that it can handle EC keys

NOTE: might be better to handle EC curves
  • Loading branch information
nov committed Feb 23, 2018
1 parent 67c6fbb commit 4708f7d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/json/jwt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ def sign(private_key_or_secret, algorithm = :autodetect)
# I'd like to make :RS256 default.
# However, by histrical reasons, :HS256 was default.
# This code is needed to keep legacy behavior.
algorithm = private_key_or_secret.is_a?(String) ? :HS256 : :RS256
algorithm = case private_key_or_secret
when String
:HS256
when OpenSSL::PKey::RSA
:RS256
when OpenSSL::PKey::EC
:ES256
else
raise UnexpectedAlgorithm.new('Signature algorithm auto-detection failed')
end
end
jws = JWS.new self
jws.kid ||= private_key_or_secret[:kid] if private_key_or_secret.is_a? JSON::JWK
Expand Down

0 comments on commit 4708f7d

Please sign in to comment.