jwt 2.3.0 → 2.10.1

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.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHORS +60 -53
  3. data/CHANGELOG.md +194 -0
  4. data/CODE_OF_CONDUCT.md +84 -0
  5. data/CONTRIBUTING.md +99 -0
  6. data/README.md +360 -106
  7. data/lib/jwt/base64.rb +19 -2
  8. data/lib/jwt/claims/audience.rb +30 -0
  9. data/lib/jwt/claims/crit.rb +35 -0
  10. data/lib/jwt/claims/decode_verifier.rb +40 -0
  11. data/lib/jwt/claims/expiration.rb +32 -0
  12. data/lib/jwt/claims/issued_at.rb +22 -0
  13. data/lib/jwt/claims/issuer.rb +34 -0
  14. data/lib/jwt/claims/jwt_id.rb +35 -0
  15. data/lib/jwt/claims/not_before.rb +32 -0
  16. data/lib/jwt/claims/numeric.rb +77 -0
  17. data/lib/jwt/claims/required.rb +33 -0
  18. data/lib/jwt/claims/subject.rb +30 -0
  19. data/lib/jwt/claims/verification_methods.rb +20 -0
  20. data/lib/jwt/claims/verifier.rb +61 -0
  21. data/lib/jwt/claims.rb +74 -0
  22. data/lib/jwt/claims_validator.rb +7 -24
  23. data/lib/jwt/configuration/container.rb +52 -0
  24. data/lib/jwt/configuration/decode_configuration.rb +70 -0
  25. data/lib/jwt/configuration/jwk_configuration.rb +28 -0
  26. data/lib/jwt/configuration.rb +23 -0
  27. data/lib/jwt/decode.rb +70 -61
  28. data/lib/jwt/deprecations.rb +49 -0
  29. data/lib/jwt/encode.rb +18 -57
  30. data/lib/jwt/encoded_token.rb +139 -0
  31. data/lib/jwt/error.rb +36 -0
  32. data/lib/jwt/json.rb +1 -1
  33. data/lib/jwt/jwa/compat.rb +32 -0
  34. data/lib/jwt/jwa/ecdsa.rb +90 -0
  35. data/lib/jwt/jwa/eddsa.rb +35 -0
  36. data/lib/jwt/jwa/hmac.rb +82 -0
  37. data/lib/jwt/jwa/hmac_rbnacl.rb +50 -0
  38. data/lib/jwt/jwa/hmac_rbnacl_fixed.rb +47 -0
  39. data/lib/jwt/jwa/none.rb +24 -0
  40. data/lib/jwt/jwa/ps.rb +35 -0
  41. data/lib/jwt/jwa/rsa.rb +35 -0
  42. data/lib/jwt/jwa/signing_algorithm.rb +63 -0
  43. data/lib/jwt/jwa/unsupported.rb +20 -0
  44. data/lib/jwt/jwa/wrapper.rb +44 -0
  45. data/lib/jwt/jwa.rb +58 -0
  46. data/lib/jwt/jwk/ec.rb +163 -63
  47. data/lib/jwt/jwk/hmac.rb +68 -24
  48. data/lib/jwt/jwk/key_base.rb +46 -6
  49. data/lib/jwt/jwk/key_finder.rb +20 -35
  50. data/lib/jwt/jwk/kid_as_key_digest.rb +16 -0
  51. data/lib/jwt/jwk/okp_rbnacl.rb +109 -0
  52. data/lib/jwt/jwk/rsa.rb +141 -54
  53. data/lib/jwt/jwk/set.rb +82 -0
  54. data/lib/jwt/jwk/thumbprint.rb +26 -0
  55. data/lib/jwt/jwk.rb +16 -11
  56. data/lib/jwt/token.rb +112 -0
  57. data/lib/jwt/verify.rb +16 -81
  58. data/lib/jwt/version.rb +53 -11
  59. data/lib/jwt/x5c_key_finder.rb +52 -0
  60. data/lib/jwt.rb +28 -4
  61. data/ruby-jwt.gemspec +15 -5
  62. metadata +75 -28
  63. data/.github/workflows/test.yml +0 -74
  64. data/.gitignore +0 -11
  65. data/.rspec +0 -2
  66. data/.rubocop.yml +0 -97
  67. data/.rubocop_todo.yml +0 -185
  68. data/.sourcelevel.yml +0 -18
  69. data/Appraisals +0 -10
  70. data/Gemfile +0 -5
  71. data/Rakefile +0 -14
  72. data/lib/jwt/algos/ecdsa.rb +0 -35
  73. data/lib/jwt/algos/eddsa.rb +0 -30
  74. data/lib/jwt/algos/hmac.rb +0 -34
  75. data/lib/jwt/algos/none.rb +0 -15
  76. data/lib/jwt/algos/ps.rb +0 -43
  77. data/lib/jwt/algos/rsa.rb +0 -19
  78. data/lib/jwt/algos/unsupported.rb +0 -17
  79. data/lib/jwt/algos.rb +0 -44
  80. data/lib/jwt/default_options.rb +0 -16
  81. data/lib/jwt/security_utils.rb +0 -57
  82. data/lib/jwt/signature.rb +0 -39
data/.rubocop_todo.yml DELETED
@@ -1,185 +0,0 @@
1
- # This configuration was generated by
2
- # `rubocop --auto-gen-config`
3
- # on 2020-12-21 23:11:43 +0200 using RuboCop version 0.52.1.
4
- # The point is for the user to remove these configuration records
5
- # one by one as the offenses are removed from the code base.
6
- # Note that changes in the inspected code, or installation of new
7
- # versions of RuboCop, may require this file to be generated again.
8
-
9
- # Offense count: 2
10
- # Cop supports --auto-correct.
11
- # Configuration parameters: Include, TreatCommentsAsGroupSeparators.
12
- # Include: **/*.gemspec
13
- Gemspec/OrderedDependencies:
14
- Exclude:
15
- - 'ruby-jwt.gemspec'
16
-
17
- # Offense count: 1
18
- # Cop supports --auto-correct.
19
- Layout/EmptyLines:
20
- Exclude:
21
- - 'spec/integration/readme_examples_spec.rb'
22
-
23
- # Offense count: 1
24
- # Cop supports --auto-correct.
25
- # Configuration parameters: EnforcedStyle.
26
- # SupportedStyles: empty_lines, no_empty_lines
27
- Layout/EmptyLinesAroundBlockBody:
28
- Exclude:
29
- - 'spec/jwt_spec.rb'
30
-
31
- # Offense count: 1
32
- # Cop supports --auto-correct.
33
- # Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
34
- Layout/ExtraSpacing:
35
- Exclude:
36
- - 'spec/jwk_spec.rb'
37
-
38
- # Offense count: 2
39
- # Cop supports --auto-correct.
40
- # Configuration parameters: EnforcedStyle.
41
- # SupportedStyles: normal, rails
42
- Layout/IndentationConsistency:
43
- Exclude:
44
- - 'spec/jwt_spec.rb'
45
-
46
- # Offense count: 1
47
- # Cop supports --auto-correct.
48
- # Configuration parameters: Width, IgnoredPatterns.
49
- Layout/IndentationWidth:
50
- Exclude:
51
- - 'spec/jwt_spec.rb'
52
-
53
- # Offense count: 3
54
- # Cop supports --auto-correct.
55
- Layout/SpaceAfterComma:
56
- Exclude:
57
- - 'spec/jwt_spec.rb'
58
-
59
- # Offense count: 2
60
- # Cop supports --auto-correct.
61
- # Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
62
- # SupportedStyles: space, no_space
63
- # SupportedStylesForEmptyBraces: space, no_space
64
- Layout/SpaceBeforeBlockBraces:
65
- Exclude:
66
- - 'spec/jwk/ec_spec.rb'
67
- - 'spec/jwt/verify_spec.rb'
68
-
69
- # Offense count: 1
70
- # Cop supports --auto-correct.
71
- # Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
72
- # SupportedStyles: space, no_space
73
- # SupportedStylesForEmptyBraces: space, no_space
74
- Layout/SpaceInsideBlockBraces:
75
- Exclude:
76
- - 'spec/jwt/verify_spec.rb'
77
-
78
- # Offense count: 1
79
- # Cop supports --auto-correct.
80
- # Configuration parameters: EnforcedStyle.
81
- # SupportedStyles: final_newline, final_blank_line
82
- Layout/TrailingBlankLines:
83
- Exclude:
84
- - 'bin/console.rb'
85
-
86
- # Offense count: 3
87
- # Cop supports --auto-correct.
88
- # Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments.
89
- Lint/UnusedBlockArgument:
90
- Exclude:
91
- - 'spec/jwk/decode_with_jwk_spec.rb'
92
- - 'spec/jwk/ec_spec.rb'
93
- - 'spec/jwt/verify_spec.rb'
94
-
95
- # Offense count: 2
96
- Metrics/CyclomaticComplexity:
97
- Max: 7
98
-
99
- # Offense count: 1
100
- Metrics/PerceivedComplexity:
101
- Max: 8
102
-
103
- # Offense count: 1
104
- # Cop supports --auto-correct.
105
- # Configuration parameters: MaxKeyValuePairs.
106
- Performance/RedundantMerge:
107
- Exclude:
108
- - 'spec/jwt_spec.rb'
109
-
110
- # Offense count: 1
111
- # Cop supports --auto-correct.
112
- Style/Encoding:
113
- Exclude:
114
- - 'lib/jwt/version.rb'
115
-
116
- # Offense count: 1
117
- # Cop supports --auto-correct.
118
- # Configuration parameters: InverseMethods, InverseBlocks.
119
- Style/InverseMethods:
120
- Exclude:
121
- - 'spec/jwk/ec_spec.rb'
122
-
123
- # Offense count: 2
124
- # Cop supports --auto-correct.
125
- Style/MethodCallWithoutArgsParentheses:
126
- Exclude:
127
- - 'spec/jwt_spec.rb'
128
-
129
- # Offense count: 2
130
- # Configuration parameters: EnforcedStyle.
131
- # SupportedStyles: module_function, extend_self
132
- Style/ModuleFunction:
133
- Exclude:
134
- - 'lib/jwt/algos.rb'
135
- - 'lib/jwt/signature.rb'
136
-
137
- # Offense count: 1
138
- # Cop supports --auto-correct.
139
- Style/MutableConstant:
140
- Exclude:
141
- - 'lib/jwt/version.rb'
142
-
143
- # Offense count: 1
144
- # Cop supports --auto-correct.
145
- # Configuration parameters: Strict.
146
- Style/NumericLiterals:
147
- MinDigits: 6
148
-
149
- # Offense count: 1
150
- # Cop supports --auto-correct.
151
- Style/ParallelAssignment:
152
- Exclude:
153
- - 'spec/integration/readme_examples_spec.rb'
154
-
155
- # Offense count: 11
156
- # Cop supports --auto-correct.
157
- # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
158
- # SupportedStyles: single_quotes, double_quotes
159
- Style/StringLiterals:
160
- Exclude:
161
- - 'bin/console.rb'
162
- - 'spec/jwk/ec_spec.rb'
163
- - 'spec/jwk/rsa_spec.rb'
164
- - 'spec/jwk_spec.rb'
165
- - 'spec/jwt_spec.rb'
166
-
167
- # Offense count: 1
168
- # Cop supports --auto-correct.
169
- # Configuration parameters: EnforcedStyleForMultiline.
170
- # SupportedStylesForMultiline: comma, consistent_comma, no_comma
171
- Style/TrailingCommaInArguments:
172
- Exclude:
173
- - 'spec/jwt_spec.rb'
174
-
175
- # Offense count: 1
176
- # Cop supports --auto-correct.
177
- Style/UnlessElse:
178
- Exclude:
179
- - 'spec/jwt_spec.rb'
180
-
181
- # Offense count: 162
182
- # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
183
- # URISchemes: http, https
184
- Metrics/LineLength:
185
- Max: 420
data/.sourcelevel.yml DELETED
@@ -1,18 +0,0 @@
1
- styleguide: excpt/linters
2
- engines:
3
- reek:
4
- enabled: true
5
- fixme:
6
- enabled: true
7
- rubocop:
8
- enabled: true
9
- channel: rubocop-0-52
10
- duplication:
11
- config:
12
- languages:
13
- - ruby
14
- enabled: true
15
- remark-lint:
16
- enabled: true
17
- exclude_paths:
18
- - spec
data/Appraisals DELETED
@@ -1,10 +0,0 @@
1
- appraise 'standalone' do
2
- end
3
-
4
- appraise 'openssl' do
5
- gem 'openssl', '~> 2.1'
6
- end
7
-
8
- appraise 'rbnacl' do
9
- gem 'rbnacl'
10
- end
data/Gemfile DELETED
@@ -1,5 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- gem 'rubocop', '~> 0.52.0' # Same as codeclimate default
data/Rakefile DELETED
@@ -1,14 +0,0 @@
1
- require 'bundler/setup'
2
- require 'bundler/gem_tasks'
3
-
4
- begin
5
- require 'rspec/core/rake_task'
6
- require 'rubocop/rake_task'
7
-
8
- RSpec::Core::RakeTask.new(:test)
9
- RuboCop::RakeTask.new(:rubocop)
10
-
11
- task default: %i[rubocop test]
12
- rescue LoadError
13
- puts 'RSpec rake tasks not available. Please run "bundle install" to install missing dependencies.'
14
- end
@@ -1,35 +0,0 @@
1
- module JWT
2
- module Algos
3
- module Ecdsa
4
- module_function
5
-
6
- SUPPORTED = %w[ES256 ES384 ES512].freeze
7
- NAMED_CURVES = {
8
- 'prime256v1' => 'ES256',
9
- 'secp384r1' => 'ES384',
10
- 'secp521r1' => 'ES512'
11
- }.freeze
12
-
13
- def sign(to_sign)
14
- algorithm, msg, key = to_sign.values
15
- key_algorithm = NAMED_CURVES[key.group.curve_name]
16
- if algorithm != key_algorithm
17
- raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key_algorithm} signing key was provided"
18
- end
19
-
20
- digest = OpenSSL::Digest.new(algorithm.sub('ES', 'sha'))
21
- SecurityUtils.asn1_to_raw(key.dsa_sign_asn1(digest.digest(msg)), key)
22
- end
23
-
24
- def verify(to_verify)
25
- algorithm, public_key, signing_input, signature = to_verify.values
26
- key_algorithm = NAMED_CURVES[public_key.group.curve_name]
27
- if algorithm != key_algorithm
28
- raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key_algorithm} verification key was provided"
29
- end
30
- digest = OpenSSL::Digest.new(algorithm.sub('ES', 'sha'))
31
- public_key.dsa_verify_asn1(digest.digest(signing_input), SecurityUtils.raw_to_asn1(signature, public_key))
32
- end
33
- end
34
- end
35
- end
@@ -1,30 +0,0 @@
1
- module JWT
2
- module Algos
3
- module Eddsa
4
- module_function
5
-
6
- SUPPORTED = %w[ED25519 EdDSA].freeze
7
-
8
- def sign(to_sign)
9
- algorithm, msg, key = to_sign.values
10
- if key.class != RbNaCl::Signatures::Ed25519::SigningKey
11
- raise EncodeError, "Key given is a #{key.class} but has to be an RbNaCl::Signatures::Ed25519::SigningKey"
12
- end
13
- unless SUPPORTED.map(&:downcase).map(&:to_sym).include?(algorithm.downcase.to_sym)
14
- raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key.primitive} signing key was provided"
15
- end
16
-
17
- key.sign(msg)
18
- end
19
-
20
- def verify(to_verify)
21
- algorithm, public_key, signing_input, signature = to_verify.values
22
- unless SUPPORTED.map(&:downcase).map(&:to_sym).include?(algorithm.downcase.to_sym)
23
- raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key.primitive} signing key was provided"
24
- end
25
- raise DecodeError, "key given is a #{public_key.class} but has to be a RbNaCl::Signatures::Ed25519::VerifyKey" if public_key.class != RbNaCl::Signatures::Ed25519::VerifyKey
26
- public_key.verify(signature, signing_input)
27
- end
28
- end
29
- end
30
- end
@@ -1,34 +0,0 @@
1
- module JWT
2
- module Algos
3
- module Hmac
4
- module_function
5
-
6
- SUPPORTED = %w[HS256 HS512256 HS384 HS512].freeze
7
-
8
- def sign(to_sign)
9
- algorithm, msg, key = to_sign.values
10
- key ||= ''
11
- authenticator, padded_key = SecurityUtils.rbnacl_fixup(algorithm, key)
12
- if authenticator && padded_key
13
- authenticator.auth(padded_key, msg.encode('binary'))
14
- else
15
- OpenSSL::HMAC.digest(OpenSSL::Digest.new(algorithm.sub('HS', 'sha')), key, msg)
16
- end
17
- end
18
-
19
- def verify(to_verify)
20
- algorithm, public_key, signing_input, signature = to_verify.values
21
- authenticator, padded_key = SecurityUtils.rbnacl_fixup(algorithm, public_key)
22
- if authenticator && padded_key
23
- begin
24
- authenticator.verify(padded_key, signature.encode('binary'), signing_input.encode('binary'))
25
- rescue RbNaCl::BadAuthenticatorError
26
- false
27
- end
28
- else
29
- SecurityUtils.secure_compare(signature, sign(JWT::Signature::ToSign.new(algorithm, signing_input, public_key)))
30
- end
31
- end
32
- end
33
- end
34
- end
@@ -1,15 +0,0 @@
1
- module JWT
2
- module Algos
3
- module None
4
- module_function
5
-
6
- SUPPORTED = %w[none].freeze
7
-
8
- def sign(*); end
9
-
10
- def verify(*)
11
- true
12
- end
13
- end
14
- end
15
- end
data/lib/jwt/algos/ps.rb DELETED
@@ -1,43 +0,0 @@
1
- module JWT
2
- module Algos
3
- module Ps
4
- # RSASSA-PSS signing algorithms
5
-
6
- module_function
7
-
8
- SUPPORTED = %w[PS256 PS384 PS512].freeze
9
-
10
- def sign(to_sign)
11
- require_openssl!
12
-
13
- algorithm, msg, key = to_sign.values
14
-
15
- key_class = key.class
16
-
17
- raise EncodeError, "The given key is a #{key_class}. It has to be an OpenSSL::PKey::RSA instance." if key_class == String
18
-
19
- translated_algorithm = algorithm.sub('PS', 'sha')
20
-
21
- key.sign_pss(translated_algorithm, msg, salt_length: :digest, mgf1_hash: translated_algorithm)
22
- end
23
-
24
- def verify(to_verify)
25
- require_openssl!
26
-
27
- SecurityUtils.verify_ps(to_verify.algorithm, to_verify.public_key, to_verify.signing_input, to_verify.signature)
28
- end
29
-
30
- def require_openssl!
31
- if Object.const_defined?('OpenSSL')
32
- major, minor = OpenSSL::VERSION.split('.').first(2)
33
-
34
- unless major.to_i >= 2 && minor.to_i >= 1
35
- raise JWT::RequiredDependencyError, "You currently have OpenSSL #{OpenSSL::VERSION}. PS support requires >= 2.1"
36
- end
37
- else
38
- raise JWT::RequiredDependencyError, 'PS signing requires OpenSSL +2.1'
39
- end
40
- end
41
- end
42
- end
43
- end
data/lib/jwt/algos/rsa.rb DELETED
@@ -1,19 +0,0 @@
1
- module JWT
2
- module Algos
3
- module Rsa
4
- module_function
5
-
6
- SUPPORTED = %w[RS256 RS384 RS512].freeze
7
-
8
- def sign(to_sign)
9
- algorithm, msg, key = to_sign.values
10
- raise EncodeError, "The given key is a #{key.class}. It has to be an OpenSSL::PKey::RSA instance." if key.class == String
11
- key.sign(OpenSSL::Digest.new(algorithm.sub('RS', 'sha')), msg)
12
- end
13
-
14
- def verify(to_verify)
15
- SecurityUtils.verify_rsa(to_verify.algorithm, to_verify.public_key, to_verify.signing_input, to_verify.signature)
16
- end
17
- end
18
- end
19
- end
@@ -1,17 +0,0 @@
1
- module JWT
2
- module Algos
3
- module Unsupported
4
- module_function
5
-
6
- SUPPORTED = [].freeze
7
-
8
- def sign(*)
9
- raise NotImplementedError, 'Unsupported signing method'
10
- end
11
-
12
- def verify(*)
13
- raise JWT::VerificationError, 'Algorithm not supported'
14
- end
15
- end
16
- end
17
- end
data/lib/jwt/algos.rb DELETED
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'jwt/algos/hmac'
4
- require 'jwt/algos/eddsa'
5
- require 'jwt/algos/ecdsa'
6
- require 'jwt/algos/rsa'
7
- require 'jwt/algos/ps'
8
- require 'jwt/algos/none'
9
- require 'jwt/algos/unsupported'
10
-
11
- # JWT::Signature module
12
- module JWT
13
- # Signature logic for JWT
14
- module Algos
15
- extend self
16
-
17
- ALGOS = [
18
- Algos::Hmac,
19
- Algos::Ecdsa,
20
- Algos::Rsa,
21
- Algos::Eddsa,
22
- Algos::Ps,
23
- Algos::None,
24
- Algos::Unsupported
25
- ].freeze
26
-
27
- def find(algorithm)
28
- indexed[algorithm && algorithm.downcase]
29
- end
30
-
31
- private
32
-
33
- def indexed
34
- @indexed ||= begin
35
- fallback = [Algos::Unsupported, nil]
36
- ALGOS.each_with_object(Hash.new(fallback)) do |alg, hash|
37
- alg.const_get(:SUPPORTED).each do |code|
38
- hash[code.downcase] = [alg, code]
39
- end
40
- end
41
- end
42
- end
43
- end
44
- end
@@ -1,16 +0,0 @@
1
- module JWT
2
- module DefaultOptions
3
- DEFAULT_OPTIONS = {
4
- verify_expiration: true,
5
- verify_not_before: true,
6
- verify_iss: false,
7
- verify_iat: false,
8
- verify_jti: false,
9
- verify_aud: false,
10
- verify_sub: false,
11
- leeway: 0,
12
- algorithms: ['HS256'],
13
- required_claims: []
14
- }.freeze
15
- end
16
- end
@@ -1,57 +0,0 @@
1
- module JWT
2
- # Collection of security methods
3
- #
4
- # @see: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/security_utils.rb
5
- module SecurityUtils
6
- module_function
7
-
8
- def secure_compare(left, right)
9
- left_bytesize = left.bytesize
10
-
11
- return false unless left_bytesize == right.bytesize
12
-
13
- unpacked_left = left.unpack "C#{left_bytesize}"
14
- result = 0
15
- right.each_byte { |byte| result |= byte ^ unpacked_left.shift }
16
- result.zero?
17
- end
18
-
19
- def verify_rsa(algorithm, public_key, signing_input, signature)
20
- public_key.verify(OpenSSL::Digest.new(algorithm.sub('RS', 'sha')), signature, signing_input)
21
- end
22
-
23
- def verify_ps(algorithm, public_key, signing_input, signature)
24
- formatted_algorithm = algorithm.sub('PS', 'sha')
25
-
26
- public_key.verify_pss(formatted_algorithm, signature, signing_input, salt_length: :auto, mgf1_hash: formatted_algorithm)
27
- end
28
-
29
- def asn1_to_raw(signature, public_key)
30
- byte_size = (public_key.group.degree + 7) / 8
31
- OpenSSL::ASN1.decode(signature).value.map { |value| value.value.to_s(2).rjust(byte_size, "\x00") }.join
32
- end
33
-
34
- def raw_to_asn1(signature, private_key)
35
- byte_size = (private_key.group.degree + 7) / 8
36
- sig_bytes = signature[0..(byte_size - 1)]
37
- sig_char = signature[byte_size..-1] || ''
38
- OpenSSL::ASN1::Sequence.new([sig_bytes, sig_char].map { |int| OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(int, 2)) }).to_der
39
- end
40
-
41
- def rbnacl_fixup(algorithm, key)
42
- algorithm = algorithm.sub('HS', 'SHA').to_sym
43
-
44
- return [] unless defined?(RbNaCl) && RbNaCl::HMAC.constants(false).include?(algorithm)
45
-
46
- authenticator = RbNaCl::HMAC.const_get(algorithm)
47
-
48
- # Fall back to OpenSSL for keys larger than 32 bytes.
49
- return [] if key.bytesize > authenticator.key_bytes
50
-
51
- [
52
- authenticator,
53
- key.bytes.fill(0, key.bytesize...authenticator.key_bytes).pack('C*')
54
- ]
55
- end
56
- end
57
- end
data/lib/jwt/signature.rb DELETED
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'jwt/security_utils'
4
- require 'openssl'
5
- require 'jwt/algos'
6
- begin
7
- require 'rbnacl'
8
- rescue LoadError
9
- raise if defined?(RbNaCl)
10
- end
11
-
12
- # JWT::Signature module
13
- module JWT
14
- # Signature logic for JWT
15
- module Signature
16
- extend self
17
- ToSign = Struct.new(:algorithm, :msg, :key)
18
- ToVerify = Struct.new(:algorithm, :public_key, :signing_input, :signature)
19
-
20
- def sign(algorithm, msg, key)
21
- algo, code = Algos.find(algorithm)
22
- algo.sign ToSign.new(code, msg, key)
23
- end
24
-
25
- def verify(algorithm, key, signing_input, signature)
26
- return true if algorithm.casecmp('none').zero?
27
-
28
- raise JWT::DecodeError, 'No verification key available' unless key
29
-
30
- algo, code = Algos.find(algorithm)
31
- verified = algo.verify(ToVerify.new(code, key, signing_input, signature))
32
- raise(JWT::VerificationError, 'Signature verification raised') unless verified
33
- rescue OpenSSL::PKey::PKeyError
34
- raise JWT::VerificationError, 'Signature verification raised'
35
- ensure
36
- OpenSSL.errors.clear
37
- end
38
- end
39
- end