aboutsummaryrefslogtreecommitdiff
path: root/provider_aegis.go
diff options
context:
space:
mode:
Diffstat (limited to 'provider_aegis.go')
-rw-r--r--provider_aegis.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/provider_aegis.go b/provider_aegis.go
new file mode 100644
index 0000000..040da0a
--- /dev/null
+++ b/provider_aegis.go
@@ -0,0 +1,58 @@
+// Copyright 2021, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package gotp
+
+import (
+ "bytes"
+ "fmt"
+ "net/url"
+ "os"
+ "strconv"
+)
+
+func parseProviderAegis(file string) (issuers []*Issuer, err error) {
+ logp := "parseProviderAegis"
+
+ b, err := os.ReadFile(file)
+ if err != nil {
+ return nil, fmt.Errorf("%s: %w", logp, err)
+ }
+
+ lines := bytes.Split(b, []byte("\n"))
+ for x, line := range lines {
+ u, err := url.Parse(string(line))
+ if err != nil {
+ return nil, fmt.Errorf("%s: line %d: invalid format %q", logp, x, line)
+ }
+ if u.Host != "totp" {
+ continue
+ }
+
+ q := u.Query()
+ issuer := &Issuer{
+ Label: normalizeLabel(u.Path[1:]),
+ Hash: q.Get("algorithm"),
+ Secret: q.Get("secret"),
+ Name: q.Get("issuer"),
+ }
+
+ val := q.Get("digits")
+ issuer.Digits, err = strconv.Atoi(val)
+ if err != nil {
+ return nil, fmt.Errorf("%s: line %d: invalid digits %q",
+ logp, x, val)
+ }
+
+ val = q.Get("period")
+ issuer.TimeStep, err = strconv.Atoi(val)
+ if err != nil {
+ return nil, fmt.Errorf("%s: line %d: invalid period %q",
+ logp, x, val)
+ }
+
+ issuers = append(issuers, issuer)
+ }
+ return issuers, nil
+}