-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotpuri.go
60 lines (52 loc) · 1.13 KB
/
otpuri.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package frothy
import (
"fmt"
"net/url"
"strings"
)
type OTPType string
const (
OTPTypeTOTP = "totp"
OTPTypeHOTP = "hotp"
)
type OTPSecret struct {
UID string `cbor:"uid"`
Name string `cbor:"name"`
Secret string `cbor:"secret"`
Issuer string `cbor:"issuer"`
Type OTPType `cbor:"type"`
Counter string `cbor:"counter"`
}
func ParseOTPSecretFromURI(uri string) (*OTPSecret, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
if u.Scheme != "otpauth" {
return nil, fmt.Errorf("invalid scheme: %s", u.Scheme)
}
query := u.Query()
secret := query.Get("secret")
if secret == "" {
return nil, fmt.Errorf("secret not found")
}
var counter string
switch u.Host {
case OTPTypeTOTP:
// no special cases needed
case OTPTypeHOTP:
counter = query.Get("counter")
if counter == "" {
return nil, fmt.Errorf("htop counter not found but required")
}
default:
return nil, fmt.Errorf("unknown otp type: %s", u.Host)
}
return &OTPSecret{
Name: strings.TrimPrefix(u.Path, "/"),
Secret: secret,
Issuer: query.Get("issuer"),
Counter: counter,
Type: OTPType(u.Host),
}, nil
}