-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
jwt_serialize_jws_example_test.go
50 lines (43 loc) · 1.38 KB
/
jwt_serialize_jws_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
package examples_test
import (
"fmt"
"time"
"github.com/lestrrat-go/jwx/v3/jwa"
"github.com/lestrrat-go/jwx/v3/jwk"
"github.com/lestrrat-go/jwx/v3/jwt"
)
func ExampleJWT_SerializeJWS() {
tok, err := jwt.NewBuilder().
Issuer(`github.com/lestrrat-go/jwx`).
IssuedAt(time.Unix(aLongLongTimeAgo, 0)).
Build()
if err != nil {
fmt.Printf("failed to build token: %s\n", err)
return
}
rawKey := []byte(`abracadabra`)
jwkKey, err := jwk.Import(rawKey)
if err != nil {
fmt.Printf("failed to create symmetric key: %s\n", err)
return
}
// This example shows you two ways to passing keys to
// jwt.Sign()
//
// * The first key is the "raw" key.
// * The second one is a jwk.Key that represents the raw key.
//
// If this were using RSA/ECDSA keys, you would be using
// *rsa.PrivateKey/*ecdsa.PrivateKey as the raw key.
for _, key := range []interface{}{rawKey, jwkKey} {
serialized, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256(), key))
if err != nil {
fmt.Printf("failed to sign token: %s\n", err)
return
}
fmt.Printf("%s\n", serialized)
}
// OUTPUT:
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjIzMzQzMTIwMCwiaXNzIjoiZ2l0aHViLmNvbS9sZXN0cnJhdC1nby9qd3gifQ.K1WVWaM6Dww9aNNFMjnyUfjaaHIs08-3Qb1b8eSEHOk
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjIzMzQzMTIwMCwiaXNzIjoiZ2l0aHViLmNvbS9sZXN0cnJhdC1nby9qd3gifQ.K1WVWaM6Dww9aNNFMjnyUfjaaHIs08-3Qb1b8eSEHOk
}