-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth_test.go
77 lines (71 loc) · 1.78 KB
/
oauth_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
package xmcnbiclient
import (
"fmt"
"testing"
"time"
)
const (
/*
{
"alg": "none"
}
*/
testTokenHeader string = "ewogICJhbGciOiAibm9uZSIKfQ"
/*
{
"iss": "foobar.example.com",
"sub": "XMC",
"jti": "foo123bar",
"roles": [
"foo",
"bar"
],
"iat": 1579535000,
"nbf": 1579535100,
"exp": 1579535200,
"longLived": false
}
*/
testTokenPayload string = "ewogICJpc3MiOiAiZm9vYmFyLmV4YW1wbGUuY29tIiwKICAic3ViIjogIlhNQyIsCiAgImp0aSI6ICJmb28xMjNiYXIiLAogICJyb2xlcyI6IFsKICAgICJmb28iLAogICAgImJhciIKICBdLAogICJpYXQiOiAxNTc5NTM1MDAwLAogICJuYmYiOiAxNTc5NTM1MTAwLAogICJleHAiOiAxNTc5NTM1MjAwLAogICJsb25nTGl2ZWQiOiBmYWxzZQp9"
/*
nonefoobarnone
*/
testTokenSignature string = "bm9uZWZvb2Jhcm5vbmU"
)
var (
testToken string = fmt.Sprintf("%s.%s.%s", testTokenHeader, testTokenPayload, testTokenSignature)
)
func TestDecode(t *testing.T) {
var o OAuthToken
o.RawToken = testToken
decodeErr := o.Decode()
if decodeErr != nil {
t.Errorf("Decode() could not decode test token: %s", decodeErr)
}
if o.Header.Algorithm != "none" {
t.Errorf("Decode() failed for header (alg != none)")
}
if o.Payload.JWTID != "foo123bar" {
t.Errorf("Decode() failed for payload (JWTID != foo123bar)")
}
if string(o.Signature) != "nonefoobarnone" {
t.Errorf("Decode() failed for signature")
}
}
func TestIsValid(t *testing.T) {
var o OAuthToken
if o.IsValid() {
t.Errorf("IsValid() returned true for an empty token")
}
}
func TestExpiresSoon(t *testing.T) {
var o OAuthToken
o.RawToken = testToken
decodeErr := o.Decode()
if decodeErr != nil {
t.Errorf("Decode() could not decode test token: %s", decodeErr)
}
if !o.ExpiresSoon(0) {
t.Errorf("ExpiresSoon() returned false for an expired token (now %s, exp %s)", time.Now(), o.Payload.ExpiresAt)
}
}