refactor: don't export the generateOTP fn, extract a few constants, update command usages (#13)
This commit is contained in:
parent
90682c1ea8
commit
174e667b64
|
@ -9,7 +9,17 @@ import (
|
||||||
"github.com/pquerna/otp/totp"
|
"github.com/pquerna/otp/totp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateOTP(entry Entry, t time.Time) (string, int64, error) {
|
const (
|
||||||
|
typeTOTP = "TOTP"
|
||||||
|
algorithmSHA1 = "SHA1"
|
||||||
|
algorithmSHA256 = "SHA256"
|
||||||
|
algorithmSHA512 = "SHA512"
|
||||||
|
algorithmMD5 = "MD5"
|
||||||
|
digitsSix = 6
|
||||||
|
digitsEight = 8
|
||||||
|
)
|
||||||
|
|
||||||
|
func generateOTP(entry Entry, t time.Time) (string, int64, error) {
|
||||||
algorithm, err := parseAlgorithm(entry.Algorithm)
|
algorithm, err := parseAlgorithm(entry.Algorithm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", 0, fmt.Errorf("parseAlgorithm: %w", err)
|
return "", 0, fmt.Errorf("parseAlgorithm: %w", err)
|
||||||
|
@ -21,7 +31,7 @@ func GenerateOTP(entry Entry, t time.Time) (string, int64, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch strings.ToUpper(entry.Type) {
|
switch strings.ToUpper(entry.Type) {
|
||||||
case "TOTP":
|
case typeTOTP:
|
||||||
code, err := totp.GenerateCodeCustom(entry.Secret, t, totp.ValidateOpts{
|
code, err := totp.GenerateCodeCustom(entry.Secret, t, totp.ValidateOpts{
|
||||||
Algorithm: algorithm,
|
Algorithm: algorithm,
|
||||||
Period: uint(entry.Period),
|
Period: uint(entry.Period),
|
||||||
|
@ -39,13 +49,13 @@ func GenerateOTP(entry Entry, t time.Time) (string, int64, error) {
|
||||||
|
|
||||||
func parseAlgorithm(algorithm string) (otp.Algorithm, error) {
|
func parseAlgorithm(algorithm string) (otp.Algorithm, error) {
|
||||||
switch strings.ToUpper(algorithm) {
|
switch strings.ToUpper(algorithm) {
|
||||||
case "SHA1":
|
case algorithmSHA1:
|
||||||
return otp.AlgorithmSHA1, nil
|
return otp.AlgorithmSHA1, nil
|
||||||
case "SHA256":
|
case algorithmSHA256:
|
||||||
return otp.AlgorithmSHA256, nil
|
return otp.AlgorithmSHA256, nil
|
||||||
case "SHA512":
|
case algorithmSHA512:
|
||||||
return otp.AlgorithmSHA512, nil
|
return otp.AlgorithmSHA512, nil
|
||||||
case "MD5":
|
case algorithmMD5:
|
||||||
return otp.AlgorithmMD5, nil
|
return otp.AlgorithmMD5, nil
|
||||||
default:
|
default:
|
||||||
return 0, fmt.Errorf("unsupported algorithm: %s", algorithm)
|
return 0, fmt.Errorf("unsupported algorithm: %s", algorithm)
|
||||||
|
@ -54,9 +64,9 @@ func parseAlgorithm(algorithm string) (otp.Algorithm, error) {
|
||||||
|
|
||||||
func parseDigits(digits uint8) (otp.Digits, error) {
|
func parseDigits(digits uint8) (otp.Digits, error) {
|
||||||
switch digits {
|
switch digits {
|
||||||
case 6:
|
case digitsSix:
|
||||||
return otp.DigitsSix, nil
|
return otp.DigitsSix, nil
|
||||||
case 8:
|
case digitsEight:
|
||||||
return otp.DigitsEight, nil
|
return otp.DigitsEight, nil
|
||||||
default:
|
default:
|
||||||
return 0, fmt.Errorf("unsupported digits: %d", digits)
|
return 0, fmt.Errorf("unsupported digits: %d", digits)
|
||||||
|
|
|
@ -104,7 +104,7 @@ func buildItemTitle(issuer, label string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildItemDescription(e Entry, t time.Time) string {
|
func buildItemDescription(e Entry, t time.Time) string {
|
||||||
otp, remaining, err := GenerateOTP(e, t)
|
otp, remaining, err := generateOTP(e, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error()
|
return err.Error()
|
||||||
}
|
}
|
||||||
|
|
3
main.go
3
main.go
|
@ -34,6 +34,7 @@ func newApp() (*cli.App, error) {
|
||||||
|
|
||||||
return &cli.App{
|
return &cli.App{
|
||||||
Name: "gootp",
|
Name: "gootp",
|
||||||
|
Usage: "2FA App compatible with andOTP backup file format",
|
||||||
Version: Version,
|
Version: Version,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
var err error
|
var err error
|
||||||
|
@ -86,7 +87,7 @@ func newApp() (*cli.App, error) {
|
||||||
func newDecryptCommand() *cli.Command {
|
func newDecryptCommand() *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "decrypt",
|
Name: "decrypt",
|
||||||
Usage: "decrypt backup file generated by andotp",
|
Usage: "Decrypts backup file generated by andotp",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
var err error
|
var err error
|
||||||
password := []byte(c.String("password"))
|
password := []byte(c.String("password"))
|
||||||
|
|
Reference in New Issue