Skip to content

Commit d3094a8

Browse files
committed
Improve support for Unsecured JWT
Allows for symmetric support of Unsecured JWT. It now works as any other algorithm for both encoding and decoding. Fixes #323.
1 parent f4c7f15 commit d3094a8

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

lib/jwt/algos/none.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module JWT
2+
module Algos
3+
module None
4+
# Unsecured JWT
5+
module_function
6+
7+
SUPPORTED = %w[none].freeze
8+
9+
def sign(to_sign)
10+
raise EncodeError, "Signing key not supported for Unsecured JWT" if to_sign.key
11+
""
12+
end
13+
14+
def verify(to_verify)
15+
raise VerificationError, "Signing key not supported for Unsecured JWT" unless to_verify.public_key.nil?
16+
raise VerificationError, "Signature should be empty for Unsecured JWT" unless to_verify.signature == ""
17+
true
18+
end
19+
20+
end
21+
end
22+
end

lib/jwt/decode.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(jwt, key, verify, options, &keyfinder)
1313
@jwt = jwt
1414
@key = key
1515
@options = options
16-
@segments = jwt.split('.')
16+
@segments = jwt.split('.', -1)
1717
@verify = verify
1818
@signature = ''
1919
@keyfinder = keyfinder
@@ -66,7 +66,6 @@ def verify_claims
6666

6767
def validate_segment_count!
6868
return if segment_length == 3
69-
return if !@verify && segment_length == 2 # If no verifying required, the signature is not needed
7069

7170
raise(JWT::DecodeError, 'Not enough or too many segments')
7271
end

lib/jwt/encode.rb

-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ def encode_payload
5252
end
5353

5454
def encode_signature
55-
return '' if @algorithm == ALG_NONE
56-
5755
JWT::Base64.url_encode(JWT::Signature.sign(@algorithm, encoded_header_and_payload, @key))
5856
end
5957

lib/jwt/signature.rb

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require 'jwt/algos/ecdsa'
88
require 'jwt/algos/rsa'
99
require 'jwt/algos/ps'
10+
require 'jwt/algos/none'
1011
require 'jwt/algos/unsupported'
1112
begin
1213
require 'rbnacl'
@@ -25,6 +26,7 @@ module Signature
2526
Algos::Rsa,
2627
Algos::Eddsa,
2728
Algos::Ps,
29+
Algos::None,
2830
Algos::Unsupported
2931
].freeze
3032
ToSign = Struct.new(:algorithm, :msg, :key)

spec/jwt_spec.rb

+26
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,45 @@
4444

4545
context 'alg: NONE' do
4646
let(:alg) { 'none' }
47+
let(:sig) { 'kWOVtIOpWcG7JnyJG0qOkTDbOy636XrrQhMm_8JrRQ8' }
4748

4849
it 'should generate a valid token' do
4950
token = JWT.encode payload, nil, alg
5051

5152
expect(token).to eq data['NONE']
5253
end
5354

55+
it 'with key should raise JWT::EncodeError' do
56+
expect do
57+
JWT.encode payload, data[:secret], alg
58+
end.to raise_error JWT::EncodeError, "Signing key not supported for Unsecured JWT"
59+
end
60+
5461
it 'should decode a valid token' do
5562
jwt_payload, header = JWT.decode data['NONE'], nil, false
5663

5764
expect(header['alg']).to eq alg
5865
expect(jwt_payload).to eq payload
5966
end
67+
68+
it 'should decode and verify a valid token' do
69+
jwt_payload, header = JWT.decode data['NONE'], nil, true, algorithm: alg
70+
71+
expect(header['alg']).to eq alg
72+
expect(jwt_payload).to eq payload
73+
end
74+
75+
it 'with signature should raise JWT::VerificationError' do
76+
expect do
77+
JWT.decode data['NONE'] + sig, nil, true, algorithm: alg
78+
end.to raise_error JWT::VerificationError, "Signature should be empty for Unsecured JWT"
79+
end
80+
81+
it 'with key should raise JWT::VerificationError' do
82+
expect do
83+
JWT.decode data['NONE'], data[:secret], true, algorithm: alg
84+
end.to raise_error JWT::VerificationError, "Signing key not supported for Unsecured JWT"
85+
end
6086
end
6187

6288
context 'payload validation' do

0 commit comments

Comments
 (0)