-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathcookies_test.go
More file actions
309 lines (289 loc) · 7.36 KB
/
Copy pathcookies_test.go
File metadata and controls
309 lines (289 loc) · 7.36 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package oauth2
import (
"context"
"fmt"
"log"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
gojwt "github.com/golang-jwt/jwt/v4"
)
type MockTokenizer struct {
Principal Principal
ValidErr error
Token Token
CreateErr error
ExtendErr error
}
const (
defaultInactivityDuration = 5 * time.Minute
)
func (m *MockTokenizer) ValidPrincipal(ctx context.Context, token Token, duration time.Duration) (Principal, error) {
return m.Principal, m.ValidErr
}
func (m *MockTokenizer) Create(ctx context.Context, p Principal) (Token, error) {
return m.Token, m.CreateErr
}
func (m *MockTokenizer) ExtendedPrincipal(ctx context.Context, principal Principal, extension time.Duration) (Principal, error) {
return principal, m.ExtendErr
}
func (m *MockTokenizer) GetClaims(tokenString string) (gojwt.MapClaims, error) {
return gojwt.MapClaims{}, nil
}
func TestCookieAuthorize(t *testing.T) {
var test = []struct {
Desc string
Value string
Expected string
Err error
CreateErr error
}{
{
Desc: "Unable to create token",
Err: ErrAuthentication,
CreateErr: ErrAuthentication,
},
{
Desc: "Cookie token extracted",
Value: "reallyimportant",
Expected: "reallyimportant",
Err: nil,
},
}
for _, test := range test {
cook := cookie{
Lifespan: 1 * time.Second,
Now: func() time.Time {
return time.Unix(0, 0)
},
Tokens: &MockTokenizer{
Token: Token(test.Value),
CreateErr: test.CreateErr,
},
}
principal := Principal{}
w := httptest.NewRecorder()
err := cook.Authorize(context.Background(), w, principal)
if err != test.Err {
t.Fatalf("Cookie extract error; expected %v actual %v", test.Err, err)
}
if test.Err != nil {
continue
}
cookies := w.HeaderMap["Set-Cookie"]
if len(cookies) == 0 {
t.Fatal("Expected some cookies but got zero")
}
log.Printf("%s", cookies[0])
if !strings.Contains(cookies[0], fmt.Sprintf("%s=%s", DefaultCookieName, test.Expected)) {
t.Errorf("Token extract error; expected %v actual %v", test.Expected, principal.Subject)
}
}
}
func TestCookieValidate(t *testing.T) {
var test = []struct {
Desc string
Name string
Value string
Lookup string
Expected string
Err error
ValidErr error
}{
{
Desc: "No cookie of this name",
Name: "Auth",
Value: "reallyimportant",
Lookup: "Doesntexist",
Expected: "",
Err: ErrAuthentication,
},
{
Desc: "Unable to create token",
Name: "Auth",
Lookup: "Auth",
Err: ErrAuthentication,
ValidErr: ErrAuthentication,
},
{
Desc: "Cookie token extracted",
Name: "Auth",
Value: "reallyimportant",
Lookup: "Auth",
Expected: "reallyimportant",
Err: nil,
},
}
for _, test := range test {
req, _ := http.NewRequest("", "http://howdy.com", nil)
req.AddCookie(&http.Cookie{
Name: test.Name,
Value: test.Value,
})
cook := cookie{
Name: test.Lookup,
Lifespan: 1 * time.Second,
Inactivity: defaultInactivityDuration,
Now: func() time.Time {
return time.Unix(0, 0)
},
Tokens: &MockTokenizer{
Principal: Principal{
Subject: test.Value,
},
ValidErr: test.ValidErr,
},
}
principal, err := cook.Validate(context.Background(), req)
if err != test.Err {
t.Errorf("Cookie extract error; expected %v actual %v", test.Err, err)
}
if principal.Subject != test.Expected {
t.Errorf("Token extract error; expected %v actual %v", test.Expected, principal.Subject)
}
}
}
func TestNewCookieJWT(t *testing.T) {
auth := NewCookieJWT("secret", 2*time.Second, defaultInactivityDuration, false)
if cookie, ok := auth.(*cookie); !ok {
t.Errorf("NewCookieJWT() did not create cookie Authenticator")
} else if cookie.Inactivity != time.Second {
t.Errorf("NewCookieJWT() inactivity was not two seconds: %s", cookie.Inactivity)
}
auth = NewCookieJWT("secret", time.Hour, defaultInactivityDuration, false)
if cookie, ok := auth.(*cookie); !ok {
t.Errorf("NewCookieJWT() did not create cookie Authenticator")
} else if cookie.Inactivity != defaultInactivityDuration {
t.Errorf("NewCookieJWT() inactivity was not five minutes: %s", cookie.Inactivity)
}
auth = NewCookieJWT("secret", 0, defaultInactivityDuration, false)
if cookie, ok := auth.(*cookie); !ok {
t.Errorf("NewCookieJWT() did not create cookie Authenticator")
} else if cookie.Inactivity != defaultInactivityDuration {
t.Errorf("NewCookieJWT() inactivity was not five minutes: %s", cookie.Inactivity)
}
auth = NewCookieJWT("secret", 30*time.Minute, defaultInactivityDuration, true)
if cookie, ok := auth.(*cookie); !ok {
t.Errorf("NewCookieJWT() did not create cookie Authenticator")
} else if cookie.Inactivity != defaultInactivityDuration {
t.Errorf("NewCookieJWT() inactivity was not five minutes: %s", cookie.Inactivity)
} else if !cookie.Secure {
t.Errorf("NewCookieJWT() secure was false, expected true")
}
}
func TestCookieExtend(t *testing.T) {
history := time.Unix(-446774400, 0)
type fields struct {
Name string
Lifespan time.Duration
Inactivity time.Duration
Now func() time.Time
Tokens Tokenizer
}
type args struct {
ctx context.Context
w *httptest.ResponseRecorder
p Principal
}
tests := []struct {
name string
fields fields
args args
want Principal
wantErr bool
}{
{
name: "Successful extention",
want: Principal{
Subject: "subject",
},
fields: fields{
Name: "session",
Lifespan: time.Second,
Inactivity: time.Second,
Now: func() time.Time {
return history
},
Tokens: &MockTokenizer{
Principal: Principal{
Subject: "subject",
},
Token: "token",
ExtendErr: nil,
},
},
args: args{
ctx: context.Background(),
w: httptest.NewRecorder(),
p: Principal{
Subject: "subject",
},
},
},
{
name: "Unable to extend",
wantErr: true,
fields: fields{
Tokens: &MockTokenizer{
ExtendErr: fmt.Errorf("bad extend"),
},
},
args: args{
ctx: context.Background(),
w: httptest.NewRecorder(),
p: Principal{
Subject: "subject",
},
},
},
{
name: "Unable to create",
wantErr: true,
fields: fields{
Tokens: &MockTokenizer{
CreateErr: fmt.Errorf("bad extend"),
},
},
args: args{
ctx: context.Background(),
w: httptest.NewRecorder(),
p: Principal{
Subject: "subject",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &cookie{
Name: tt.fields.Name,
Lifespan: tt.fields.Lifespan,
Inactivity: tt.fields.Inactivity,
Now: tt.fields.Now,
Tokens: tt.fields.Tokens,
}
got, err := c.Extend(tt.args.ctx, tt.args.w, tt.args.p)
if (err != nil) != tt.wantErr {
t.Errorf("cookie.Extend() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr == false {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("cookie.Extend() = %v, want %v", got, tt.want)
}
cookies := tt.args.w.HeaderMap["Set-Cookie"]
if len(cookies) == 0 {
t.Fatal("Expected some cookies but got zero")
}
log.Printf("%s", cookies)
want := fmt.Sprintf("%s=%s", DefaultCookieName, "token")
if !strings.Contains(cookies[0], want) {
t.Errorf("cookie.Extend() = %v, want %v", cookies[0], want)
}
}
})
}
}