-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotp.go
117 lines (100 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
package otp
import (
"time"
xtp "github.com/pquerna/otp"
"github.com/pquerna/otp/hotp"
"github.com/pquerna/otp/totp"
)
// Opts provides options for ValidateCustom().
//
// Only for TOTP: Period, Skew.
type Opts struct {
// Number of seconds a TOTP hash is valid for. Defaults to 30 seconds.
Period uint
// Periods before or after the current time to allow. Value of 1 allows up to Period
// of either side of the specified time. Defaults to 0 allowed skews. Values greater
// than 1 are likely sketchy.
Skew uint
// Digits as part of the input. Defaults to 6.
Digits xtp.Digits
// Algorithm to use for HMAC. Defaults to SHA1.
Algorithm xtp.Algorithm
}
func (opts *Opts) GetPeriod() uint {
if opts == nil || opts.Period == 0 {
return 30
}
return opts.Period
}
func (opts *Opts) GetSkew() uint {
if opts == nil {
return 0
}
return opts.Skew
}
func (opts *Opts) GetDigits() xtp.Digits {
if opts == nil || opts.Digits == 0 {
return xtp.DigitsSix
}
return opts.Digits
}
func (opts *Opts) GetAlgorithm() xtp.Algorithm {
if opts == nil {
return xtp.AlgorithmSHA1
}
return opts.Algorithm
}
// Code generates the totp code, with the default settings: digits=6, algorithm=SHA1, base now timestamp.
func Code(secret string) string {
return TOTPCode(secret)
}
// CodeCustom generates the totp code, with the default settings: digits=6, algorithm=SHA1, with your specified timestamp.
func CodeCustom(secret string, t time.Time) string {
return TOTPCodeCustom(secret, t, nil)
}
func TOTPCode(secret string) (code string) {
code, _ = totp.GenerateCode(secret, time.Now())
return
}
func TOTPCodeCustom(secret string, t time.Time, opts *Opts) (code string) {
code, _ = totp.GenerateCodeCustom(secret, t, totp.ValidateOpts{
Period: opts.GetPeriod(),
Skew: opts.GetSkew(),
Digits: opts.GetDigits(),
Algorithm: opts.GetAlgorithm(),
})
return
}
func HOTPCode(secret string, counter uint64) (code string) {
code, _ = hotp.GenerateCode(secret, counter)
return
}
func HOTPCodeCustom(secret string, counter uint64, opts *Opts) (code string) {
code, _ = hotp.GenerateCodeCustom(secret, counter, hotp.ValidateOpts{
Digits: opts.GetDigits(),
Algorithm: opts.GetAlgorithm(),
})
return
}
func VerifyTOTP(passcode string, secret string) bool {
return totp.Validate(passcode, secret)
}
func VerifyTOTPCustom(passcode string, secret string, t time.Time, opts *Opts) (ret bool) {
ret, _ = totp.ValidateCustom(passcode, secret, t, totp.ValidateOpts{
Period: opts.GetPeriod(),
Skew: opts.GetSkew(),
Digits: opts.GetDigits(),
Algorithm: opts.GetAlgorithm(),
})
return
}
func VerifyHOTP(passcode string, counter uint64, secret string) bool {
return hotp.Validate(passcode, counter, secret)
}
func VerifyHOTPCustom(passcode string, counter uint64, secret string, opts *Opts) (ret bool) {
ret, _ = hotp.ValidateCustom(passcode, counter, secret, hotp.ValidateOpts{
Digits: opts.GetDigits(),
Algorithm: opts.GetAlgorithm(),
})
return
}