Repository navigation
Expand file tree
/
Copy pathexpected_test.go
More file actions
162 lines (148 loc) · 4.89 KB
/
Copy pathexpected_test.go
File metadata and controls
162 lines (148 loc) · 4.89 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package jwt
import (
"errors"
"fmt"
"testing"
)
func TestExpected(t *testing.T) {
expected := Expected{
NotBefore: 2019,
IssuedAt: 1193,
Expiry: 2020,
ID: "my-jti",
Issuer: "my-iss",
Subject: "1194",
Audience: []string{"aud1", "aud2"},
}
err := fmt.Errorf("test err")
if got := expected.ValidateToken(nil, Claims{}, err); err != got {
t.Fatalf("expected to return the previous error but got: %v", got)
}
// Test all OK.
err = expected.ValidateToken(nil, Claims{
NotBefore: 2019,
IssuedAt: 1193,
Expiry: 2020,
ID: "my-jti",
Issuer: "my-iss",
Subject: "1194",
Audience: []string{"aud1", "aud2"},
}, nil)
if err != nil {
t.Fatalf("expected nil error but got: %v", err)
}
// Test failures one by one, should stop on the first error.
var getExpectedErr = func(field string) error {
return fmt.Errorf("%w: %s", ErrExpected, field)
}
expectedErr := getExpectedErr("nbf")
gotErr := expected.ValidateToken(nil, Claims{NotBefore: 1}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
expectedErr = getExpectedErr("iat")
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: 1}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
expectedErr = getExpectedErr("exp")
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: 1}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
expectedErr = getExpectedErr("jti")
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: expected.Expiry,
ID: "unmatched"}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
expectedErr = getExpectedErr("iss")
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: expected.Expiry,
ID: expected.ID,
Issuer: "unmatched"}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
expectedErr = getExpectedErr("sub")
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: expected.Expiry,
ID: expected.ID,
Issuer: expected.Issuer,
Subject: "unmatched"}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
// A token listing more audiences than we asked for is fine. RFC 7519 section 4.1.3
// asks a recipient to check that it is among the audiences, not that it is the only
// one. This used to fail on a length comparison, so an issuer that added a second
// audience broke every recipient at once.
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: expected.Expiry,
ID: expected.ID,
Issuer: expected.Issuer,
Subject: expected.Subject,
Audience: []string{"aud1", "aud2", "aud3"}}, nil)
if gotErr != nil {
t.Fatalf("a superset of the expected audiences must be accepted, got: %v", gotErr)
}
// Order does not matter either.
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: expected.Expiry,
ID: expected.ID,
Issuer: expected.Issuer,
Subject: expected.Subject,
Audience: []string{"aud2", "aud1"}}, nil)
if gotErr != nil {
t.Fatalf("audience order must not matter, got: %v", gotErr)
}
expectedErr = getExpectedErr(`aud ("aud2")`)
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: expected.Expiry,
ID: expected.ID,
Issuer: expected.Issuer,
Subject: expected.Subject,
Audience: []string{"aud1", "aud3"}}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
}