Debian Codegraph @

command/crypto/jwk/public.go

raw symbols SCIP
1 package jwk
2
3 import (
4 "encoding/json"
5 "fmt"
6 "io/ioutil"
7 "os"
8
9 "github.com/pkg/errors"
10 "github.com/smallstep/cli/jose"
11 "github.com/urfave/cli"
12 )
13
14 func publicCommand() cli.Command {
15 return cli.Command{
16 Name: "public",
17 Action: cli.ActionFunc(publicAction),
18 Usage: "extract a public JSON Web Key (JWK) from a private JWK",
19 UsageText: `**step crypto jwk public**`,
20 Description: `**step crypto jwk public** command reads a JWK from STDIN, derives the
21 corresponding public JWK, and prints the derived JWK to STDOUT.
22
23 For examples, see **step help crypto jwk**.`,
24 }
25 }
26
27 func publicAction(ctx *cli.Context) error {
28 b, err := ioutil.ReadAll(os.Stdin)
29 if err != nil {
30 return errors.Wrap(err, "error reading from STDIN")
31 }
32
33 jwk := new(jose.JSONWebKey)
34 // Attempt to decrypt if encrypted
35 if b, err = jose.Decrypt("Please enter the password to decrypt your private JWK", b); err != nil {
36 return err
37 }
38
39 // Unmarshal the plain (or decrypted JWK)
40 if err = json.Unmarshal(b, jwk); err != nil {
41 return errors.New("error reading JWK: unsupported format")
42 }
43
44 var public jose.JSONWebKey
45
46 // go-jose JSONWebKey.Public() returns an empty key for oct
47 if _, ok := jwk.Key.([]byte); ok {
48 public = *jwk
49 } else {
50 public = jwk.Public()
51 }
52
53 b, err = json.MarshalIndent(public, "", " ")
54 if err != nil {
55 return errors.Wrap(err, "error marshaling JWK")
56 }
57
58 fmt.Println(string(b))
59 return nil
60 }