forked from lestrrat-go/jwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt_validate_example_test.go
51 lines (45 loc) · 1.02 KB
/
jwt_validate_example_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
package examples_test
import (
"encoding/json"
"fmt"
"time"
"github.com/lestrrat-go/jwx/v2/jwt"
)
func ExampleJWT_Validate() {
tok, err := jwt.NewBuilder().
Issuer(`github.com/lestrrat-go/jwx`).
Expiration(time.Now().Add(-1 * time.Hour)).
Build()
if err != nil {
fmt.Printf("failed to build token: %s\n", err)
return
}
{
// Case 1: Using jwt.Validate()
err = jwt.Validate(tok)
if err == nil {
fmt.Printf("token should fail validation\n")
return
}
fmt.Printf("%s\n", err)
}
{
// Case 2: Using jwt.Parse()
buf, err := json.Marshal(tok)
if err != nil {
fmt.Printf("failed to serialize token: %s\n", err)
return
}
// NOTE: This token has NOT been verified for demonstration
// purposes. Use `jwt.WithKey()` or the like in your production code
_, err = jwt.Parse(buf, jwt.WithVerify(false), jwt.WithValidate(true))
if err == nil {
fmt.Printf("token should fail validation\n")
return
}
fmt.Printf("%s\n", err)
}
// OUTPUT:
// "exp" not satisfied
// "exp" not satisfied
}