-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler_test.go
154 lines (123 loc) · 3.84 KB
/
handler_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package jwt
import (
"sync"
"testing"
"time"
)
const testKey = "testkey"
func TestEncodeAndSign(t *testing.T) {
alg := NewHandler[PublicClaims](NewHmacSha256([]byte(testKey)))
var claims PublicClaims
claims.Iss = "jwt testing"
claims.Exp = int64(1516239022)
token, err := alg.EncodeAndSign(claims)
if err != nil {
t.Fatal(err)
}
expectedToken := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJqd3QgdGVzdGluZyIsImV4cCI6MTUxNjIzOTAyMn0.GALxzaFGfbggUvAigJlp_tU4S-Oejui6GPHP2edEE8Y"
if token != expectedToken {
t.Fatalf("Result token differs:\n\texpected: %s\n\tis: %s",
expectedToken, token)
}
}
func TestDecode(t *testing.T) {
const invalidToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJqd3QgdGVzdGluZyIsImV4cCI6MTUxNjIzOT"
const token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJqd3QgdGVzdGluZyIsImV4cCI6MTUxNjIzOTAyMn0.GALxzaFGfbggUvAigJlp_tU4S-Oejui6GPHP2edEE8Y"
alg := NewHandler[PublicClaims](NewHmacSha256([]byte(testKey)))
_, err := alg.Decode(invalidToken)
if err != ErrInvalidTokenFormat {
t.Fatalf("Resulting error differs:\n\texpected: %s\n\tis: %s",
ErrInvalidTokenFormat.Error(), err.Error())
}
claims, err := alg.Decode(token)
if err != nil {
t.Fatal(err)
}
var expectedClaims PublicClaims
expectedClaims.Iss = "jwt testing"
expectedClaims.Exp = int64(1516239022)
if claims.Iss != expectedClaims.Iss {
t.Fatalf("Claims 'iss' value differs:\n\texpected: %s\n\tis: %s",
expectedClaims.Iss, claims.Iss)
}
if claims.Exp != expectedClaims.Exp {
t.Fatalf("Claims 'exp' value differs:\n\texpected: %d\n\tis: %d",
expectedClaims.Exp, claims.Exp)
}
}
func TestValidateSignature(t *testing.T) {
alg := NewHandler[PublicClaims](NewHmacSha256([]byte(testKey)))
var claims PublicClaims
claims.Iss = "jwt testing"
token, err := alg.EncodeAndSign(claims)
if err != nil {
t.Fatal(err)
}
err = alg.ValidateSignature(token)
if err != nil {
t.Fatalf("valid signature was falsely detected as invalid (%s)", err.Error())
}
alg = NewHandler[PublicClaims](NewHmacSha256([]byte("invalid key")))
err = alg.ValidateSignature(token)
if err != ErrInvalidSignature {
t.Fatal("invalid signature was not detected")
}
}
func TestDecodeAndValidate_Signature(t *testing.T) {
alg := NewHandler[PublicClaims](NewHmacSha256([]byte(testKey)))
var claims PublicClaims
claims.Iss = "jwt testing"
token, err := alg.EncodeAndSign(claims)
if err != nil {
t.Fatal(err)
}
_, err = alg.DecodeAndValidate(token)
if err != nil {
t.Fatalf("valid signature was falsely detected as invalid (%s)", err.Error())
}
alg = NewHandler[PublicClaims](NewHmacSha256([]byte("invalid key")))
_, err = alg.DecodeAndValidate(token)
if err != ErrInvalidSignature {
t.Fatal("invalid signature was not detected")
}
}
func TestDecodeAndValidate_ExpNbf(t *testing.T) {
alg := NewHandler[PublicClaims](NewHmacSha256([]byte(testKey)))
var claims PublicClaims
claims.Exp = time.Now().Add(2 * time.Second).Unix()
claims.Nbf = time.Now().Add(1 * time.Second).Unix()
token, err := alg.EncodeAndSign(claims)
if err != nil {
t.Fatal(err)
}
_, err = alg.DecodeAndValidate(token)
if err != ErrNotValidYet {
t.Fatal("invalid token was incorrectly validated against NBF")
}
time.Sleep(1 * time.Second)
_, err = alg.DecodeAndValidate(token)
if err != nil {
t.Fatalf("valid token was incorrectly validated with error: %s", err.Error())
}
time.Sleep(1 * time.Second)
_, err = alg.DecodeAndValidate(token)
if err != ErrTokenExpired {
t.Fatal("invalid token was incorrectly validated against EXP")
}
}
func TestTestEncodeAndSign_RaceCondition(t *testing.T) {
alg := NewHandler[PublicClaims](NewHmacSha256([]byte(testKey)))
n := 100
var wg sync.WaitGroup
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
alg.EncodeAndSign(PublicClaims{
Iss: "test",
Sub: "test",
})
wg.Done()
}()
}
wg.Wait()
}