-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
422 lines (388 loc) · 10.3 KB
/
main.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package main
import (
"crypto/rand"
"encoding/hex"
"flag"
"fmt"
"log"
"net/url"
"os"
"strings"
"time"
)
type payload struct {
Iss string `json:"iss,omitempty"`
Aud string `json:"aud,omitempty"`
Exp int64 `json:"exp,omitempty"`
Iat int64 `json:"iat,omitempty"`
Email string `json:"email,omitempty"`
EmailOrig string `json:"email_original,omitempty"`
Nonce string `json:"nonce,omitempty"`
}
func main() {
bin := flag.String("bin", "", "executable that runs the client")
debug := flag.Bool("debug", false, "log all communication")
flag.Parse()
if *bin == "" {
log.Fatal("-bin is required")
}
log.SetFlags(0)
initSigner()
initServer(sgn.keySet)
initSubprocess(*bin, srv.origin)
proc.debug = *debug
var clientID string
test("basic auth", func() {
email := "[email protected]"
stateBuf := make([]uint8, 4)
_, err := rand.Read(stateBuf)
if err != nil {
log.Fatal("rand.Read error:", err)
}
state := hex.EncodeToString(stateBuf)
proc.writeLine("auth", email, state)
authURLStr := proc.expect("ok", "start authentication request")
if authURLStr == "" {
return
}
assert(
strings.HasPrefix(authURLStr, authEndpoint+"?"),
"auth URL points to the server auth endpoint",
)
authURL, err := url.Parse(authURLStr)
if !assertOK(err, "auth URL is a valid URL") {
return
}
params := authURL.Query()
assertEq(params.Get("login_hint"), email, "login_hint is correct")
assertEq(params.Get("scope"), "openid email", "scope is correct")
assertEq(params.Get("response_type"), "id_token", "response_type is correct")
assertEq(params.Get("state"), state, "state matches input")
redirectURI, err := url.Parse(params.Get("redirect_uri"))
if !assertOK(err, "redirect_uri is a valid URL") ||
!assert(redirectURI.IsAbs(), "redirect_uri is an absolute URL") {
return
}
// Assume further test runs have the same client ID.
clientID = fmt.Sprintf("%s://%s", redirectURI.Scheme, redirectURI.Host)
assertEq(params.Get("client_id"), clientID, "client_id is the origin of redirect_uri")
nonce := params.Get("nonce")
assert(nonce != "", "nonce is not empty")
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 5,
Iat: now,
Email: email,
Nonce: nonce,
}))
verified := proc.expect("ok", "verify token request")
assertEq(verified, email, "verified email matches input")
})
test("invalid issuer", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: "http://eve.invalid",
Aud: clientID,
Exp: now + 5,
Iat: now,
Email: email,
Nonce: nonce,
}))
proc.expect("err", "rejects token")
}
})
test("invalid audience", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: "http://eve.invalid",
Exp: now + 5,
Iat: now,
Email: email,
Nonce: nonce,
}))
proc.expect("err", "rejects token")
}
})
test("expired token", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: clientID,
Exp: now - 10000,
Iat: now - 10020,
Email: email,
Nonce: nonce,
}))
proc.expect("err", "rejects token")
}
})
test("expired token, but within leeway", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: clientID,
Exp: now - 30,
Iat: now - 50,
Email: email,
Nonce: nonce,
}))
proc.expect("ok", "accepts token")
}
})
test("future issue time", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 10020,
Iat: now + 10000,
Email: email,
Nonce: nonce,
}))
proc.expect("err", "rejects token")
}
})
test("floating point exp", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&struct {
Iss string `json:"iss,omitempty"`
Aud string `json:"aud,omitempty"`
Exp float64 `json:"exp,omitempty"`
Iat int64 `json:"iat,omitempty"`
Email string `json:"email,omitempty"`
EmailOrig string `json:"email_original,omitempty"`
Nonce string `json:"nonce,omitempty"`
}{
Iss: srv.origin,
Aud: clientID,
Exp: float64(now) + 5.5,
Iat: now,
Email: email,
Nonce: nonce,
}))
proc.expect("ok", "accepts token")
}
})
test("floating point iat", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&struct {
Iss string `json:"iss,omitempty"`
Aud string `json:"aud,omitempty"`
Exp int64 `json:"exp,omitempty"`
Iat float64 `json:"iat,omitempty"`
Email string `json:"email,omitempty"`
EmailOrig string `json:"email_original,omitempty"`
Nonce string `json:"nonce,omitempty"`
}{
Iss: srv.origin,
Aud: clientID,
Exp: now + 5,
Iat: float64(now) - 0.5,
Email: email,
Nonce: nonce,
}))
proc.expect("ok", "accepts token")
}
})
test("future issue time, but within leeway", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 50,
Iat: now + 30,
Email: email,
Nonce: nonce,
}))
proc.expect("ok", "accepts token")
}
})
test("unexpected email transform", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 5,
Iat: now,
Email: "[email protected]",
Nonce: nonce,
}))
proc.expect("err", "rejects token")
}
})
test("unexpected email_original transform", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 5,
Iat: now,
Email: email,
EmailOrig: "[email protected]",
Nonce: nonce,
}))
proc.expect("err", "rejects token")
}
})
test("server normalization", func() {
email := "İⅢ@İⅢ.example"
normalized := "i̇ⅲ@xn--iiii-qwc.example"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 5,
Iat: now,
Email: normalized,
EmailOrig: email,
Nonce: nonce,
}))
verified := proc.expect("ok", "verify token request")
assertEq(verified, normalized, "verified email is normalized input")
}
})
test("custom server normalization", func() {
email := "[email protected]"
normalized := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 5,
Iat: now,
Email: normalized,
EmailOrig: email,
Nonce: nonce,
}))
verified := proc.expect("ok", "verify token request")
assertEq(verified, normalized, "verified email is normalized input")
}
})
test("invalid nonce", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 5,
Iat: now,
Email: email,
Nonce: "definitely not something the client generates",
}))
proc.expect("err", "rejects token")
}
})
test("nonce reuse", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 5,
Iat: now,
Email: email,
Nonce: nonce,
}))
proc.expect("ok", "accepts token")
now = time.Now().Unix()
proc.writeLine("verify", sgn.simple(&payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 5,
Iat: now,
Email: email,
Nonce: nonce,
}))
proc.expect("err", "rejects token")
}
})
test("invalid key ID", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.sign(sgn.priv, &header{
KID: "bad key",
Alg: alg,
}, &payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 5,
Iat: now,
Email: email,
Nonce: nonce,
}))
proc.expect("err", "rejects token")
}
})
test("bad signature", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.sign(sgn.fake, &header{
KID: kid,
Alg: alg,
}, &payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 5,
Iat: now,
Email: email,
Nonce: nonce,
}))
proc.expect("err", "rejects token")
}
})
test("token cannot change alg from jwk", func() {
email := "[email protected]"
if nonce := quickStart(email); nonce != "" {
now := time.Now().Unix()
proc.writeLine("verify", sgn.sign(sgn.priv, &header{
KID: kid,
Alg: "RS384",
}, &payload{
Iss: srv.origin,
Aud: clientID,
Exp: now + 5,
Iat: now,
Email: email,
Nonce: nonce,
}))
proc.expect("err", "rejects token")
}
})
test("caching", func() {
assertEq(srv.numConfigRequests, 1, "discovery requested just once")
assertEq(srv.numKeysRequests, 1, "keys requested just once")
})
proc.stop()
if !allOk {
os.Exit(1)
}
}