summaryrefslogtreecommitdiff
path: root/gotp.go
diff options
context:
space:
mode:
Diffstat (limited to 'gotp.go')
-rw-r--r--gotp.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/gotp.go b/gotp.go
index 3c597d0..e913b8d 100644
--- a/gotp.go
+++ b/gotp.go
@@ -5,7 +5,9 @@
package gotp
import (
+ "fmt"
"io"
+ "net/url"
"strings"
"time"
"unicode"
@@ -29,6 +31,11 @@ const (
providerNameAegis = `aegis`
)
+// List of known format for export.
+const (
+ formatNameURI = `uri`
+)
+
// Version define the latest version of this module and gotp CLI.
var Version = `0.5.0`
@@ -60,3 +67,38 @@ func normalizeLabel(in string) (out string) {
}
return strings.ToLower(buf.String())
}
+
+// exportAsURI export the list of issuers using [URI] format.
+//
+// [URI]: https://github.com/google/google-authenticator/wiki/Key-Uri-Format
+func exportAsURI(w io.Writer, issuers []*Issuer) (err error) {
+ var (
+ logp = `exportAsURI`
+ issuer *Issuer
+ )
+
+ for _, issuer = range issuers {
+ var q = url.Values{}
+
+ q.Set(`algorithm`, issuer.Hash)
+ q.Set(`secret`, issuer.Secret)
+ if len(issuer.Name) == 0 {
+ q.Set(`issuer`, issuer.Label)
+ } else {
+ q.Set(`issuer`, issuer.Name)
+ }
+
+ var otpauth = url.URL{
+ Scheme: `otpauth`,
+ Host: `totp`,
+ Path: issuer.Label,
+ RawQuery: q.Encode(),
+ }
+
+ _, err = w.Write([]byte(otpauth.String() + "\n"))
+ if err != nil {
+ return fmt.Errorf(`%s: %w`, logp, err)
+ }
+ }
+ return nil
+}