-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
otp.go
223 lines (193 loc) · 5.14 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package crypto
import (
"crypto/sha1"
"encoding/base32"
"net/url"
"strconv"
"strings"
"time"
"github.com/Laisky/errors/v2"
"github.com/xlzd/gotp"
gutils "github.com/Laisky/go-utils/v4"
)
// OTPType otp type
type OTPType string
const (
// OTPTypeTOTP time-based otp
OTPTypeTOTP OTPType = "totp"
// OTPTypeHOTP hash-based otp
OTPTypeHOTP OTPType = "hotp"
)
// OTPAlgorithm hash algorithm for otp
type OTPAlgorithm string
const (
// OTPAlgorithmSHA1 sha1
OTPAlgorithmSHA1 OTPAlgorithm = "sha1"
)
// Base32Secret generate base32 encoded secret
func Base32Secret(key []byte) string {
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(key)
}
// OTPArgs arguments for OTP
type OTPArgs struct {
// OtpType (readonly) otp type, must in totp/hotp
OtpType OTPType
// Base32Secret (required) the hotp/totp secret used to generate the URI
//
// genrate Base32Secret:
//
// Base32Secret([]byte("xxxxxx"))
Base32Secret string
// AccountName (optional) name of the account
AccountName string
// Authenticator (optional) the name of the OTP issuer;
// this will be the organization title of the OTP entry in Authenticator
IssuerName string
// Algorithm (optional) the algorithm used in the OTP generation
//
// default to sha1
Algorithm OTPAlgorithm
// InitialCount (optional) starting counter value. Only works for hotp
InitialCount int
// Digits (optional) the length of the OTP generated code.
//
// default to 6
Digits uint
// PeriodSecs (optional) the number of seconds the OTP
// generator is set to expire every code.
//
// default to 30
PeriodSecs uint
}
// Hasher get hasher from argument
func (a OTPArgs) Hasher() (*gotp.Hasher, error) {
switch strings.ToLower(string(a.Algorithm)) {
case "", string(OTPAlgorithmSHA1):
return &gotp.Hasher{
HashName: string(OTPAlgorithmSHA1),
Digest: sha1.New,
}, nil
default:
return nil, errors.Errorf("unsupport hasher %q", a.Algorithm)
}
}
// TOTP time-based OTP
type TOTP struct {
arg OTPArgs
engine *gotp.TOTP
}
// TOTPInterface interface for TOTP
type TOTPInterface interface {
// Key generate key by totp
Key() string
// KeyAt generate key by totp at arbitraty time
KeyAt(at time.Time) string
// URI build uri for otp arguments
URI() string
}
// NewTOTP new TOTP
func NewTOTP(arg OTPArgs) (*TOTP, error) {
arg.OtpType = OTPTypeTOTP
if len(arg.Base32Secret) == 0 {
return nil, errors.Errorf("secret shoule not be empty")
}
arg.Algorithm = gutils.OptionalVal(&arg.Algorithm, OTPAlgorithmSHA1)
arg.Digits = gutils.OptionalVal(&arg.Digits, 6)
arg.PeriodSecs = gutils.OptionalVal(&arg.PeriodSecs, 30)
hasher, err := arg.Hasher()
if err != nil {
return nil, err
}
return &TOTP{
engine: gotp.NewTOTP(
arg.Base32Secret,
int(arg.Digits),
int(arg.PeriodSecs),
hasher,
),
arg: arg,
}, nil
}
// Key generate key by totp
func (t *TOTP) Key() string {
return t.engine.Now()
}
// KeyAt generate key by totp at arbitraty time
func (t *TOTP) KeyAt(at time.Time) string {
return t.engine.AtTime(at)
}
// URI build uri for otp arguments
func (t *TOTP) URI() string {
return gotp.BuildUri(
string(t.arg.OtpType),
t.arg.Base32Secret,
t.arg.AccountName,
t.arg.IssuerName,
string(t.arg.Algorithm),
t.arg.InitialCount,
int(t.arg.Digits),
int(t.arg.PeriodSecs),
)
}
// ParseOTPUri parse otp uri to otp arguments
//
// # Args
//
// - uri: like `otpauth://totp/issuerName:demoAccountName?secret=4S62BZNFXXSZLCRO&issuer=issuerName`
func ParseOTPUri(uri string) (arg OTPArgs, err error) {
parsedURL, err := url.Parse(uri)
if err != nil {
return arg, errors.Wrap(err, "parse uri")
}
arg.OtpType = OTPType(parsedURL.Host)
arg.AccountName, err = url.PathUnescape(parsedURL.Path)
if err != nil {
return arg, errors.Wrapf(err, "unescape path %q", parsedURL.Path)
}
if vs := strings.SplitN(arg.AccountName, ":", 2); len(vs) == 2 {
arg.IssuerName = strings.TrimPrefix(vs[0], "/")
arg.AccountName = vs[1]
}
arg.Base32Secret, err = url.QueryUnescape(parsedURL.Query().Get("secret"))
if err != nil {
return arg, errors.Wrap(err, "unescape secret")
}
digit, err := url.QueryUnescape(parsedURL.Query().Get("digit"))
if err != nil {
return arg, errors.Wrap(err, "unescape digit")
}
if gutils.Contains([]string{"0", "6", ""}, digit) {
arg.Digits = 6
} else {
v, err := strconv.Atoi(digit)
if err != nil {
return arg, errors.Wrapf(err, "parse digit %q", digit)
}
arg.Digits = uint(v)
}
period, err := url.QueryUnescape(parsedURL.Query().Get("period"))
if err != nil {
return arg, errors.Wrap(err, "unescape period")
}
if gutils.Contains([]string{"0", "30", ""}, period) {
arg.PeriodSecs = 30
} else {
v, err := strconv.Atoi(period)
if err != nil {
return arg, errors.Wrapf(err, "parse period %q", period)
}
arg.PeriodSecs = uint(v)
}
if arg.OtpType == "hotp" {
counter, err := url.QueryUnescape(parsedURL.Query().Get("counter"))
if err != nil {
return arg, errors.Wrap(err, "unescape counter")
}
arg.InitialCount, err = strconv.Atoi(counter)
if err != nil {
return arg, errors.Wrapf(err, "parse counter %q", counter)
}
}
arg.Algorithm = gutils.OptionalVal(&arg.Algorithm, OTPAlgorithmSHA1)
return arg, nil
}