Debian Codegraph @

command/crypto/otp/generate.go

raw symbols SCIP
1 package otp
2
3 import (
4 "bytes"
5 "fmt"
6 "image/png"
7 "strings"
8
9 "github.com/pquerna/otp"
10 "github.com/pquerna/otp/totp"
11 "github.com/smallstep/cli/command"
12 "github.com/smallstep/cli/errs"
13 "github.com/smallstep/cli/flags"
14 "github.com/smallstep/cli/utils"
15 "github.com/urfave/cli"
16 )
17
18 func generateCommand() cli.Command {
19 return cli.Command{
20 Name: "generate",
21 Action: command.ActionFunc(generateAction),
22 Usage: "generate a one-time password",
23 UsageText: `**step crypto otp generate** [**--issuer**=<name>]
24 [**--account**=<user-name>] [**--period**=<seconds>] [**--length**=<size>]
25 [**--alg**=<alg>] [**--url**] [**--qr**]`,
26 Description: `**step crypto otp generate** does TOTP and HTOP`,
27 Flags: []cli.Flag{
28 cli.StringFlag{
29 Name: "issuer, iss",
30 Usage: `Name of the issuing organization (e.g., smallstep.com)`,
31 },
32 cli.StringFlag{
33 Name: "account",
34 Usage: `Name of the user's account (e.g., a username or email
35 address)`,
36 },
37 cli.IntFlag{
38 Name: "period",
39 Usage: `Number of seconds a TOTP hash is valid. Defaults to 30
40 seconds.`,
41 Value: 30,
42 },
43 cli.IntFlag{
44 Name: "length, digits",
45 Usage: `Length of one-time passwords. Defaults to 6.`,
46 Value: 6,
47 },
48 cli.IntFlag{
49 Name: "secret-size",
50 Usage: `Size of generated TOTP secret. Defaults to 20.`,
51 Value: 20,
52 },
53 cli.StringFlag{
54 Name: "alg, algorithm",
55 Usage: `Algorithm to use for HMAC. Defaults to SHA1. Must be
56 one of: SHA1, SHA256, SHA512`,
57 Value: "SHA1",
58 },
59 cli.BoolFlag{
60 Name: "url",
61 Usage: `Output a TOTP Key URI. See
62 https://github.com/google/google-authenticator/wiki/Key-Uri-Format`,
63 },
64 cli.StringFlag{
65 Name: "qr",
66 Usage: `Write a QR code to the specified path`,
67 },
68 flags.Force,
69 },
70 }
71 }
72
73 func generateAction(ctx *cli.Context) error {
74 switch {
75 case len(ctx.String("issuer")) == 0:
76 return errs.RequiredFlag(ctx, "issuer")
77 case len(ctx.String("account")) == 0:
78 return errs.RequiredFlag(ctx, "account")
79 }
80
81 key, err := generate(ctx)
82 if err != nil {
83 return err
84 }
85
86 if ctx.IsSet("qr") {
87 filename := ctx.String("qr")
88
89 // Convert TOTP key into a PNG
90 var buf bytes.Buffer
91 img, err := key.Image(200, 200)
92 if err != nil {
93 return err
94 }
95 png.Encode(&buf, img)
96 if err := utils.WriteFile(filename, buf.Bytes(), 0644); err != nil {
97 return errs.FileError(err, filename)
98 }
99 }
100
101 if ctx.Bool("url") {
102 fmt.Println(key.String())
103 } else {
104 fmt.Println(key.Secret())
105 }
106
107 return nil
108 }
109
110 func algFromString(ctx *cli.Context, alg string) (otp.Algorithm, error) {
111 switch strings.ToUpper(alg) {
112 case "SHA1":
113 return otp.AlgorithmSHA1, nil
114 case "SHA256":
115 return otp.AlgorithmSHA256, nil
116 case "SHA512":
117 return otp.AlgorithmSHA512, nil
118 default:
119 return 0, errs.InvalidFlagValue(ctx, "alg", alg, "SHA1, SHA256, or SHA512")
120 }
121 }
122
123 func generate(ctx *cli.Context) (*otp.Key, error) {
124 alg, err := algFromString(ctx, ctx.String("alg"))
125 if err != nil {
126 return nil, err
127 }
128 return totp.Generate(totp.GenerateOpts{
129 Issuer: ctx.String("issuer"),
130 AccountName: ctx.String("account"),
131 Period: uint(ctx.Int("period")),
132 SecretSize: uint(ctx.Int("secret-size")),
133 Digits: otp.Digits(ctx.Int("length")),
134 Algorithm: alg,
135 })
136 }