-
Notifications
You must be signed in to change notification settings - Fork 3
/
gotp.go
63 lines (52 loc) · 1.49 KB
/
gotp.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
package gotp
import (
"time"
"github.com/jtbonhomme/gotp/backend"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
)
// DefaultTimeIntervalSeedTimeIntervalSeed is the default time interval seed used to compute the HOTP.
const DefaultTimeIntervalSeed uint = 30
type GOTP struct {
backend backend.Backend
timeIntervalSeed uint
}
// New instanciates a GoTP object with a secured backend
func New(bkd backend.Backend) *GOTP {
return &GOTP{
timeIntervalSeed: DefaultTimeIntervalSeed,
backend: bkd,
}
}
// WithTimeIntervalSeed configures a specific timeIntervalSeed
func (gotp *GOTP) WithTimeIntervalSeed(interval uint) *GOTP {
g := &GOTP{}
*g = *gotp
g.timeIntervalSeed = interval
return g
}
// List retrieves all existing keys in the secured key ring
func (gotp *GOTP) List() ([]string, error) {
return gotp.backend.List()
}
// Store creates a new key/value pair in the secured key ring
func (gotp *GOTP) Store(key, value string) error {
return gotp.backend.Store(key, value)
}
// Remove deletes a key in the secured key ring
func (gotp *GOTP) Remove(key string) error {
return gotp.backend.Remove(key)
}
// Get retrieves a key in the secured key ring
func (gotp *GOTP) Get(key string) (string, error) {
secret, err := gotp.backend.Read(key)
if err != nil {
return "", err
}
return totp.GenerateCodeCustom(string(secret), time.Now(), totp.ValidateOpts{
Period: gotp.timeIntervalSeed,
Skew: 1,
Digits: otp.DigitsSix,
Algorithm: otp.AlgorithmSHA1,
})
}