-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth_test.go
50 lines (41 loc) · 1.31 KB
/
auth_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
package auth
import (
"testing"
)
func TestMacaroon(t *testing.T) {
auth, _ := NewAuth("", NewInMemoryDB([]byte("kek"), MacaroonLifetime))
// Emulate generation token on the server, with the user id taken from other
// token, for example jwt.
tokenStr, err := auth.GenerateToken(100, []string{"disabled"})
if err != nil {
t.Fatalf("unable to generate macaroon token: %v", err)
}
// Emulate that client received the token, and now want to make some
// operation on the server, he has to add time and nonce constraints,
// before making an operation, otherwise he/she under threat of replay-attack.
m, err := DecodeMacaroon(tokenStr)
if err != nil {
t.Fatalf("unable to decode macaroon: %v", err)
}
m, err = AddNonce(m, 10)
if err != nil {
t.Fatalf("unable to add nonce: %v", err)
}
m, err = AddCurrentTime(m)
if err != nil {
t.Fatalf("unable to add current time: %v", err)
}
tokenStr, err = EncodeMacaroon(m)
if err != nil {
t.Fatalf("unable to encode macaroon")
}
// Emulate that server received the macaroon and validating it and also
// check that operation is permitted.
token, err := auth.ExtractToken(tokenStr)
if err != nil {
t.Fatalf("operation should be not allowed")
}
if err = token.IsAuthorized("disabled"); err != ErrOperNotAllowed {
t.Fatalf("operation should be not allowed")
}
}