commit 897b722d12dfa8dbffd0f6d57bb7686c0f1d6fc7
parent 4e8eab16f5f040d09a5b5ccd0849e1b449fe54bb
Author: Sean Enck <sean@ttypty.com>
Date: Sat, 16 Jul 2022 12:16:31 -0400
totp should work fully again
Diffstat:
2 files changed, 18 insertions(+), 23 deletions(-)
diff --git a/cmd/lb-totp/main.go b/cmd/lb-totp/main.go
@@ -31,11 +31,11 @@ func list() ([]string, error) {
if err != nil {
return nil, err
}
- token := totpToken()
+ token := totpToken(f, true)
var results []string
for _, obj := range files {
if filepath.Base(obj) == token {
- results = append(results, obj)
+ results = append(results, filepath.Dir(f.CleanPath(obj)))
}
}
if len(results) == 0 {
@@ -52,12 +52,15 @@ func clear() {
}
}
-func totpToken() string {
+func totpToken(f store.FileSystem, extension bool) string {
t := os.Getenv("LOCKBOX_TOTP")
if t == "" {
t = "totp"
}
- return t
+ if !extension {
+ return t
+ }
+ return f.NewFile(t)
}
func display(token string, args cli.Arguments) error {
@@ -75,11 +78,9 @@ func display(token string, args cli.Arguments) error {
if err != nil {
return err
}
- tok := strings.TrimSpace(token)
- if !strings.HasSuffix(tok, totpToken()) {
- return errors.New("does not look like a totp token path")
- }
- pathing := store.NewFileSystemStore().NewPath(tok)
+ f := store.NewFileSystemStore()
+ tok := filepath.Join(strings.TrimSpace(token), totpToken(f, false))
+ pathing := f.NewPath(tok)
if !misc.PathExists(pathing) {
return errors.New("object does not exist")
}
diff --git a/internal/store/filesystem.go b/internal/store/filesystem.go
@@ -44,9 +44,7 @@ func (s FileSystem) List(options ViewOptions) ([]string, error) {
if strings.HasSuffix(path, extension) {
usePath := path
if options.Display {
- usePath = strings.TrimPrefix(usePath, s.path)
- usePath = strings.TrimPrefix(usePath, string(os.PathSeparator))
- usePath = strings.TrimSuffix(usePath, extension)
+ usePath = s.trim(usePath)
}
results = append(results, usePath)
}
@@ -74,15 +72,11 @@ func (s FileSystem) NewFile(file string) string {
// CleanPath will clean store and extension information from an entry.
func (s FileSystem) CleanPath(fullPath string) string {
- fileName := fullPath
- if strings.HasPrefix(fullPath, s.path) {
- fileName = fileName[len(s.path):]
- }
- if fileName[0] == os.PathSeparator {
- fileName = fileName[1:]
- }
- if strings.HasSuffix(fileName, extension) {
- fileName = fileName[0 : len(fileName)-len(extension)]
- }
- return fileName
+ return s.trim(fullPath)
+}
+
+func (s FileSystem) trim(path string) string {
+ f := strings.TrimPrefix(path, s.path)
+ f = strings.TrimPrefix(f, string(os.PathSeparator))
+ return strings.TrimSuffix(f, extension)
}