Debian Codegraph @

command/crypto/jose/jose.go

raw symbols SCIP
1 package jose
2
3 import (
4 "fmt"
5 "io/ioutil"
6 "os"
7 "regexp"
8 "strings"
9
10 "github.com/pkg/errors"
11 "github.com/smallstep/cli/jose"
12 "github.com/urfave/cli"
13 )
14
15 // Command returns the cli.Command for jose related subcommands.
16 func Command() cli.Command {
17 return cli.Command{
18 Name: "jose",
19 Usage: "collection of JOSE utilities",
20 UsageText: "step crypto jose <subcommand> [arguments] [global-flags] [subcommand-flags]",
21 Subcommands: cli.Commands{
22 formatCommand(),
23 },
24 }
25 }
26
27 type serializer interface {
28 CompactSerialize() (string, error)
29 FullSerialize() string
30 }
31
32 func formatCommand() cli.Command {
33 return cli.Command{
34 Name: "format",
35 Usage: "swap serialization format",
36 UsageText: `**step crypto jose format**`,
37 Action: cli.ActionFunc(formatAction),
38 Description: `**step crypto jose format** reads a JWT, a JWS, or a JWE from STDIN swaps the
39 serialization of the content, from compact to JSON or from JSON to compact.
40
41 ## EXAMPLES
42
43 Transform a JSON encrypted message to the compact serialization format:
44 '''
45 $ echo The message | step crypto jwe encrypt --key p256.enc.pub | step crypto jose format
46 eyJhbGciOiJFQ0RILUVTIiwiZW5jIjoiQTI1NkdDTSIsImVwayI6eyJrdHkiOiJFQyIsImNydiI6IlAtMjU2IiwieCI6IlNTR1pNdjZyMGlHbmtsMnpKRERXS1JlaDU4R3RwTjVjT2tBZnlaaUI0enMiLCJ5IjoiLUJzQ2w5RjZNd28zRWZoTFJIeVdDbGlxU2d6T2tubzNuWW80azlPSVk0TSJ9LCJraWQiOiJHd0tSTUdXY1pWNFE2dGZZblpjZm90N090N2hjQ0t2cUJPVWljX0JoZ0gwIn0
47 .
48 .
49 iJNn8SrqE8I5Bhog
50 .
51 NO9FfC25Ow9ogzq1.6M3Jiy_osGwlioJjXPyl9w
52 '''
53
54 Transform a compact token to the JSON serialization format:
55 '''
56 $ step crypto jwt sign --key p256.priv.json --iss "joe" --aud "bob" \
57 --sub "hello" --exp $(date -v+1M +"%s") | step crypto jose format
58 {
59 "payload":"eyJhdWQiOiJib2IiLCJleHAiOjE1MzUyNDE4OTYsImlhdCI6MTUzMjU2MzQ5OCwiaXNzIjoiam9lIiwibmJmIjoxNTMyNTYzNDk4LCJzdWIiOiJoZWxsbyJ9",
60 "protected":"eyJhbGciOiJFUzI1NiIsImtpZCI6IlpqR1g5N0xtY2ZsUG9sV3Zzb0FXekM1V1BXa05GRkgzUWRLTFVXOTc4aGsiLCJ0eXAiOiJKV1QifQ",
61 "signature":"wlRDGrjQItHFu5j2H4A4T6_P5Ek00ugJXQ3iIXibsZjU96_BaqddnAqFWeKpb6xHWGRAHKtlm9bUYBfLQ8Jlsg"
62 }
63 '''`,
64 }
65 }
66
67 func formatAction(ctx *cli.Context) error {
68 input, err := ioutil.ReadAll(os.Stdin)
69 if err != nil {
70 return errors.Wrap(err, "error reading input")
71 }
72
73 token := stripWhitespace(string(input))
74
75 // Attempt to parse an encrypted
76 // If it fails parse a regular JWS
77 var srz serializer
78 if enc, err := jose.ParseEncrypted(token); err == nil {
79 srz = enc
80 } else {
81 tok, err := jose.ParseJWS(token)
82 if err != nil {
83 return errors.Wrap(trimPrefix(err), "error parsing data")
84 }
85 srz = tok
86 }
87
88 if strings.HasPrefix(token, "{") {
89 str, err := srz.CompactSerialize()
90 if err != nil {
91 return errors.Wrap(trimPrefix(err), "error serializing data")
92 }
93 fmt.Println(str)
94 } else {
95 fmt.Println(srz.FullSerialize())
96 }
97
98 return nil
99 }
100
101 func trimPrefix(err error) error {
102 return errors.New(strings.TrimPrefix(err.Error(), "square/go-jose: "))
103 }
104
105 var stripWhitespaceRegex = regexp.MustCompile(`\s`)
106
107 // stripWhitespace strip all newlines and whitespace
108 func stripWhitespace(data string) string {
109 return stripWhitespaceRegex.ReplaceAllString(data, "")
110 }