commit b9d3cdbdee5f32fbf2d51f22cb31636b22e70b3f
parent 0d2802cc6ad9e24df235ada92864fe7bc4df809d
Author: Sean Enck <sean@ttypty.com>
Date: Tue, 25 Jul 2023 20:35:43 -0400
merging these definitions together
Diffstat:
4 files changed, 48 insertions(+), 66 deletions(-)
diff --git a/internal/inputs/totp.go b/internal/inputs/totp.go
@@ -1,37 +0,0 @@
-// Package inputs handles user inputs/UI elements.
-package inputs
-
-import (
- "fmt"
- "net/url"
- "strings"
-)
-
-const (
- otpAuth = "otpauth"
- otpIssuer = "lbissuer"
-)
-
-// FormatTOTP will format a totp otpauth url
-func FormatTOTP(value string) string {
- if strings.HasPrefix(value, otpAuth) {
- return value
- }
- override := EnvironOrDefault(formatTOTPEnv, "")
- if override != "" {
- return fmt.Sprintf(override, value)
- }
- v := url.Values{}
- v.Set("secret", value)
- v.Set("issuer", otpIssuer)
- v.Set("period", "30")
- v.Set("algorithm", "SHA1")
- v.Set("digits", "6")
- u := url.URL{
- Scheme: otpAuth,
- Host: "totp",
- Path: "/" + otpIssuer + ":" + "lbaccount",
- RawQuery: v.Encode(),
- }
- return u.String()
-}
diff --git a/internal/inputs/totp_test.go b/internal/inputs/totp_test.go
@@ -1,29 +0,0 @@
-package inputs_test
-
-import (
- "os"
- "testing"
-
- "github.com/enckse/lockbox/internal/inputs"
-)
-
-func TestFormatTOTP(t *testing.T) {
- otp := inputs.FormatTOTP("otpauth://abc")
- if otp != "otpauth://abc" {
- t.Errorf("invalid totp token: %s", otp)
- }
- otp = inputs.FormatTOTP("abc")
- if otp != "otpauth://totp/lbissuer:lbaccount?algorithm=SHA1&digits=6&issuer=lbissuer&period=30&secret=abc" {
- t.Errorf("invalid totp token: %s", otp)
- }
- os.Setenv("LOCKBOX_TOTP_FORMAT", "test/%s")
- otp = inputs.FormatTOTP("abc")
- if otp != "test/abc" {
- t.Errorf("invalid totp token: %s", otp)
- }
- os.Setenv("LOCKBOX_TOTP_FORMAT", "")
- otp = inputs.FormatTOTP("abc")
- if otp != "otpauth://totp/lbissuer:lbaccount?algorithm=SHA1&digits=6&issuer=lbissuer&period=30&secret=abc" {
- t.Errorf("invalid totp token: %s", otp)
- }
-}
diff --git a/internal/inputs/vars.go b/internal/inputs/vars.go
@@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
+ "net/url"
"os"
"os/exec"
"sort"
@@ -13,6 +14,8 @@ import (
)
const (
+ otpAuth = "otpauth"
+ otpIssuer = "lbissuer"
prefixKey = "LOCKBOX_"
noClipEnv = prefixKey + "NOCLIP"
noColorEnv = prefixKey + "NOCOLOR"
@@ -233,3 +236,27 @@ func ListEnvironmentVariables(showValues bool) []string {
results = append(results, e.formatEnvironmentVariable(false, hashJSONLengthEnv, fmt.Sprintf("%d", defaultHashLength), fmt.Sprintf("maximum hash length the JSON output should contain\nwhen '%s' mode is set for JSON output", JSONDataOutputHash), []string{"integer"}))
return results
}
+
+// FormatTOTP will format a totp otpauth url
+func FormatTOTP(value string) string {
+ if strings.HasPrefix(value, otpAuth) {
+ return value
+ }
+ override := EnvironOrDefault(formatTOTPEnv, "")
+ if override != "" {
+ return fmt.Sprintf(override, value)
+ }
+ v := url.Values{}
+ v.Set("secret", value)
+ v.Set("issuer", otpIssuer)
+ v.Set("period", "30")
+ v.Set("algorithm", "SHA1")
+ v.Set("digits", "6")
+ u := url.URL{
+ Scheme: otpAuth,
+ Host: "totp",
+ Path: "/" + otpIssuer + ":" + "lbaccount",
+ RawQuery: v.Encode(),
+ }
+ return u.String()
+}
diff --git a/internal/inputs/vars_test.go b/internal/inputs/vars_test.go
@@ -198,3 +198,24 @@ func TestGetHashLength(t *testing.T) {
t.Errorf("invalid err: %v", err)
}
}
+
+func TestFormatTOTP(t *testing.T) {
+ otp := inputs.FormatTOTP("otpauth://abc")
+ if otp != "otpauth://abc" {
+ t.Errorf("invalid totp token: %s", otp)
+ }
+ otp = inputs.FormatTOTP("abc")
+ if otp != "otpauth://totp/lbissuer:lbaccount?algorithm=SHA1&digits=6&issuer=lbissuer&period=30&secret=abc" {
+ t.Errorf("invalid totp token: %s", otp)
+ }
+ os.Setenv("LOCKBOX_TOTP_FORMAT", "test/%s")
+ otp = inputs.FormatTOTP("abc")
+ if otp != "test/abc" {
+ t.Errorf("invalid totp token: %s", otp)
+ }
+ os.Setenv("LOCKBOX_TOTP_FORMAT", "")
+ otp = inputs.FormatTOTP("abc")
+ if otp != "otpauth://totp/lbissuer:lbaccount?algorithm=SHA1&digits=6&issuer=lbissuer&period=30&secret=abc" {
+ t.Errorf("invalid totp token: %s", otp)
+ }
+}