diff options
| author | Shulhan <ms@kilabit.info> | 2024-06-22 16:35:47 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-06-22 17:21:56 +0700 |
| commit | dbe4f9722bbd63f685b36606abbfd97cbf1dfb10 (patch) | |
| tree | b1eb61d3f75aa60f65541ba51408d3d4a708b48d /gotp.go | |
| parent | 4eaf76a096a0ba8ba5e687ede23c1dafd9064358 (diff) | |
| download | gotp-dbe4f9722bbd63f685b36606abbfd97cbf1dfb10.tar.xz | |
all: implement command "export"
The "export" command export all issuers to file or standard output,
$ gotp export <FORMAT> [FILE]
List of known supported FORMAT is: uri.
If FILE is not defined it will print to standard output.
The list of exported issuers are printed in order of its label.
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 +} |
