mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-27 20:36:29 -04:00
Move GenTOTP() to core package
This commit is contained in:
+2
-24
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user