Debian Codegraph @

command/crypto/otp/verify.go

raw symbols SCIP
1 package otp
2
3 import (
4 "fmt"
5 "io/ioutil"
6 "net/url"
7 "os"
8 "strconv"
9 "strings"
10 "time"
11
12 "github.com/pkg/errors"
13 "github.com/pquerna/otp"
14 "github.com/pquerna/otp/totp"
15 "github.com/smallstep/cli/errs"
16 "github.com/smallstep/cli/flags"
17 "github.com/smallstep/cli/utils"
18 "github.com/urfave/cli"
19 )
20
21 func verifyCommand() cli.Command {
22 return cli.Command{
23 Name: "verify",
24 Action: cli.ActionFunc(verifyAction),
25 Usage: "verify a one-time password",
26 UsageText: `**step crypto otp verify** [**--secret**=<path>]
27 [**--period**=<seconds>] [**--skew**=<num>] [**--length**=<size>]
28 [**--alg**=<alg>] [*--time**=<time|duration>]`,
29 Description: `**step crypto otp verify** does TOTP and HTOP`,
30 Flags: []cli.Flag{
31 cli.StringFlag{
32 Name: "secret",
33 Usage: `Path to file containing TOTP secret.`,
34 },
35 cli.UintFlag{
36 Name: "period",
37 Usage: `Number of seconds a TOTP hash is valid. Defaults to 30
38 seconds.`,
39 Value: 30,
40 },
41 cli.UintFlag{
42 Name: "skew",
43 Usage: `Periods before or after current time to allow. Defaults
44 to 0. Values greater than 1 require '--insecure' flag.`,
45 Value: 0,
46 },
47 cli.IntFlag{
48 Name: "length, digits",
49 Usage: `Length of one-time passwords. Defaults to 6 digits.`,
50 Value: 6,
51 },
52 cli.StringFlag{
53 Name: "alg, algorithm",
54 Usage: `Algorithm to use for HMAC. Defaults to SHA1. Must be
55 one of: SHA1, SHA256, SHA512`,
56 Value: "SHA1",
57 },
58 cli.StringFlag{
59 Name: "time",
60 Usage: `The <time|duration> to use for TOTP validation. If a <time> is
61 used it is expected to be in RFC 3339 format. If a <duration> is used, it is a
62 sequence of decimal numbers, each with optional fraction and a unit suffix, such
63 as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms",
64 "s", "m", "h". A <duration> value is added to the current time. An empty
65 <time|duration> defaults to "time.Now()".`,
66 },
67 cli.BoolFlag{
68 Name: "insecure",
69 Hidden: true,
70 },
71 },
72 }
73 }
74
75 func verifyAction(ctx *cli.Context) error {
76 var (
77 secret string
78 // defaults
79 period = uint(30)
80 digits = 6
81 algStr = "SHA1"
82 )
83
84 secretFile := ctx.String("secret")
85 if len(secretFile) == 0 {
86 args := ctx.Args()
87 if len(args) == 0 {
88 return errs.RequiredFlag(ctx, "secret")
89 }
90 secretFile = args[0]
91 }
92 b, err := ioutil.ReadFile(secretFile)
93 if err != nil {
94 return errs.FileError(err, secretFile)
95 }
96 secret = string(b)
97 if strings.HasPrefix(secret, "otpauth://") {
98 otpKey, err := otp.NewKeyFromURL(secret)
99 if err != nil {
100 return errors.Wrap(err, "error parsing TOTP key from URL")
101 }
102
103 u, err := url.Parse(strings.TrimSpace(secret))
104 if err != nil {
105 return errors.Wrap(err, "error parsing TOTP Key URI in secret file")
106 }
107 q := u.Query()
108
109 secret = otpKey.Secret()
110 // period query param
111 if periodStr := q.Get("period"); len(periodStr) > 0 {
112 period64, err := strconv.ParseUint(periodStr, 10, 0)
113 if err != nil {
114 return errors.Wrap(err, "error parsing period from url")
115 }
116 period = uint(period64)
117 }
118 // digits query param
119 if digitStr := q.Get("digits"); len(digitStr) > 0 {
120 digits64, err := strconv.ParseInt(digitStr, 10, 0)
121 if err != nil {
122 return errors.Wrap(err, "error parsing period from url")
123 }
124 digits = int(digits64)
125 }
126 // algorithm query param
127 algFromQuery := q.Get("algorithm")
128 if len(algFromQuery) > 0 {
129 algStr = algFromQuery
130 }
131 }
132
133 if ctx.IsSet("period") {
134 period = ctx.Uint("period")
135 }
136 if ctx.IsSet("digits") {
137 digits = ctx.Int("digits")
138 }
139 if ctx.IsSet("alg") {
140 algStr = ctx.String("alg")
141 }
142 alg, err := algFromString(ctx, algStr)
143 if err != nil {
144 return err
145 }
146
147 var (
148 ok bool
149 selectTime time.Time
150 )
151 if ctx.String("time") == "" {
152 selectTime = time.Now()
153 } else {
154 selectTime, ok = flags.ParseTimeOrDuration(ctx.String("time"))
155 if !ok {
156 return errs.InvalidFlagValue(ctx, "time", ctx.String("time"), "")
157 }
158 }
159
160 skew := ctx.Uint("skew")
161 if skew > 1 && !ctx.Bool("insecure") {
162 return errors.Errorf("'--skew' values greater than 1 require the '--insecure' flag")
163 }
164
165 passcode, err := utils.ReadInput("Enter Passcode")
166 if err != nil {
167 return errors.Wrap(err, "error reading passcode")
168 }
169 valid, err := totp.ValidateCustom(string(passcode), secret, selectTime, totp.ValidateOpts{
170 Period: period,
171 Skew: skew,
172 Digits: otp.Digits(digits),
173 Algorithm: alg,
174 })
175
176 if err != nil {
177 return errors.Wrap(err, "error while validating TOTP")
178 } else if valid {
179 fmt.Println("ok")
180 os.Exit(0)
181 } else {
182 fmt.Println("fail")
183 os.Exit(1)
184 }
185 return nil
186 }