This repository was archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathjwt_test.go
133 lines (108 loc) · 3.09 KB
/
jwt_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 jwt
import (
"encoding/json"
"fmt"
"strings"
"testing"
"time"
)
var secret = "this is the secret"
var algorithms = []Algorithm{
HmacSha256(secret),
HmacSha384(secret),
HmacSha512(secret),
}
func RunTest(t *testing.T, command func(Algorithm)) {
for _, algorithm := range algorithms {
command(algorithm)
}
}
func TestEncodeAndValidateToken(t *testing.T) {
RunTest(t, func(algorithm Algorithm) {
payload := NewClaim()
payload.SetTime("nbf", time.Now().Add(time.Duration(-1)*time.Hour))
payload.SetTime("exp", time.Now().Add(time.Duration(100)*time.Hour))
token, err := algorithm.Encode(payload)
if err != nil {
t.Fatal(err)
}
err = algorithm.Validate(token)
if err != nil {
t.Fatal(err)
}
})
}
func TestValidateToken(t *testing.T) {
RunTest(t, func(algorithm Algorithm) {
payload := NewClaim()
err := json.Unmarshal([]byte(`{"sub":"1234567890","name":"John Doe","admin":true}`), &payload)
if err != nil {
t.Fatal(err)
}
token, err := algorithm.Encode(payload)
if err != nil {
t.Fatal(err)
}
tokenComponents := strings.Split(token, ".")
invalidSignature := "cBab30RMHrHDcEfxjoYZgeFONFh7Hg"
invalidToken := tokenComponents[0] + "." + tokenComponents[1] + "." + invalidSignature
err = algorithm.Validate(invalidToken)
if err == nil {
t.Fatal(err)
}
})
}
func TestVerifyTokenExp(t *testing.T) {
RunTest(t, func(algorithm Algorithm) {
payload := NewClaim()
payload.Set("exp", fmt.Sprintf("%d", time.Now().Add(-1*time.Hour).Unix()))
err := json.Unmarshal([]byte(`{"sub":"1234567890","name":"John Doe","admin":true}`), &payload)
if err != nil {
t.Fatal(err)
}
token, err := algorithm.Encode(payload)
if err != nil {
t.Fatal(err)
}
err = algorithm.Validate(token)
if err == nil {
t.Fatal(err)
}
})
}
func TestVerifyTokenNbf(t *testing.T) {
RunTest(t, func(algorithm Algorithm) {
payload := NewClaim()
payload.SetTime("nbf", time.Now().Add(time.Duration(1)*time.Hour))
err := json.Unmarshal([]byte(`{"sub":"1234567890","name":"John Doe","admin":true}`), &payload)
if err != nil {
t.Fatal(err)
}
token, err := algorithm.Encode(payload)
if err != nil {
t.Fatal(err)
}
err = algorithm.Validate(token)
if err == nil {
t.Fatal(err)
}
})
}
func TestDecodeMalformedToken(t *testing.T) {
RunTest(t, func(algorithm Algorithm) {
bogusTokens := []string{"", "abc", "czwmS6hE.NZLElvuy"}
for _, bogusToken := range bogusTokens {
if _, err := algorithm.Decode(bogusToken); err == nil {
t.Fatalf("no error returned upon decoding malformed token '%s'", bogusToken)
}
}
})
}
func TestValidateExternalToken(t *testing.T) {
token := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6ImZmNzJkMWM5LTMzMTktNGIyOS04YjlhLWU1OThkNGJhNDRlZCJ9.eyJpc3MiOiJodHRwOi8vbG9jYWwuaG9zdC5jb20iLCJhdWQiOiJodHRwOi8vbG9jYWwuaG9zdC5jb20iLCJqdGkiOiJmZjcyZDFjOS0zMzE5LTRiMjktOGI5YS1lNTk4ZDRiYTQ0ZWQiLCJpYXQiOjE1MTkzMjc2NDYsIm5iZiI6MTUxOTMyNzY1MCwiZXhwIjoxNjQwMzkwNDAwfQ.ASo8eiekkwZ7on43S9n697x-SqmdehY680GetK_KqpI"
algorithm := HmacSha256("this-needs-a-test")
err := algorithm.Validate(token)
if err != nil {
t.Fatal(err)
}
}