-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_test.go
64 lines (52 loc) · 1.8 KB
/
custom_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
package jwt
import (
"testing"
)
func Test_NewHS256Custom(t *testing.T) {
claims := struct {
IssuedAt int64 `json:"iat"`
Subject string `json:"sub"`
}{
Subject: "1234567890",
IssuedAt: 1516239022,
}
expected := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjIsInN1YiI6IjEyMzQ1Njc4OTAifQ.J3p7EuXSUS_3nmNgw9ZK0jJgdSEq5VOOJ52psItXEwI"
key := []byte("your-256-bit-secret")
token, err := NewHS256Custom(key, claims)
if err != nil {
t.Error(err)
}
if string(token) != expected {
t.Fail()
}
}
func Test_ParseHS256Custom(t *testing.T) {
token := []byte("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjIsInN1YiI6IjEyMzQ1Njc4OTAifQ.J3p7EuXSUS_3nmNgw9ZK0jJgdSEq5VOOJ52psItXEwI")
malformedTokens := []string{
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjIsInN1YiI6IjEyMzQ1Njc4OTAifQ.J3p7EuXSUS_3nmNgw9ZK0jJgdSEq5VOOJ52psItXEw",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjIsInN1YiI6IjEyMzQ1Njc4OTAif.J3p7EuXSUS_3nmNgw9ZK0jJgdSEq5VOOJ52psItXEwI",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ.eyJpYXQiOjE1MTYyMzkwMjIsInN1YiI6IjEyMzQ1Njc4OTAifQ.J3p7EuXSUS_3nmNgw9ZK0jJgdSEq5VOOJ52psItXEwI",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjIsInN1YiI6IjEyMzQ1Njc4OTAifQ",
}
key := []byte("your-256-bit-secret")
claims := &struct {
IssuedAt int64 `json:"iat"`
Subject string `json:"sub"`
}{}
err := ParseHS256Custom(key, token, claims, func(v interface{}) bool {
return true
})
if err != nil || claims.Subject != "1234567890" || claims.IssuedAt != 1516239022 {
t.Log("wrong claims or failed to parse")
t.Fail()
}
for _, v := range malformedTokens {
err = ParseHS256Custom(key, []byte(v), claims, func(v interface{}) bool {
return true
})
if err == nil {
t.Log("malformed token didn't give any errors")
t.Fail()
}
}
}