| 1 | package jws |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/pkg/errors" |
| 11 | "github.com/smallstep/cli/errs" |
| 12 | "github.com/smallstep/cli/jose" |
| 13 | "github.com/smallstep/cli/utils" |
| 14 | "github.com/urfave/cli" |
| 15 | ) |
| 16 | |
| 17 | func inspectCommand() cli.Command { |
| 18 | return cli.Command{ |
| 19 | Name: "inspect", |
| 20 | Action: cli.ActionFunc(inspectAction), |
| 21 | Usage: `return the decoded JWS without verification`, |
| 22 | UsageText: `**step crypto jws inspect** |
| 23 | **--insecure** [**--json**]`, |
| 24 | Description: `**step crypto jws inspect** reads a JWS data structure from STDIN, decodes it, |
| 25 | and outputs the payload on STDERR. Since this command does not verify the JWS |
| 26 | you must pass **--insecure** as a misuse prevention mechanism. |
| 27 | |
| 28 | For examples, see **step help crypto jws**.`, |
| 29 | Flags: []cli.Flag{ |
| 30 | cli.BoolFlag{ |
| 31 | Name: "json", |
| 32 | Usage: `Displays the header, payload and signature as a JSON object. The payload will |
| 33 | be encoded using Base64.`, |
| 34 | }, |
| 35 | cli.BoolFlag{ |
| 36 | Name: "insecure", |
| 37 | Hidden: true, |
| 38 | }, |
| 39 | }, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func inspectAction(ctx *cli.Context) error { |
| 44 | if err := errs.NumberOfArguments(ctx, 0); err != nil { |
| 45 | return err |
| 46 | } |
| 47 | |
| 48 | if !ctx.Bool("insecure") { |
| 49 | return errs.InsecureCommand(ctx) |
| 50 | } |
| 51 | |
| 52 | token, err := utils.ReadString(os.Stdin) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | |
| 57 | tok, err := jose.ParseJWS(token) |
| 58 | if err != nil { |
| 59 | return errors.Wrap(jose.TrimPrefix(err), "error parsing token") |
| 60 | } |
| 61 | |
| 62 | if ctx.Bool("json") { |
| 63 | return printToken(tok) |
| 64 | } |
| 65 | |
| 66 | os.Stdout.Write(tok.UnsafePayloadWithoutVerification()) |
| 67 | return nil |
| 68 | } |
| 69 | |
| 70 | func printToken(tok *jose.JSONWebSignature) error { |
| 71 | token, err := tok.CompactSerialize() |
| 72 | if err != nil { |
| 73 | return errors.Wrap(jose.TrimPrefix(err), "error serializing token") |
| 74 | } |
| 75 | |
| 76 | parts := strings.Split(token, ".") |
| 77 | if len(parts) != 3 { |
| 78 | return errors.New("error decoding token: JWS must have three parts") |
| 79 | } |
| 80 | |
| 81 | header, err := base64.RawURLEncoding.DecodeString(parts[0]) |
| 82 | if err != nil { |
| 83 | return errors.Wrapf(err, "error decoding token") |
| 84 | } |
| 85 | |
| 86 | m := make(map[string]json.RawMessage) |
| 87 | m["header"] = header |
| 88 | m["payload"] = []byte(`"` + parts[1] + `"`) |
| 89 | m["signature"] = []byte(`"` + parts[2] + `"`) |
| 90 | |
| 91 | b, err := json.MarshalIndent(m, "", " ") |
| 92 | if err != nil { |
| 93 | return errors.Wrapf(err, "error marshaling token data") |
| 94 | } |
| 95 | |
| 96 | fmt.Println(string(b)) |
| 97 | return nil |
| 98 | } |