jwt 2.7.1 → 2.10.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +157 -27
- data/README.md +207 -101
- data/lib/jwt/base64.rb +19 -2
- data/lib/jwt/claims/audience.rb +30 -0
- data/lib/jwt/claims/crit.rb +35 -0
- data/lib/jwt/claims/decode_verifier.rb +40 -0
- data/lib/jwt/claims/expiration.rb +32 -0
- data/lib/jwt/claims/issued_at.rb +22 -0
- data/lib/jwt/claims/issuer.rb +34 -0
- data/lib/jwt/claims/jwt_id.rb +35 -0
- data/lib/jwt/claims/not_before.rb +32 -0
- data/lib/jwt/claims/numeric.rb +77 -0
- data/lib/jwt/claims/required.rb +33 -0
- data/lib/jwt/claims/subject.rb +30 -0
- data/lib/jwt/claims/verification_methods.rb +20 -0
- data/lib/jwt/claims/verifier.rb +61 -0
- data/lib/jwt/claims.rb +74 -0
- data/lib/jwt/claims_validator.rb +6 -25
- data/lib/jwt/configuration/container.rb +34 -3
- data/lib/jwt/configuration/decode_configuration.rb +24 -0
- data/lib/jwt/configuration/jwk_configuration.rb +2 -1
- data/lib/jwt/configuration.rb +8 -0
- data/lib/jwt/decode.rb +34 -82
- data/lib/jwt/deprecations.rb +49 -0
- data/lib/jwt/encode.rb +18 -67
- data/lib/jwt/encoded_token.rb +139 -0
- data/lib/jwt/error.rb +35 -0
- data/lib/jwt/json.rb +1 -1
- data/lib/jwt/jwa/compat.rb +32 -0
- data/lib/jwt/{algos → jwa}/ecdsa.rb +40 -26
- data/lib/jwt/jwa/eddsa.rb +35 -0
- data/lib/jwt/{algos → jwa}/hmac.rb +31 -20
- data/lib/jwt/jwa/hmac_rbnacl.rb +50 -0
- data/lib/jwt/jwa/hmac_rbnacl_fixed.rb +47 -0
- data/lib/jwt/jwa/none.rb +24 -0
- data/lib/jwt/jwa/ps.rb +35 -0
- data/lib/jwt/jwa/rsa.rb +35 -0
- data/lib/jwt/jwa/signing_algorithm.rb +63 -0
- data/lib/jwt/jwa/unsupported.rb +20 -0
- data/lib/jwt/jwa/wrapper.rb +44 -0
- data/lib/jwt/jwa.rb +58 -0
- data/lib/jwt/jwk/ec.rb +44 -30
- data/lib/jwt/jwk/hmac.rb +2 -3
- data/lib/jwt/jwk/key_base.rb +4 -1
- data/lib/jwt/jwk/key_finder.rb +5 -4
- data/lib/jwt/jwk/kid_as_key_digest.rb +1 -0
- data/lib/jwt/jwk/okp_rbnacl.rb +3 -4
- data/lib/jwt/jwk/rsa.rb +2 -3
- data/lib/jwt/jwk/set.rb +3 -1
- data/lib/jwt/jwk.rb +2 -1
- data/lib/jwt/token.rb +112 -0
- data/lib/jwt/verify.rb +16 -89
- data/lib/jwt/version.rb +33 -11
- data/lib/jwt/x5c_key_finder.rb +2 -5
- data/lib/jwt.rb +24 -1
- data/ruby-jwt.gemspec +4 -0
- metadata +76 -18
- data/lib/jwt/algos/algo_wrapper.rb +0 -26
- data/lib/jwt/algos/eddsa.rb +0 -33
- data/lib/jwt/algos/hmac_rbnacl.rb +0 -53
- data/lib/jwt/algos/hmac_rbnacl_fixed.rb +0 -52
- data/lib/jwt/algos/none.rb +0 -19
- data/lib/jwt/algos/ps.rb +0 -41
- data/lib/jwt/algos/rsa.rb +0 -23
- data/lib/jwt/algos/unsupported.rb +0 -19
- data/lib/jwt/algos.rb +0 -66
|
@@ -1,9 +1,33 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module JWT
|
|
4
|
-
module
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
module JWA
|
|
5
|
+
# ECDSA signing algorithm
|
|
6
|
+
class Ecdsa
|
|
7
|
+
include JWT::JWA::SigningAlgorithm
|
|
8
|
+
|
|
9
|
+
def initialize(alg, digest)
|
|
10
|
+
@alg = alg
|
|
11
|
+
@digest = digest
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def sign(data:, signing_key:)
|
|
15
|
+
curve_definition = curve_by_name(signing_key.group.curve_name)
|
|
16
|
+
key_algorithm = curve_definition[:algorithm]
|
|
17
|
+
raise IncorrectAlgorithm, "payload algorithm is #{alg} but #{key_algorithm} signing key was provided" if alg != key_algorithm
|
|
18
|
+
|
|
19
|
+
asn1_to_raw(signing_key.dsa_sign_asn1(OpenSSL::Digest.new(digest).digest(data)), signing_key)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def verify(data:, signature:, verification_key:)
|
|
23
|
+
curve_definition = curve_by_name(verification_key.group.curve_name)
|
|
24
|
+
key_algorithm = curve_definition[:algorithm]
|
|
25
|
+
raise IncorrectAlgorithm, "payload algorithm is #{alg} but #{key_algorithm} verification key was provided" if alg != key_algorithm
|
|
26
|
+
|
|
27
|
+
verification_key.dsa_verify_asn1(OpenSSL::Digest.new(digest).digest(data), raw_to_asn1(signature, verification_key))
|
|
28
|
+
rescue OpenSSL::PKey::PKeyError
|
|
29
|
+
raise JWT::VerificationError, 'Signature verification raised'
|
|
30
|
+
end
|
|
7
31
|
|
|
8
32
|
NAMED_CURVES = {
|
|
9
33
|
'prime256v1' => {
|
|
@@ -28,38 +52,28 @@ module JWT
|
|
|
28
52
|
}
|
|
29
53
|
}.freeze
|
|
30
54
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
def sign(algorithm, msg, key)
|
|
34
|
-
curve_definition = curve_by_name(key.group.curve_name)
|
|
35
|
-
key_algorithm = curve_definition[:algorithm]
|
|
36
|
-
if algorithm != key_algorithm
|
|
37
|
-
raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key_algorithm} signing key was provided"
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
digest = OpenSSL::Digest.new(curve_definition[:digest])
|
|
41
|
-
asn1_to_raw(key.dsa_sign_asn1(digest.digest(msg)), key)
|
|
55
|
+
NAMED_CURVES.each_value do |v|
|
|
56
|
+
register_algorithm(new(v[:algorithm], v[:digest]))
|
|
42
57
|
end
|
|
43
58
|
|
|
44
|
-
def
|
|
45
|
-
|
|
46
|
-
key_algorithm = curve_definition[:algorithm]
|
|
47
|
-
if algorithm != key_algorithm
|
|
48
|
-
raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key_algorithm} verification key was provided"
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
digest = OpenSSL::Digest.new(curve_definition[:digest])
|
|
52
|
-
public_key.dsa_verify_asn1(digest.digest(signing_input), raw_to_asn1(signature, public_key))
|
|
53
|
-
rescue OpenSSL::PKey::PKeyError
|
|
54
|
-
raise JWT::VerificationError, 'Signature verification raised'
|
|
59
|
+
def self.from_algorithm(algorithm)
|
|
60
|
+
new(algorithm, algorithm.downcase.gsub('es', 'sha'))
|
|
55
61
|
end
|
|
56
62
|
|
|
57
|
-
def curve_by_name(name)
|
|
63
|
+
def self.curve_by_name(name)
|
|
58
64
|
NAMED_CURVES.fetch(name) do
|
|
59
65
|
raise UnsupportedEcdsaCurve, "The ECDSA curve '#{name}' is not supported"
|
|
60
66
|
end
|
|
61
67
|
end
|
|
62
68
|
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
attr_reader :digest
|
|
72
|
+
|
|
73
|
+
def curve_by_name(name)
|
|
74
|
+
self.class.curve_by_name(name)
|
|
75
|
+
end
|
|
76
|
+
|
|
63
77
|
def raw_to_asn1(signature, private_key)
|
|
64
78
|
byte_size = (private_key.group.degree + 7) / 8
|
|
65
79
|
sig_bytes = signature[0..(byte_size - 1)]
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
module JWA
|
|
5
|
+
# Implementation of the EdDSA family of algorithms
|
|
6
|
+
class Eddsa
|
|
7
|
+
include JWT::JWA::SigningAlgorithm
|
|
8
|
+
|
|
9
|
+
def initialize(alg)
|
|
10
|
+
@alg = alg
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def sign(data:, signing_key:)
|
|
14
|
+
raise_sign_error!("Key given is a #{signing_key.class} but has to be an RbNaCl::Signatures::Ed25519::SigningKey") unless signing_key.is_a?(RbNaCl::Signatures::Ed25519::SigningKey)
|
|
15
|
+
|
|
16
|
+
Deprecations.warning('Using the EdDSA algorithm is deprecated and will be removed in a future version of ruby-jwt. In the future the algorithm will be provided by the jwt-eddsa gem.')
|
|
17
|
+
|
|
18
|
+
signing_key.sign(data)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def verify(data:, signature:, verification_key:)
|
|
22
|
+
raise_verify_error!("key given is a #{verification_key.class} but has to be a RbNaCl::Signatures::Ed25519::VerifyKey") unless verification_key.is_a?(RbNaCl::Signatures::Ed25519::VerifyKey)
|
|
23
|
+
|
|
24
|
+
Deprecations.warning('Using the EdDSA algorithm is deprecated and will be removed in a future version of ruby-jwt. In the future the algorithm will be provided by the jwt-eddsa gem.')
|
|
25
|
+
|
|
26
|
+
verification_key.verify(signature, data)
|
|
27
|
+
rescue RbNaCl::CryptoError
|
|
28
|
+
false
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
register_algorithm(new('ED25519'))
|
|
32
|
+
register_algorithm(new('EdDSA'))
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -1,34 +1,43 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module JWT
|
|
4
|
-
module
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
module JWA
|
|
5
|
+
# Implementation of the HMAC family of algorithms
|
|
6
|
+
class Hmac
|
|
7
|
+
include JWT::JWA::SigningAlgorithm
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
'HS512' => OpenSSL::Digest::SHA512
|
|
12
|
-
}.freeze
|
|
9
|
+
def self.from_algorithm(algorithm)
|
|
10
|
+
new(algorithm, OpenSSL::Digest.new(algorithm.downcase.gsub('hs', 'sha')))
|
|
11
|
+
end
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
def initialize(alg, digest)
|
|
14
|
+
@alg = alg
|
|
15
|
+
@digest = digest
|
|
16
|
+
end
|
|
15
17
|
|
|
16
|
-
def sign(
|
|
17
|
-
|
|
18
|
+
def sign(data:, signing_key:)
|
|
19
|
+
ensure_valid_key!(signing_key)
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
OpenSSL::HMAC.digest(digest.new, signing_key, data)
|
|
22
|
+
end
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if key == '' && e.message == 'EVP_PKEY_new_mac_key: malloc failure'
|
|
24
|
-
raise JWT::DecodeError, 'OpenSSL 3.0 does not support nil or empty hmac_secret'
|
|
25
|
-
end
|
|
24
|
+
def verify(data:, signature:, verification_key:)
|
|
25
|
+
ensure_valid_key!(verification_key)
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
SecurityUtils.secure_compare(signature, OpenSSL::HMAC.digest(digest.new, verification_key, data))
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
register_algorithm(new('HS256', OpenSSL::Digest::SHA256))
|
|
31
|
+
register_algorithm(new('HS384', OpenSSL::Digest::SHA384))
|
|
32
|
+
register_algorithm(new('HS512', OpenSSL::Digest::SHA512))
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
attr_reader :digest
|
|
37
|
+
|
|
38
|
+
def ensure_valid_key!(key)
|
|
39
|
+
raise_verify_error!('HMAC key expected to be a String') unless key.is_a?(String)
|
|
40
|
+
raise_verify_error!('HMAC key cannot be empty') if key.empty?
|
|
32
41
|
end
|
|
33
42
|
|
|
34
43
|
# Copy of https://github.com/rails/rails/blob/v7.0.3.1/activesupport/lib/active_support/security_utils.rb
|
|
@@ -44,6 +53,7 @@ module JWT
|
|
|
44
53
|
OpenSSL.fixed_length_secure_compare(a, b)
|
|
45
54
|
end
|
|
46
55
|
else
|
|
56
|
+
# :nocov:
|
|
47
57
|
def fixed_length_secure_compare(a, b)
|
|
48
58
|
raise ArgumentError, "string length mismatch." unless a.bytesize == b.bytesize
|
|
49
59
|
|
|
@@ -53,6 +63,7 @@ module JWT
|
|
|
53
63
|
b.each_byte { |byte| res |= byte ^ l.shift }
|
|
54
64
|
res == 0
|
|
55
65
|
end
|
|
66
|
+
# :nocov:
|
|
56
67
|
end
|
|
57
68
|
module_function :fixed_length_secure_compare
|
|
58
69
|
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
module JWA
|
|
5
|
+
# Implementation of the HMAC family of algorithms (using RbNaCl)
|
|
6
|
+
class HmacRbNaCl
|
|
7
|
+
include JWT::JWA::SigningAlgorithm
|
|
8
|
+
|
|
9
|
+
def self.from_algorithm(algorithm)
|
|
10
|
+
new(algorithm, ::RbNaCl::HMAC.const_get(algorithm.upcase.gsub('HS', 'SHA')))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def initialize(alg, hmac)
|
|
14
|
+
@alg = alg
|
|
15
|
+
@hmac = hmac
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def sign(data:, signing_key:)
|
|
19
|
+
Deprecations.warning("The use of the algorithm #{alg} is deprecated and will be removed in the next major version of ruby-jwt")
|
|
20
|
+
hmac.auth(key_for_rbnacl(hmac, signing_key).encode('binary'), data.encode('binary'))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def verify(data:, signature:, verification_key:)
|
|
24
|
+
Deprecations.warning("The use of the algorithm #{alg} is deprecated and will be removed in the next major version of ruby-jwt")
|
|
25
|
+
hmac.verify(key_for_rbnacl(hmac, verification_key).encode('binary'), signature.encode('binary'), data.encode('binary'))
|
|
26
|
+
rescue ::RbNaCl::BadAuthenticatorError, ::RbNaCl::LengthError
|
|
27
|
+
false
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
register_algorithm(new('HS512256', ::RbNaCl::HMAC::SHA512256))
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
attr_reader :hmac
|
|
35
|
+
|
|
36
|
+
def key_for_rbnacl(hmac, key)
|
|
37
|
+
key ||= ''
|
|
38
|
+
raise JWT::DecodeError, 'HMAC key expected to be a String' unless key.is_a?(String)
|
|
39
|
+
|
|
40
|
+
return padded_empty_key(hmac.key_bytes) if key == ''
|
|
41
|
+
|
|
42
|
+
key
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def padded_empty_key(length)
|
|
46
|
+
Array.new(length, 0x0).pack('C*').encode('binary')
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
module JWA
|
|
5
|
+
# Implementation of the HMAC family of algorithms (using RbNaCl prior to a certain version)
|
|
6
|
+
class HmacRbNaClFixed
|
|
7
|
+
include JWT::JWA::SigningAlgorithm
|
|
8
|
+
|
|
9
|
+
def self.from_algorithm(algorithm)
|
|
10
|
+
new(algorithm, ::RbNaCl::HMAC.const_get(algorithm.upcase.gsub('HS', 'SHA')))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def initialize(alg, hmac)
|
|
14
|
+
@alg = alg
|
|
15
|
+
@hmac = hmac
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def sign(data:, signing_key:)
|
|
19
|
+
signing_key ||= ''
|
|
20
|
+
Deprecations.warning("The use of the algorithm #{alg} is deprecated and will be removed in the next major version of ruby-jwt")
|
|
21
|
+
raise JWT::DecodeError, 'HMAC key expected to be a String' unless signing_key.is_a?(String)
|
|
22
|
+
|
|
23
|
+
hmac.auth(padded_key_bytes(signing_key, hmac.key_bytes), data.encode('binary'))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def verify(data:, signature:, verification_key:)
|
|
27
|
+
verification_key ||= ''
|
|
28
|
+
Deprecations.warning("The use of the algorithm #{alg} is deprecated and will be removed in the next major version of ruby-jwt")
|
|
29
|
+
raise JWT::DecodeError, 'HMAC key expected to be a String' unless verification_key.is_a?(String)
|
|
30
|
+
|
|
31
|
+
hmac.verify(padded_key_bytes(verification_key, hmac.key_bytes), signature.encode('binary'), data.encode('binary'))
|
|
32
|
+
rescue ::RbNaCl::BadAuthenticatorError, ::RbNaCl::LengthError
|
|
33
|
+
false
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
register_algorithm(new('HS512256', ::RbNaCl::HMAC::SHA512256))
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
attr_reader :hmac
|
|
41
|
+
|
|
42
|
+
def padded_key_bytes(key, bytesize)
|
|
43
|
+
key.bytes.fill(0, key.bytesize...bytesize).pack('C*')
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
data/lib/jwt/jwa/none.rb
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
module JWA
|
|
5
|
+
# Implementation of the none algorithm
|
|
6
|
+
class None
|
|
7
|
+
include JWT::JWA::SigningAlgorithm
|
|
8
|
+
|
|
9
|
+
def initialize
|
|
10
|
+
@alg = 'none'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def sign(*)
|
|
14
|
+
''
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def verify(*)
|
|
18
|
+
true
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
register_algorithm(new)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
data/lib/jwt/jwa/ps.rb
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
module JWA
|
|
5
|
+
# Implementation of the RSASSA-PSS family of algorithms
|
|
6
|
+
class Ps
|
|
7
|
+
include JWT::JWA::SigningAlgorithm
|
|
8
|
+
|
|
9
|
+
def initialize(alg)
|
|
10
|
+
@alg = alg
|
|
11
|
+
@digest_algorithm = alg.sub('PS', 'sha')
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def sign(data:, signing_key:)
|
|
15
|
+
raise_sign_error!("The given key is a #{signing_key.class}. It has to be an OpenSSL::PKey::RSA instance.") unless signing_key.is_a?(::OpenSSL::PKey::RSA)
|
|
16
|
+
|
|
17
|
+
signing_key.sign_pss(digest_algorithm, data, salt_length: :digest, mgf1_hash: digest_algorithm)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def verify(data:, signature:, verification_key:)
|
|
21
|
+
verification_key.verify_pss(digest_algorithm, signature, data, salt_length: :auto, mgf1_hash: digest_algorithm)
|
|
22
|
+
rescue OpenSSL::PKey::PKeyError
|
|
23
|
+
raise JWT::VerificationError, 'Signature verification raised'
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
register_algorithm(new('PS256'))
|
|
27
|
+
register_algorithm(new('PS384'))
|
|
28
|
+
register_algorithm(new('PS512'))
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
attr_reader :digest_algorithm
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
data/lib/jwt/jwa/rsa.rb
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
module JWA
|
|
5
|
+
# Implementation of the RSA family of algorithms
|
|
6
|
+
class Rsa
|
|
7
|
+
include JWT::JWA::SigningAlgorithm
|
|
8
|
+
|
|
9
|
+
def initialize(alg)
|
|
10
|
+
@alg = alg
|
|
11
|
+
@digest = alg.sub('RS', 'SHA')
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def sign(data:, signing_key:)
|
|
15
|
+
raise_sign_error!("The given key is a #{signing_key.class}. It has to be an OpenSSL::PKey::RSA instance") unless signing_key.is_a?(OpenSSL::PKey::RSA)
|
|
16
|
+
|
|
17
|
+
signing_key.sign(OpenSSL::Digest.new(digest), data)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def verify(data:, signature:, verification_key:)
|
|
21
|
+
verification_key.verify(OpenSSL::Digest.new(digest), signature, data)
|
|
22
|
+
rescue OpenSSL::PKey::PKeyError
|
|
23
|
+
raise JWT::VerificationError, 'Signature verification raised'
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
register_algorithm(new('RS256'))
|
|
27
|
+
register_algorithm(new('RS384'))
|
|
28
|
+
register_algorithm(new('RS512'))
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
attr_reader :digest
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
# JSON Web Algorithms
|
|
5
|
+
module JWA
|
|
6
|
+
# Base functionality for signing algorithms
|
|
7
|
+
module SigningAlgorithm
|
|
8
|
+
# Class methods for the SigningAlgorithm module
|
|
9
|
+
module ClassMethods
|
|
10
|
+
def register_algorithm(algo)
|
|
11
|
+
::JWT::JWA.register_algorithm(algo)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.included(klass)
|
|
16
|
+
klass.extend(ClassMethods)
|
|
17
|
+
klass.include(JWT::JWA::Compat)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
attr_reader :alg
|
|
21
|
+
|
|
22
|
+
def valid_alg?(alg_to_check)
|
|
23
|
+
alg&.casecmp(alg_to_check)&.zero? == true
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def header(*)
|
|
27
|
+
{ 'alg' => alg }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def sign(*)
|
|
31
|
+
raise_sign_error!('Algorithm implementation is missing the sign method')
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def verify(*)
|
|
35
|
+
raise_verify_error!('Algorithm implementation is missing the verify method')
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def raise_verify_error!(message)
|
|
39
|
+
raise(DecodeError.new(message).tap { |e| e.set_backtrace(caller(1)) })
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def raise_sign_error!(message)
|
|
43
|
+
raise(EncodeError.new(message).tap { |e| e.set_backtrace(caller(1)) })
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
class << self
|
|
48
|
+
def register_algorithm(algo)
|
|
49
|
+
algorithms[algo.alg.to_s.downcase] = algo
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def find(algo)
|
|
53
|
+
algorithms.fetch(algo.to_s.downcase, Unsupported)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def algorithms
|
|
59
|
+
@algorithms ||= {}
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
module JWA
|
|
5
|
+
# Represents an unsupported algorithm
|
|
6
|
+
module Unsupported
|
|
7
|
+
class << self
|
|
8
|
+
include JWT::JWA::SigningAlgorithm
|
|
9
|
+
|
|
10
|
+
def sign(*)
|
|
11
|
+
raise_sign_error!('Unsupported signing method')
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def verify(*)
|
|
15
|
+
raise JWT::VerificationError, 'Algorithm not supported'
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
module JWA
|
|
5
|
+
# @api private
|
|
6
|
+
class Wrapper
|
|
7
|
+
include SigningAlgorithm
|
|
8
|
+
|
|
9
|
+
def initialize(algorithm)
|
|
10
|
+
@algorithm = algorithm
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def alg
|
|
14
|
+
return @algorithm.alg if @algorithm.respond_to?(:alg)
|
|
15
|
+
|
|
16
|
+
super
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def valid_alg?(alg_to_check)
|
|
20
|
+
return @algorithm.valid_alg?(alg_to_check) if @algorithm.respond_to?(:valid_alg?)
|
|
21
|
+
|
|
22
|
+
super
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def header(*args, **kwargs)
|
|
26
|
+
return @algorithm.header(*args, **kwargs) if @algorithm.respond_to?(:header)
|
|
27
|
+
|
|
28
|
+
super
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def sign(*args, **kwargs)
|
|
32
|
+
return @algorithm.sign(*args, **kwargs) if @algorithm.respond_to?(:sign)
|
|
33
|
+
|
|
34
|
+
super
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def verify(*args, **kwargs)
|
|
38
|
+
return @algorithm.verify(*args, **kwargs) if @algorithm.respond_to?(:verify)
|
|
39
|
+
|
|
40
|
+
super
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/lib/jwt/jwa.rb
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'openssl'
|
|
4
|
+
|
|
5
|
+
begin
|
|
6
|
+
require 'rbnacl'
|
|
7
|
+
rescue LoadError
|
|
8
|
+
raise if defined?(RbNaCl)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
require_relative 'jwa/compat'
|
|
12
|
+
require_relative 'jwa/signing_algorithm'
|
|
13
|
+
require_relative 'jwa/ecdsa'
|
|
14
|
+
require_relative 'jwa/hmac'
|
|
15
|
+
require_relative 'jwa/none'
|
|
16
|
+
require_relative 'jwa/ps'
|
|
17
|
+
require_relative 'jwa/rsa'
|
|
18
|
+
require_relative 'jwa/unsupported'
|
|
19
|
+
require_relative 'jwa/wrapper'
|
|
20
|
+
|
|
21
|
+
require_relative 'jwa/eddsa' if JWT.rbnacl?
|
|
22
|
+
|
|
23
|
+
if JWT.rbnacl_6_or_greater?
|
|
24
|
+
require_relative 'jwa/hmac_rbnacl'
|
|
25
|
+
elsif JWT.rbnacl?
|
|
26
|
+
require_relative 'jwa/hmac_rbnacl_fixed'
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
module JWT
|
|
30
|
+
# The JWA module contains all supported algorithms.
|
|
31
|
+
module JWA
|
|
32
|
+
class << self
|
|
33
|
+
# @api private
|
|
34
|
+
def resolve(algorithm)
|
|
35
|
+
return find(algorithm) if algorithm.is_a?(String) || algorithm.is_a?(Symbol)
|
|
36
|
+
|
|
37
|
+
unless algorithm.is_a?(SigningAlgorithm)
|
|
38
|
+
Deprecations.warning('Custom algorithms are required to include JWT::JWA::SigningAlgorithm. Custom algorithms that do not include this module may stop working in the next major version of ruby-jwt.')
|
|
39
|
+
return Wrapper.new(algorithm)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
algorithm
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# @api private
|
|
46
|
+
def resolve_and_sort(algorithms:, preferred_algorithm:)
|
|
47
|
+
algs = Array(algorithms).map { |alg| JWA.resolve(alg) }
|
|
48
|
+
algs.partition { |alg| alg.valid_alg?(preferred_algorithm) }.flatten
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# @deprecated The `::JWT::JWA.create` method is deprecated and will be removed in the next major version of ruby-jwt.
|
|
52
|
+
def create(algorithm)
|
|
53
|
+
Deprecations.warning('The ::JWT::JWA.create method is deprecated and will be removed in the next major version of ruby-jwt.')
|
|
54
|
+
resolve(algorithm)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|