forked from jwt/ruby-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsigning_algorithm_spec.rb
More file actions
128 lines (103 loc) · 3.71 KB
/
Copy pathsigning_algorithm_spec.rb
File metadata and controls
128 lines (103 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# frozen_string_literal: true
RSpec.describe JWT::JWA::SigningAlgorithm do
let(:payload) { { 'user_id' => 'some@user.tld' } }
let(:custom_algorithm) do
Class.new do
include JWT::JWA::SigningAlgorithm
def initialize(signature: 'custom_signature', alg: 'custom')
@signature = signature
@alg = alg
end
def sign(*)
@signature
end
def verify(data:, signature:, verification_key:) # rubocop:disable Lint/UnusedMethodArgument
signature == @signature
end
end
end
let(:token) { JWT.encode(payload, 'secret', custom_algorithm.new) }
let(:expected_token) { 'eyJhbGciOiJjdXN0b20ifQ.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.Y3VzdG9tX3NpZ25hdHVyZQ' }
it 'can be used for encoding' do
expect(token).to eq(expected_token)
end
it 'can be used for decoding' do
expect(JWT.decode(token, 'secret', true, algorithm: custom_algorithm.new)).to eq([payload, { 'alg' => 'custom' }])
end
context 'when multiple custom algorithms are given for decoding' do
it 'tries until the first match' do
expect(JWT.decode(token, 'secret', true, algorithms: [custom_algorithm.new(signature: 'not_this'), custom_algorithm.new])).to eq([payload, { 'alg' => 'custom' }])
end
end
context 'when class has custom header method' do
before do
custom_algorithm.class_eval do
def header(*)
{ 'alg' => alg, 'foo' => 'bar' }
end
end
end
it 'uses the provided header' do
expect(JWT.decode(token, 'secret', true, algorithm: custom_algorithm.new)).to eq([payload, { 'alg' => 'custom', 'foo' => 'bar' }])
end
end
context 'when class is not utilizing the ::JWT::JWA::SigningAlgorithm module' do
let(:custom_algorithm) do
Class.new do
attr_reader :alg
def initialize(signature: 'custom_signature', alg: 'custom')
@signature = signature
@alg = alg
end
def header(*)
{ 'alg' => @alg, 'foo' => 'bar' }
end
def sign(*)
@signature
end
def verify(*)
true
end
end
end
it 'raises an error' do
expect { token }.to raise_error(ArgumentError, 'Custom algorithms are required to include JWT::JWA::SigningAlgorithm')
end
end
context 'when alg is not matching' do
it 'fails the validation process' do
expect { JWT.decode(token, 'secret', true, algorithms: custom_algorithm.new(alg: 'not_a_match')) }.to raise_error(JWT::IncorrectAlgorithm, 'Expected a different algorithm')
end
end
context 'when signature is not matching' do
it 'fails the validation process' do
expect { JWT.decode(token, 'secret', true, algorithms: custom_algorithm.new(signature: 'not_a_match')) }.to raise_error(JWT::VerificationError, 'Signature verification failed')
end
end
context 'when #sign method is missing' do
before do
custom_algorithm.instance_eval do
remove_method :sign
end
end
it 'raises an error on encoding' do
expect { token }.to raise_error(JWT::EncodeError, /missing the sign method/)
end
it 'allows decoding' do
expect(JWT.decode(expected_token, 'secret', true, algorithm: custom_algorithm.new)).to eq([payload, { 'alg' => 'custom' }])
end
end
context 'when #verify method is missing' do
before do
custom_algorithm.instance_eval do
remove_method :verify
end
end
it 'can be used for encoding' do
expect(token).to eq(expected_token)
end
it 'raises error on decoding' do
expect { JWT.decode(expected_token, 'secret', true, algorithm: custom_algorithm.new) }.to raise_error(JWT::VerificationKeyError, /missing the verify method/)
end
end
end