-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
jwt_serialize_jwe_jws_example_test.go
54 lines (47 loc) · 1.21 KB
/
jwt_serialize_jwe_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
51
52
53
54
package examples_test
import (
"crypto/rand"
"crypto/rsa"
"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_SerializeJWEJWS() {
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
}
privkey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Printf("failed to generate private key: %s\n", err)
return
}
enckey, err := jwk.Import(privkey.PublicKey)
if err != nil {
fmt.Printf("failed to create symmetric key: %s\n", err)
return
}
signkey, err := jwk.Import([]byte(`abracadabra`))
if err != nil {
fmt.Printf("failed to create symmetric key: %s\n", err)
return
}
serialized, err := jwt.NewSerializer().
Encrypt(jwt.WithKey(jwa.RSA_OAEP(), enckey)).
Sign(jwt.WithKey(jwa.HS256(), signkey)).
Serialize(tok)
if err != nil {
fmt.Printf("failed to encrypt and sign token: %s\n", err)
return
}
_ = serialized
// We don't use the result of serialization as it will always be
// different because of randomness used in the encryption logic
// OUTPUT:
}