-
Notifications
You must be signed in to change notification settings - Fork 89
/
extension_jwt_test.go
181 lines (155 loc) · 4.94 KB
/
extension_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package helix
import (
"net/http"
"strings"
"testing"
"time"
)
func TestValidateJwtParameters(t *testing.T) {
t.Parallel()
c := newMockClient(&Options{}, newMockHandler(http.StatusOK, "", nil))
err := c.validateExtensionOpts()
if err == nil {
t.Errorf("expected to get an error got nil")
}
if err != nil && !strings.Contains(err.Error(), "extension owner id is empty") {
t.Errorf("expected error extension owner id is empty, got err:%s", err)
}
c = newMockClient(&Options{
ExtensionOpts: ExtensionOptions{OwnerUserID: "100249558"},
}, newMockHandler(http.StatusOK, "", nil))
err = c.validateExtensionOpts()
if err == nil {
t.Errorf("expected to get an error got nil")
}
if err != nil && !strings.Contains(err.Error(), "extension secret is empty") {
t.Errorf("expected error extension secret is empty, got err:%s", err)
}
}
func TestCreateClaims(t *testing.T) {
t.Parallel()
userId := "100249558"
c := newMockClient(&Options{
ExtensionOpts: ExtensionOptions{
OwnerUserID: userId,
Secret: "12012311231",
},
}, newMockHandler(http.StatusOK, "", nil))
channelID := "1337"
params := &ExtensionCreateClaimsParams{
ChannelID: channelID,
PubSub: c.FormBroadcastSendPubSubPermissions(),
Expiration: 0,
}
claims, err := c.ExtensionCreateClaims(params)
if err != nil {
t.Errorf("unexpected error generating claims %s", err)
}
if claims.UserID != userId {
t.Errorf("claims userId doesn't match got %s expected %s", claims.UserID, userId)
}
if claims.ChannelID != channelID {
t.Errorf("claims broadcasterId doesn't match got %s expected %s", claims.ChannelID, channelID)
}
if claims.ExpiresAt < time.Now().Add(4*time.Minute).UnixNano() && claims.ExpiresAt > time.Now().Add(-2*time.Minute).UnixNano() {
t.Errorf("claims expiry less than 3 minutes")
}
expiration := time.Now().Add(10*time.Minute).UnixNano() / int64(time.Millisecond)
params.Expiration = expiration
claims, err = c.ExtensionCreateClaims(params)
if err != nil {
t.Errorf("unexpected error generating claims %s", err)
}
overTime := time.Now().Add(15 * time.Minute).Unix()
if claims.ExpiresAt < overTime {
t.Errorf("claims expiry does not confine to 10 minutes expiry")
}
}
func TestSignClaimsToJWT(t *testing.T) {
t.Parallel()
userId := "100249558"
c := newMockClient(&Options{
ExtensionOpts: ExtensionOptions{
OwnerUserID: userId,
Secret: "TYkWIXLIKljq0e4u9id6KvqOxa80uSKKPTreIT12ERk=",
},
}, newMockHandler(http.StatusOK, "", nil))
params := &ExtensionCreateClaimsParams{
ChannelID: "1337",
PubSub: c.FormBroadcastSendPubSubPermissions(),
Expiration: 0,
}
claims, err := c.ExtensionCreateClaims(params)
if err != nil {
t.Errorf("unexpected error generating claims %s", err)
}
jwt, err := c.ExtensionJWTSign(claims)
if err != nil {
t.Errorf("failed to sign claims %s", err)
}
if jwt == "" {
t.Errorf("JWT token is empty")
}
}
func TestVerifyJWT(t *testing.T) {
t.Parallel()
userId := "100249558"
c := newMockClient(&Options{
ExtensionOpts: ExtensionOptions{
OwnerUserID: userId,
Secret: "TYkWIXLIKljq0e4u9id6KvqOxa80uSKKPTreIT12ERk=",
},
}, newMockHandler(http.StatusOK, "", nil))
channelID := "1337"
params := &ExtensionCreateClaimsParams{
ChannelID: channelID,
PubSub: c.FormBroadcastSendPubSubPermissions(),
Expiration: 0,
}
claims, err := c.ExtensionCreateClaims(params)
if err != nil {
t.Errorf("unexpected error generating claims %s", err)
}
jwt, err := c.ExtensionJWTSign(claims)
if err != nil {
t.Errorf("failed to sign claims %s", err)
}
if jwt == "" {
t.Errorf("JWT token is empty")
}
claims, err = c.ExtensionJWTVerify("")
if err != nil && !strings.Contains(err.Error(), "JWT token string missing") {
t.Errorf("unexpected error verifying JWT err:%s", err)
}
claims, err = c.ExtensionJWTVerify("abcd")
if err != nil && !strings.Contains(err.Error(), "token contains an invalid number of segments") {
t.Errorf("unexpected error verifying JWT err:%s", err)
}
claims, err = c.ExtensionJWTVerify(jwt)
if err != nil && !strings.Contains(err.Error(), "JWT token string missing") {
t.Errorf("unexpected error verifying JWT err:%s", err)
}
if claims.ChannelID != channelID {
t.Errorf("found unexpected broadcaster in claims got:%s expected:%s", claims.ChannelID, channelID)
}
if claims.UserID != userId {
t.Errorf("found unexpected userId in claims got:%s expected:%s", claims.UserID, userId)
}
// generate expired claims to vefiry expiration behaviour
params.Expiration = time.Now().Add(-10 * time.Minute).Unix()
claims, err = c.ExtensionCreateClaims(params)
if err != nil {
t.Errorf("unexpected error generating claims %s", err)
}
jwt, err = c.ExtensionJWTSign(claims)
if err != nil {
t.Errorf("failed to sign claims %s", err)
}
if jwt == "" {
t.Errorf("JWT token is empty")
}
claims, err = c.ExtensionJWTVerify(jwt)
if err != nil && !strings.Contains(err.Error(), "token is expired by 10m") {
t.Errorf("unexpected error verifying JWT err:%s", err)
}
}