forked from jwt/ruby-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Algos module renamed to JWA - Standard HMAC algorithms provided explicitly by OpenSSL (jwt#550) - Prepare to remove support for the HS512256 algorithm provided by RbNaCl (jwt#549)
- Loading branch information
Showing
24 changed files
with
219 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'openssl' | ||
|
||
require_relative 'jwa/hmac' | ||
require_relative 'jwa/eddsa' | ||
require_relative 'jwa/ecdsa' | ||
require_relative 'jwa/rsa' | ||
require_relative 'jwa/ps' | ||
require_relative 'jwa/none' | ||
require_relative 'jwa/unsupported' | ||
require_relative 'jwa/wrapper' | ||
|
||
module JWT | ||
module JWA | ||
ALGOS = [Hmac, Ecdsa, Rsa, Eddsa, Ps, None, Unsupported].tap do |l| | ||
if ::JWT.rbnacl_6_or_greater? | ||
require_relative 'algos/hmac_rbnacl' | ||
l << Algos::HmacRbNaCl | ||
elsif ::JWT.rbnacl? | ||
require_relative 'algos/hmac_rbnacl_fixed' | ||
l << Algos::HmacRbNaClFixed | ||
end | ||
end.freeze | ||
|
||
class << self | ||
def find(algorithm) | ||
indexed[algorithm&.downcase] | ||
end | ||
|
||
def create(algorithm) | ||
return algorithm if JWA.implementation?(algorithm) | ||
|
||
Wrapper.new(*find(algorithm)) | ||
end | ||
|
||
def implementation?(algorithm) | ||
(algorithm.respond_to?(:valid_alg?) && algorithm.respond_to?(:verify)) || | ||
(algorithm.respond_to?(:alg) && algorithm.respond_to?(:sign)) | ||
end | ||
|
||
private | ||
|
||
def indexed | ||
@indexed ||= begin | ||
fallback = [nil, Unsupported] | ||
ALGOS.each_with_object(Hash.new(fallback)) do |cls, hash| | ||
cls.const_get(:SUPPORTED).each do |alg| | ||
hash[alg.downcase] = [alg, cls] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module JWT | ||
module Algos | ||
module JWA | ||
module Ecdsa | ||
module_function | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module JWT | ||
module JWA | ||
module Eddsa | ||
SUPPORTED = %w[ED25519 EdDSA].freeze | ||
SUPPORTED_DOWNCASED = SUPPORTED.map(&:downcase).freeze | ||
|
||
class << self | ||
def sign(algorithm, msg, key) | ||
unless key.is_a?(RbNaCl::Signatures::Ed25519::SigningKey) | ||
raise EncodeError, "Key given is a #{key.class} but has to be an RbNaCl::Signatures::Ed25519::SigningKey" | ||
end | ||
|
||
validate_algorithm!(algorithm) | ||
|
||
key.sign(msg) | ||
end | ||
|
||
def verify(algorithm, public_key, signing_input, signature) | ||
unless public_key.is_a?(RbNaCl::Signatures::Ed25519::VerifyKey) | ||
raise DecodeError, "key given is a #{public_key.class} but has to be a RbNaCl::Signatures::Ed25519::VerifyKey" | ||
end | ||
|
||
validate_algorithm!(algorithm) | ||
|
||
public_key.verify(signature, signing_input) | ||
rescue RbNaCl::CryptoError | ||
false | ||
end | ||
|
||
private | ||
|
||
def validate_algorithm!(algorithm) | ||
return if SUPPORTED_DOWNCASED.include?(algorithm.downcase) | ||
|
||
raise IncorrectAlgorithm, "Algorithm #{algorithm} not supported. Supported algoritms are #{SUPPORTED.join(', ')}" | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.