-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclaims_test.go
133 lines (116 loc) · 3.58 KB
/
claims_test.go
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
package pvx
import (
"encoding/json"
"errors"
"testing"
"time"
)
func TestRegisteredClaimsValidation(t *testing.T) {
// zero values are always ok
rc := &RegisteredClaims{}
if err := rc.Valid(); err != nil {
t.Errorf("zero initialized claims are not valid: %v", err)
}
// expiration problem
now := time.Now()
rc = &RegisteredClaims{Expiration: TimePtr(now.AddDate(0, -1, 0))}
if err := rc.Valid(); err == nil {
t.Errorf("claims should expire")
} else {
validationErr := &ValidationError{}
if !errors.As(err, &validationErr) {
t.Errorf("error is not a type of validation error")
}
if !validationErr.HasExpiredErr() {
t.Errorf("field should have expired error")
}
}
// NotBeforeProblem
rc = &RegisteredClaims{NotBefore: TimePtr(time.Now().Add(time.Minute * 60))}
if err := rc.Valid(); err == nil {
t.Errorf("claims should be invalid because time is less then nbf")
} else {
validationErr := &ValidationError{}
if !errors.As(err, &validationErr) {
t.Errorf("error is not a type of validation error")
}
if !validationErr.HasNotBeforeErr() {
t.Errorf("field should have nbf error")
}
}
rc = &RegisteredClaims{IssuedAt: TimePtr(time.Now().Add(time.Minute * 60))}
if err := rc.Valid(); err == nil {
t.Errorf("claims should be invalid because token issued at in future")
} else {
validationErr := &ValidationError{}
if !errors.As(err, &validationErr) {
t.Errorf("error is not a type of validation error")
}
if !validationErr.HasIssuedAtErr() {
t.Errorf("field should have issued at error")
}
}
// Multiple problems at once
now = time.Now()
rc = &RegisteredClaims{
IssuedAt: TimePtr(now.Add(time.Minute * 60)),
NotBefore: TimePtr(now.Add(time.Minute * 60)),
Expiration: TimePtr(now.AddDate(0, 0, -1))}
if err := rc.Valid(); err == nil {
t.Errorf("claims must be invalid")
} else {
validationErr := &ValidationError{}
if !errors.As(err, &validationErr) {
t.Errorf("error is not a type of validation error")
}
passed := validationErr.HasIssuedAtErr() &&
validationErr.HasNotBeforeErr() &&
validationErr.HasExpiredErr() &&
!validationErr.HasAudienceErr() &&
!validationErr.HasGenericValidationErr() &&
!validationErr.HasIssuerErr() &&
!validationErr.HasKeyIDErr() &&
!validationErr.HasSubjectErr() &&
!validationErr.HasKeyIDErr() &&
!validationErr.HasTokenIDErr()
if !passed {
t.Errorf("claims should have simultaneously 3 errors and don't have others")
}
}
}
func TestRegisteredClaimsMarshalJSON(t *testing.T) {
iat := time.Now()
exp := time.Now()
c := RegisteredClaims{IssuedAt: &iat, Expiration: &exp, NotBefore: &time.Time{}}
b, err := json.Marshal(c)
if err != nil {
t.Errorf("problem while trying to marshal registered claims: %v", err)
}
cc := RegisteredClaims{}
if err := json.Unmarshal(b, &cc); err != nil {
t.Errorf("problem while trying to unmarshal registered claims: %v", err)
}
if !cc.NotBefore.IsZero() {
t.Errorf("not before must be zero")
}
if cc.Expiration.IsZero() || cc.IssuedAt.IsZero() {
t.Errorf("exp and iat must not be zero")
}
passed := c.IssuedAt.Equal(*cc.IssuedAt) && c.Expiration.Equal(*cc.Expiration) && c.NotBefore.Equal(*cc.NotBefore)
if !passed {
t.Errorf("serialized and deserialized values are not equal")
}
nbf := time.Now()
c = RegisteredClaims{NotBefore: &nbf}
b, err = json.Marshal(c)
if err != nil {
t.Errorf("serialization problem: %v", err)
}
cc = RegisteredClaims{}
if err := json.Unmarshal(b, &cc); err != nil {
t.Errorf("deserialization problem: %v", err)
}
if !c.NotBefore.Equal(*cc.NotBefore) {
t.Errorf("nbf are not equal")
}
}