Move GenTOTP() to core package

This commit is contained in:
2025-11-13 23:46:21 -05:00
parent 04e1b0dd37
commit d2a6ed5eaa
2 changed files with 24 additions and 24 deletions
+2 -24
View File
@@ -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
}
+22
View File
@@ -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
}