diff options
Diffstat (limited to 'gotp.go')
| -rw-r--r-- | gotp.go | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -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 +} |
