From 66bacad2aaf8460aabdc0047d040e9999be94888 Mon Sep 17 00:00:00 2001 From: Alex Zorin Date: Tue, 30 Jul 2019 06:48:06 +1000 Subject: [PATCH] Add fallbacks for token name if OriginalName unset Will use Name if it is available, and otherwise will use UniqueID, which is not descriptive but should always be available. Should fix one of the complaints reported in #1. --- cmd/authy-export/authy-export.go | 4 ++-- objects.go | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cmd/authy-export/authy-export.go b/cmd/authy-export/authy-export.go index b542138..8697480 100644 --- a/cmd/authy-export/authy-export.go +++ b/cmd/authy-export/authy-export.go @@ -70,7 +70,7 @@ func main() { for _, tok := range tokensResponse.AuthenticatorTokens { decrypted, err := tok.Decrypt(string(pp)) if err != nil { - log.Printf("Failed to decrypt token %s: %v", tok.OriginalName, err) + log.Printf("Failed to decrypt token %s: %v", tok.Description(), err) continue } @@ -80,7 +80,7 @@ func main() { u := url.URL{ Scheme: "otpauth", Host: "totp", - Path: tok.OriginalName, + Path: tok.Description(), RawQuery: params.Encode(), } fmt.Println(u.String()) diff --git a/objects.go b/objects.go index 42dc96e..eff004a 100644 --- a/objects.go +++ b/objects.go @@ -182,3 +182,15 @@ func (t AuthenticatorToken) Decrypt(passphrase string) (string, error) { } return strings.ToUpper(string(buf)), nil } + +// Description returns OriginalName if not empty, otherwise Name, +// otherwise `Token-{UniqueID}`. +func (t AuthenticatorToken) Description() string { + if t.OriginalName != "" { + return t.OriginalName + } + if t.Name != "" { + return t.Name + } + return "Token-" + t.UniqueID +}