Skip to content

Commit

Permalink
fix: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
jtbonhomme committed Feb 10, 2021
1 parent e91cbee commit 5fb8cdd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 100 deletions.
96 changes: 0 additions & 96 deletions backend/random/random.go

This file was deleted.

4 changes: 3 additions & 1 deletion cmd/gotp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func main() {
case "get":
err := getCmd.Parse(os.Args[2:])
check(err)
totp, err := gotp.Get(*getKey)
totp, err := gotp.
WithTimeIntervalSeed(30).
Get(*getKey)
check(err)
fmt.Printf("%s", totp)
err = clipboard.WriteAll(totp)
Expand Down
17 changes: 14 additions & 3 deletions gotp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,30 @@ import (
)

// DefaultTimeIntervalSeedTimeIntervalSeed is the default time interval seed used to compute the HOTP.
const DefaultTimeIntervalSeed int64 = 30
const DefaultTimeIntervalSeed uint = 30

type GOTP struct {
secRing *secure.Secure
timeIntervalSeed uint
}

// New instanciates a GoTP object with a secured backend
func New(secring *secure.Secure) *GOTP {
// create the key ring
return &GOTP{
timeIntervalSeed: DefaultTimeIntervalSeed,
secRing: secring,
}
}

// 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.secRing.List()
Expand All @@ -43,7 +54,7 @@ func (gotp *GOTP) Get(key string) (string, error) {
return "", err
}
return totp.GenerateCodeCustom(string(secret), time.Now(), totp.ValidateOpts{
Period: 30,
Period: gotp.timeIntervalSeed,
Skew: 1,
Digits: otp.DigitsSix,
Algorithm: otp.AlgorithmSHA1,
Expand Down

0 comments on commit 5fb8cdd

Please sign in to comment.