/
genjwk.go
54 lines (46 loc) · 1.04 KB
/
genjwk.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 cmd
import (
"encoding/json"
"github.com/spf13/cobra"
"github.com/shantanu-hashcash/go/exp/support/jwtkey"
supportlog "github.com/shantanu-hashcash/go/support/log"
"gopkg.in/square/go-jose.v2"
)
type GenJWKCommand struct {
Logger *supportlog.Entry
}
func (c *GenJWKCommand) Command() *cobra.Command {
cmd := &cobra.Command{
Use: "genjwk",
Short: "Generate a JSON Web Key (ECDSA/ES256) for JWT issuing",
Run: func(_ *cobra.Command, _ []string) {
c.Run()
},
}
return cmd
}
func (c *GenJWKCommand) Run() {
k, err := jwtkey.GenerateKey()
if err != nil {
c.Logger.Fatal(err)
}
alg := jose.ES256
{
jwk := jose.JSONWebKey{Key: &k.PublicKey, Algorithm: string(alg)}
bytes, err := json.Marshal(jwk)
if err == nil {
c.Logger.Print("Public:", string(bytes))
} else {
c.Logger.Print("Public:", err)
}
}
{
jwk := jose.JSONWebKey{Key: k, Algorithm: string(alg)}
bytes, err := json.Marshal(jwk)
if err == nil {
c.Logger.Print("Private:", string(bytes))
} else {
c.Logger.Print("Private:", err)
}
}
}