mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-28 04:46:42 -04:00
Move GenTOTP() to core package
This commit is contained in:
+2
-24
@@ -3,39 +3,17 @@
|
|||||||
package clip
|
package clip
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pquerna/otp"
|
"github.com/rwinkhart/libmutton/core"
|
||||||
"github.com/pquerna/otp/totp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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
|
// TOTPCopier is meant to be run as a goroutine to keep
|
||||||
// the clipboard up-to-date with the latest TOTP token.
|
// the clipboard up-to-date with the latest TOTP token.
|
||||||
func TOTPCopier(secret string, errorChan chan<- error, done <-chan bool) {
|
func TOTPCopier(secret string, errorChan chan<- error, done <-chan bool) {
|
||||||
for {
|
for {
|
||||||
currentTime := time.Now()
|
currentTime := time.Now()
|
||||||
token, err := GenTOTP(secret, currentTime)
|
token, err := core.GenTOTP(secret, currentTime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorChan <- err
|
errorChan <- err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,10 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pquerna/otp"
|
||||||
|
"github.com/pquerna/otp/totp"
|
||||||
"github.com/rwinkhart/go-boilerplate/back"
|
"github.com/rwinkhart/go-boilerplate/back"
|
||||||
"github.com/rwinkhart/libmutton/crypt"
|
"github.com/rwinkhart/libmutton/crypt"
|
||||||
"github.com/rwinkhart/libmutton/global"
|
"github.com/rwinkhart/libmutton/global"
|
||||||
@@ -178,3 +181,22 @@ func EntryIsNotEmpty(entryData []string) bool {
|
|||||||
}
|
}
|
||||||
return false
|
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