-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
48 lines (40 loc) · 979 Bytes
/
common.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
package totp
import (
"strconv"
"time"
"github.com/pquerna/otp/totp"
)
// StrToUint converts a string to an unsigned integer. If the string is not a
// valid integer or out of range of int32, it returns 0.
func StrToUint(number string) uint {
const (
base10 = 10
bitSize = 32
)
u, err := strconv.ParseUint(number, base10, bitSize)
if err != nil {
return 0
}
return uint(u)
}
// Validate returns true if the given passcode is valid for the secret and options.
//
// The passcode should be a string of 6 or 8 digit number and the secret should
// be a base32 encoded string.
func Validate(passcode, secret string, options Options) bool {
isValid, err := totp.ValidateCustom(
passcode,
secret,
time.Now().UTC(),
totp.ValidateOpts{
Period: options.Period,
Skew: options.Skew,
Digits: options.Digits.OTPDigits(),
Algorithm: options.Algorithm.OTPAlgorithm(),
},
)
if !isValid || err != nil {
return false
}
return true
}