-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
86 lines (70 loc) · 2.22 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
package main
import (
"log"
"time"
"github.com/kataras/jwt"
)
// Replace with your own keys and keep them secret.
// The "encKey" is used for the encryption and
// the "sigKey" is used for the selected JSON Web Algorithm
// (shared/symmetric HMAC in that case).
var (
sigKey = jwt.MustGenerateRandom(32)
encKey = jwt.MustGenerateRandom(32)
)
type myClaims struct {
Foo string `json:"bar"`
}
func main() {
token, err := jwt.Sign(jwt.HS256, sigKey, myClaims{Foo: "bar"}, jwt.MaxAge(10*time.Second))
if err != nil {
log.Fatal(err)
}
verifyWithStandardExpected(token)
verifyWithLeeway(token)
verifyWithCustomExpectations(token)
log.Print("Sleeping for 6 seconds...")
time.Sleep(6 * time.Second)
verifyWithCustomExpectations(token) // this should pass.
// Now sleep for 6 seconds, even if it's not expired,
// the Leeway adds 5 seconds; 11 > 10, so it should fail with ErrExpired.
verifyWithLeeway(token) // this should fail.
}
func verifyWithStandardExpected(token []byte) {
// The last argument of Verify/VerifyEncrypted optionally
// accepts one or more TokenValidators.
// Builtin validators: The Blocklist, Expected and Leeway.
_, err := jwt.Verify(jwt.HS256, sigKey, token, jwt.Expected{
Issuer: "my-app",
})
if err != nil {
// Should fail with:
// errors.Is(err, jwt.ErrExpected)
log.Printf("(verifyWithStandardExpected) token is invalid: %v", err)
return
}
}
func verifyWithLeeway(token []byte) {
verifiedToken, err := jwt.Verify(jwt.HS256, sigKey, token, jwt.Leeway(5*time.Second) /* you can add more validators, e.g. jwt.Expected{} too */)
if err != nil {
log.Printf("(verifyWithLeeway) token is invalid: %v", err)
return
}
var claims myClaims
verifiedToken.Claims(&claims)
log.Printf("(verifyWithLeeway) Got claims: %#+v", claims)
}
func verifyWithCustomExpectations(token []byte) {
verifiedToken, err := jwt.Verify(jwt.HS256, sigKey, token)
if err != nil {
log.Printf("(verifyWithCustomExpectations) token is invalid: %v", err)
}
var claims myClaims
verifiedToken.Claims(&claims)
// Here you can add custom type validations.
if claims.Foo != "bar" {
log.Printf("expected foo==bar but got: %v", claims.Foo)
return
}
log.Printf("(verifyWithCustomExpectations) Got claims: %#+v", claims)
}