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