-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt_test.go
64 lines (61 loc) · 1.2 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
package gojwt
import (
"encoding/base64"
"encoding/json"
"strings"
"testing"
)
func TestCreateJWT(t *testing.T) {
sub := "12123"
plt := "web"
secret := "some secret"
jwt, err := Create(sub, plt, secret)
if err != nil {
t.Error(err)
}
explodedJwt := strings.Split(jwt, ".")
if len(explodedJwt) != 3 {
t.Error("Invalid jwt")
}
header, err := base64.RawStdEncoding.DecodeString(explodedJwt[0])
if err != nil {
t.Error(err)
}
var h Header
err = json.Unmarshal(header, &h)
if err != nil {
t.Error(err)
}
if h.Algorithm != "HS256" {
t.Error("invalid algo")
}
if h.Type != "jwt" {
t.Error("invalid token type")
}
var p Payload
payload, err := base64.RawStdEncoding.DecodeString(explodedJwt[1])
err = json.Unmarshal(payload, &p)
if p.Platform != plt {
t.Error("invalid plt")
}
if p.Subject != sub {
t.Error("invalid sub")
}
_, err = base64.RawStdEncoding.DecodeString(explodedJwt[2])
if err != nil {
t.Error(err)
}
}
func TestVerifySignature(t *testing.T) {
sub := "12123"
plt := "web"
secret := "some secret"
jwt, err := Create(sub, plt, secret)
if err != nil {
t.Error(err)
}
_, err = VerifySignature(jwt, secret)
if err != nil {
t.Errorf("signature failed")
}
}