diff --git a/clip/totp.go b/clip/totp.go index 8ef5c77..7b42eb4 100644 --- a/clip/totp.go +++ b/clip/totp.go @@ -3,39 +3,17 @@ package clip import ( - "errors" - "strings" "time" - "github.com/pquerna/otp" - "github.com/pquerna/otp/totp" + "github.com/rwinkhart/libmutton/core" ) -// GenTOTP generates a TOTP token from a secret. -// Prefix `secret` with "steam@" for Steam TOTP format. -func GenTOTP(secret string, time time.Time) (string, error) { - var totpToken string - var err error - - if strings.HasPrefix(secret, "steam@") { - totpToken, err = totp.GenerateCodeCustom(secret[6:], time, totp.ValidateOpts{Period: 30, Digits: 5, Encoder: otp.EncoderSteam}) - } else { - totpToken, err = totp.GenerateCode(secret, time) - } - - if err != nil { - return "", errors.New("unable to generate TOTP token: " + err.Error()) - } - - return totpToken, nil -} - // TOTPCopier is meant to be run as a goroutine to keep // the clipboard up-to-date with the latest TOTP token. func TOTPCopier(secret string, errorChan chan<- error, done <-chan bool) { for { currentTime := time.Now() - token, err := GenTOTP(secret, currentTime) + token, err := core.GenTOTP(secret, currentTime) if err != nil { errorChan <- err } diff --git a/core/util.go b/core/util.go index 56daca7..981a1ba 100644 --- a/core/util.go +++ b/core/util.go @@ -4,7 +4,10 @@ import ( "errors" "os" "strings" + "time" + "github.com/pquerna/otp" + "github.com/pquerna/otp/totp" "github.com/rwinkhart/go-boilerplate/back" "github.com/rwinkhart/libmutton/crypt" "github.com/rwinkhart/libmutton/global" @@ -178,3 +181,22 @@ func EntryIsNotEmpty(entryData []string) bool { } return false } + +// GenTOTP generates a TOTP token from a secret. +// Prefix `secret` with "steam@" for Steam TOTP format. +func GenTOTP(secret string, time time.Time) (string, error) { + var totpToken string + var err error + + if strings.HasPrefix(secret, "steam@") { + totpToken, err = totp.GenerateCodeCustom(secret[6:], time, totp.ValidateOpts{Period: 30, Digits: 5, Encoder: otp.EncoderSteam}) + } else { + totpToken, err = totp.GenerateCode(secret, time) + } + + if err != nil { + return "", errors.New("unable to generate TOTP token: " + err.Error()) + } + + return totpToken, nil +}