Skip to content

Commit 13dd333

Browse files
committed
Algorithm cleanup.
- Algos module renamed to JWA - Standard HMAC algorithms provided explicitly by OpenSSL (#550) - Prepare to remove support for the HS512256 algorithm provided by RbNaCl (#549)
1 parent 39aa57a commit 13dd333

24 files changed

+225
-360
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
- Updated rubocop to 1.56 [#573](https://github.com/jwt/ruby-jwt/pull/573) - [@anakinj](https://github.com/anakinj).
1010
- Run CI on Ruby 3.3 [#577](https://github.com/jwt/ruby-jwt/pull/577) - [@anakinj](https://github.com/anakinj).
11+
- Deprecation warning added for the HMAC algorithm HS512256 (HMAC-SHA-512 truncated to 256-bits) [#575](https://github.com/jwt/ruby-jwt/pull/575) ([@anakinj](https://github.com/anakinj)).
12+
- Stop using RbNaCl for standard HMAC algorithms [#575](https://github.com/jwt/ruby-jwt/pull/575) ([@anakinj](https://github.com/anakinj)).
1113
- Your contribution here
1214

1315
**Fixes and enhancements:**
@@ -19,6 +21,7 @@
1921
- Repair EC x/y coordinates when importing JWK [#585](https://github.com/jwt/ruby-jwt/pull/585) - [@julik](https://github.com/julik).
2022
- Explicit dependency to the base64 gem [#582](https://github.com/jwt/ruby-jwt/pull/582) - [@anakinj](https://github.com/anakinj).
2123
- Deprecation warning for decoding content not compliant with RFC 4648 [#582](https://github.com/jwt/ruby-jwt/pull/582) - [@anakinj](https://github.com/anakinj).
24+
- Algorithms moved under the `::JWT::JWA` module ([@anakinj](https://github.com/anakinj)).
2225
- Your contribution here
2326

2427
## [v2.7.1](https://github.com/jwt/ruby-jwt/tree/v2.8.0) (2023-06-09)

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ puts decoded_token
7272
### **HMAC**
7373

7474
* HS256 - HMAC using SHA-256 hash algorithm
75-
* HS512256 - HMAC using SHA-512-256 hash algorithm (only available with RbNaCl; see note below)
7675
* HS384 - HMAC using SHA-384 hash algorithm
7776
* HS512 - HMAC using SHA-512 hash algorithm
7877

@@ -95,12 +94,6 @@ decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
9594
puts decoded_token
9695
```
9796

98-
Note: If [RbNaCl](https://github.com/RubyCrypto/rbnacl) is loadable, ruby-jwt will use it for HMAC-SHA256, HMAC-SHA512-256, and HMAC-SHA512. RbNaCl prior to 6.0.0 only support a maximum key size of 32 bytes for these algorithms.
99-
100-
[RbNaCl](https://github.com/RubyCrypto/rbnacl) requires
101-
[libsodium](https://github.com/jedisct1/libsodium), it can be installed
102-
on MacOS with `brew install libsodium`.
103-
10497
### **RSA**
10598

10699
* RS256 - RSA using SHA-256 hash algorithm

lib/jwt/algos.rb

Lines changed: 0 additions & 66 deletions
This file was deleted.

lib/jwt/algos/eddsa.rb

Lines changed: 0 additions & 33 deletions
This file was deleted.

lib/jwt/algos/hmac_rbnacl.rb

Lines changed: 0 additions & 53 deletions
This file was deleted.

lib/jwt/algos/hmac_rbnacl_fixed.rb

Lines changed: 0 additions & 52 deletions
This file was deleted.

lib/jwt/decode.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,7 @@ def allowed_algorithms
9292
end
9393

9494
def resolve_allowed_algorithms
95-
algs = given_algorithms.map do |alg|
96-
if Algos.implementation?(alg)
97-
alg
98-
else
99-
Algos.create(alg)
100-
end
101-
end
95+
algs = given_algorithms.map { |alg| JWA.create(alg) }
10296

10397
sort_by_alg_header(algs)
10498
end

lib/jwt/encode.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require_relative 'algos'
3+
require_relative 'jwa'
44
require_relative 'claims_validator'
55

66
# JWT::Encode module
@@ -12,7 +12,7 @@ class Encode
1212
def initialize(options)
1313
@payload = options[:payload]
1414
@key = options[:key]
15-
@algorithm = resolve_algorithm(options[:algorithm])
15+
@algorithm = JWA.create(options[:algorithm])
1616
@headers = options[:headers].transform_keys(&:to_s)
1717
@headers[ALG_KEY] = @algorithm.alg
1818
end
@@ -24,12 +24,6 @@ def segments
2424

2525
private
2626

27-
def resolve_algorithm(algorithm)
28-
return algorithm if Algos.implementation?(algorithm)
29-
30-
Algos.create(algorithm)
31-
end
32-
3327
def encoded_header
3428
@encoded_header ||= encode_header
3529
end

lib/jwt/jwa.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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/hmac'
12+
require_relative 'jwa/eddsa'
13+
require_relative 'jwa/ecdsa'
14+
require_relative 'jwa/rsa'
15+
require_relative 'jwa/ps'
16+
require_relative 'jwa/none'
17+
require_relative 'jwa/unsupported'
18+
require_relative 'jwa/wrapper'
19+
20+
module JWT
21+
module JWA
22+
ALGOS = [Hmac, Ecdsa, Rsa, Eddsa, Ps, None, Unsupported].tap do |l|
23+
if ::JWT.rbnacl_6_or_greater?
24+
require_relative 'jwa/hmac_rbnacl'
25+
l << Algos::HmacRbNaCl
26+
elsif ::JWT.rbnacl?
27+
require_relative 'jwa/hmac_rbnacl_fixed'
28+
l << Algos::HmacRbNaClFixed
29+
end
30+
end.freeze
31+
32+
class << self
33+
def find(algorithm)
34+
indexed[algorithm&.downcase]
35+
end
36+
37+
def create(algorithm)
38+
return algorithm if JWA.implementation?(algorithm)
39+
40+
Wrapper.new(*find(algorithm))
41+
end
42+
43+
def implementation?(algorithm)
44+
(algorithm.respond_to?(:valid_alg?) && algorithm.respond_to?(:verify)) ||
45+
(algorithm.respond_to?(:alg) && algorithm.respond_to?(:sign))
46+
end
47+
48+
private
49+
50+
def indexed
51+
@indexed ||= begin
52+
fallback = [nil, Unsupported]
53+
ALGOS.each_with_object(Hash.new(fallback)) do |cls, hash|
54+
cls.const_get(:SUPPORTED).each do |alg|
55+
hash[alg.downcase] = [alg, cls]
56+
end
57+
end
58+
end
59+
end
60+
end
61+
end
62+
end

lib/jwt/algos/ecdsa.rb renamed to lib/jwt/jwa/ecdsa.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
module JWT
4-
module Algos
4+
module JWA
55
module Ecdsa
66
module_function
77

0 commit comments

Comments
 (0)