-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathjwt_get_claims_example_test.go
91 lines (77 loc) · 2.61 KB
/
jwt_get_claims_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
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
package examples_test
import (
"encoding/json"
"fmt"
"time"
"github.com/lestrrat-go/jwx/v3/jwk"
"github.com/lestrrat-go/jwx/v3/jwt"
)
func ExampleJWT_GetClaims() {
tok, err := jwt.NewBuilder().
IssuedAt(time.Now()).
Issuer(`github.com/lestrrat-go/jwx`).
Subject(`example`).
Claim(`claim1`, `value1`).
Claim(`claim2`, `2022-05-16T07:35:56+00:00`).
Claim(`claim3`, `{"kty": "oct", "alg":"A128KW", "k":"GawgguFyGrWKav7AX4VKUg"}`).
Build()
if err != nil {
fmt.Printf("failed to build token: %s\n", err)
return
}
// Pre-defined fields have typed accessors.
iat, _ := tok.IssuedAt()
iss, _ := tok.Issuer()
sub, _ := tok.Subject()
var _ time.Time = iat
var _ string = iss
var _ string = sub
// But you can also get them via the generic `.Get()` method.
// However, you would need to decide for yourself what the
// return type is. If you don't need the exact type, you could
// use interface{}, or you could use the specific time.Time
// type
//
// For the key name you could also use jwt.IssuedAtKey constant
_ = tok.Get(`iat`, &iat)
// var iat interface{} would also work, but you would need to
// convert the type if you need time.Time specific behavior
// Private claims
var dummy interface{}
_ = tok.Get(`claim1`, &dummy)
_ = tok.Get(`claim2`, &dummy)
_ = tok.Get(`claim3`, &dummy)
// However, it is possible to globally specify that a private
// claim should be parsed into a custom type.
// In the sample below `claim2` is to be an instance of time.Time
jwt.RegisterCustomField(`claim2`, time.Time{})
tok = jwt.New()
if err := json.Unmarshal([]byte(`{"claim2":"2022-05-16T07:35:56+00:00"}`), tok); err != nil {
fmt.Printf(`failed to parse token: %s`, err)
return
}
// now you can use the exact type
var claim2 time.Time
if err := tok.Get(`claim2`, &claim2); err != nil {
fmt.Printf("failed to get private claim \"claim2\": %s\n", err)
return
}
// It's also possible to specify a custom decoder for a private claim.
// For example, in the case of `claim3`, it needs to call `jwk.ParseKey`
// which returns an interface that can't be instantiated like the
// `time.Time` value for `claim2`.
jwt.RegisterCustomField(`claim3`, jwt.CustomDecodeFunc(func(data []byte) (interface{}, error) {
return jwk.ParseKey(data)
}))
tok = jwt.New()
if err := json.Unmarshal([]byte(`{"claim3": {"kty": "oct", "alg":"A128KW", "k":"GawgguFyGrWKav7AX4VKUg"}}`), tok); err != nil {
fmt.Printf(`failed to parse token: %s`, err)
return
}
var claim3 jwk.Key
if err := tok.Get(`claim3`, &claim3); err != nil {
fmt.Printf("failed to get private claim \"claim3\": %s\n", err)
return
}
// OUTPUT:
}