-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt_test.go
88 lines (71 loc) · 1.54 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
package jwt
import (
"testing"
"time"
)
var issuer *Issuer
func newIssuer(d string) (*Issuer, error) {
dur, err := time.ParseDuration(d)
if err != nil {
return nil, err
}
return NewIssuer([]byte("SIGNING_KEY_HERE"), "iss", "aud", dur), nil
}
func init() {
var err error
issuer, err = newIssuer("1h")
if err != nil {
panic(err)
}
}
func TestIssuer(t *testing.T) {
token, err := issuer.Token("hash", 0, "identifier", "role")
if err != nil {
t.Error(err)
}
claims, err := issuer.Validate(token)
if err != nil {
t.Error(err)
}
if claims.Hash != "hash" {
t.Errorf("Unexpected hash: %v", claims.Hash)
}
if claims.Refreshed != 0 {
t.Errorf("Unexpected refresh: %v", claims.Refreshed)
}
}
func TestExpired(t *testing.T) {
var err error
issuer, err = newIssuer("1ms")
if err != nil {
t.Error(err)
}
token, err := issuer.Token("hash", 0, "identifier", "role")
if err != nil {
t.Error(err)
}
time.Sleep(1 * time.Second)
_, err = issuer.Validate(token)
if err == nil {
t.Fatalf("Validate should have failed due to expiration")
}
}
func TestCopy(t *testing.T) {
var err error
issuer, err = newIssuer("1ms")
if err != nil {
t.Error(err)
}
copy := issuer.Copy()
if copy.iss != issuer.iss {
t.Errorf("Expected issuers to match: %v, %v", copy.iss, issuer.iss)
}
aud := copy.WithAud("new")
if aud.aud == copy.aud {
t.Errorf("Expected aud to not match: %v, %v", aud.aud, copy.aud)
}
dur := copy.WithDur(10 * time.Second)
if dur.dur == copy.dur {
t.Errorf("Expected dur to not match: %v, %v", dur.dur, aud.dur)
}
}