forked from gopasspw/gopass
/
otp.go
121 lines (102 loc) · 2.95 KB
/
otp.go
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
package action
import (
"context"
"errors"
"fmt"
"io/ioutil"
"strings"
"time"
"github.com/gokyle/twofactor"
"github.com/justwatchcom/gopass/store/secret"
"github.com/justwatchcom/gopass/utils/out"
"github.com/urfave/cli"
)
const (
// TODO - replace this with the currently un-exported step value
// from twofactor.FromURL
otpPeriod = 30
)
// OTP implements OTP token handling for TOTP and HOTP
func (s *Action) OTP(ctx context.Context, c *cli.Context) error {
name := c.Args().First()
if name == "" {
return exitError(ctx, ExitUsage, nil, "usage: %s otp [name]", s.Name)
}
qrf := c.String("qr")
clip := c.Bool("clip")
return s.otp(ctx, name, qrf, clip)
}
func (s *Action) otp(ctx context.Context, name, qrf string, clip bool) error {
sec, err := s.Store.Get(ctx, name)
if err != nil {
return exitError(ctx, ExitDecrypt, err, "failed to get entry '%s': %s", name, err)
}
otp, label, err := otpData(ctx, name, sec)
if err != nil {
return exitError(ctx, ExitUnknown, err, "No OTP entry found for %s: %s", name, err)
}
token := otp.OTP()
now := time.Now()
t := now.Add(otpPeriod * time.Second)
expiresAt := time.Unix(t.Unix()+otpPeriod-(t.Unix()%otpPeriod), 0)
secondsLeft := int(time.Until(expiresAt).Seconds())
if secondsLeft >= otpPeriod {
secondsLeft = secondsLeft - otpPeriod
}
out.Yellow(ctx, "%s lasts %ds \t|%s%s|", token, secondsLeft, strings.Repeat("-", otpPeriod-secondsLeft), strings.Repeat("=", secondsLeft))
if clip {
if err := copyToClipboard(ctx, fmt.Sprintf("token for %s", name), []byte(token)); err != nil {
return exitError(ctx, ExitIO, err, "failed to copy to clipboard: %s", err)
}
return nil
}
if qrf != "" {
return s.otpWriteQRFile(ctx, otp, label, qrf)
}
return nil
}
func otpData(ctx context.Context, name string, sec *secret.Secret) (twofactor.OTP, string, error) {
otpURL := ""
// check body
for _, line := range strings.Split(sec.Body(), "\n") {
if strings.HasPrefix(line, "otpauth://") {
otpURL = line
break
}
}
if otpURL != "" {
return twofactor.FromURL(otpURL)
}
// check yaml entry and fall back to password if we don't have one
label := name
secKey, err := sec.Value("totp")
if err != nil {
secKey = sec.Password()
}
if strings.HasPrefix(secKey, "otpauth://") {
return twofactor.FromURL(secKey)
}
otp, err := twofactor.NewGoogleTOTP(secKey)
return otp, label, err
}
func (s *Action) otpWriteQRFile(ctx context.Context, otp twofactor.OTP, label, file string) error {
var qr []byte
var err error
switch otp.Type() {
case twofactor.OATH_HOTP:
hotp := otp.(*twofactor.HOTP)
qr, err = hotp.QR(label)
case twofactor.OATH_TOTP:
totp := otp.(*twofactor.TOTP)
qr, err = totp.QR(label)
default:
err = errors.New("QR codes can only be generated for OATH OTPs")
}
if err != nil {
return exitError(ctx, ExitIO, err, "%s", err)
}
if err := ioutil.WriteFile(file, qr, 0600); err != nil {
return exitError(ctx, ExitIO, err, "failed to write QR code: %s", err)
}
return nil
}