-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp.go
175 lines (153 loc) · 3.95 KB
/
otp.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package otp
import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base32"
"errors"
"fmt"
"hash"
"net/url"
"strconv"
"strings"
)
var (
ErrUnsupportedAlgorithm = errors.New("unsupported algorithm")
ErrNoDigits = errors.New("required digits not set")
ErrEmptyIssuer = errors.New("empty issuer")
ErrPeriodNotValid = errors.New("period is not valid")
ErrSkewNotValid = errors.New("skew is not valid")
ErrCodeLengthMismatch = errors.New("code length mismatch")
ErrCodeIsNotValid = errors.New("code is not valid")
ErrEncodingNotValid = errors.New("encoding is not valid")
)
// Algorithm represents the hashing function to use for OTP.
type Algorithm uint
const (
AlgorithmUnknown Algorithm = 0
AlgorithmSHA1 Algorithm = 1
AlgorithmSHA256 Algorithm = 2
AlgorithmSHA512 Algorithm = 3
algorithmMax Algorithm = 4
)
func (a Algorithm) String() string {
switch a {
case AlgorithmUnknown:
return ""
case AlgorithmSHA1:
return "SHA1"
case AlgorithmSHA256:
return "SHA256"
case AlgorithmSHA512:
return "SHA512"
default:
panic(fmt.Sprintf("otp: unsupported algorithm: %d", int(a)))
}
}
func (a Algorithm) Hash() hash.Hash {
switch a {
case AlgorithmUnknown:
return nil
case AlgorithmSHA1:
return sha1.New()
case AlgorithmSHA256:
return sha256.New()
case AlgorithmSHA512:
return sha512.New()
default:
panic(fmt.Sprintf("otp: unsupported algorithm: %d", int(a)))
}
}
// Key represents an HTOP or TOTP key.
type Key struct {
url *url.URL
values url.Values
}
// ParseKeyFromURL creates a new Key from the HOTP or TOTP URL.
// See: https://github.com/google/google-authenticator/wiki/Key-Uri-Format
func ParseKeyFromURL(s string) (*Key, error) {
u, err := url.Parse(s)
if err != nil {
return nil, errors.New("otp: invalid otpauth URL")
}
key := &Key{
url: u,
values: u.Query(),
}
return key, nil
}
func (k *Key) String() string { return k.url.String() }
// Type returns "hotp" or "totp".
func (k *Key) Type() string { return k.url.Host }
// Secret returns the opaque secret for this Key.
func (k *Key) Secret() string { return k.values.Get("secret") }
// Issuer returns the name of the issuing organization.
func (k *Key) Issuer() string {
issuer := k.values.Get("issuer")
if issuer != "" {
return issuer
}
p := strings.TrimPrefix(k.url.Path, "/")
if i := strings.Index(p, ":"); i != -1 {
return p[:i]
}
return ""
}
// Account returns the name of the user's account.
func (k *Key) Account() string {
p := strings.TrimPrefix(k.url.Path, "/")
if i := strings.Index(p, ":"); i != -1 {
return p[i+1:]
}
return p
}
// Period returns a tiny int representing the rotation time in seconds.
func (k *Key) Period() uint64 {
period := k.values.Get("period")
u, err := strconv.ParseUint(period, 10, 64)
if err == nil {
return u
}
return 30 // If no period is defined 30 seconds is the default per (RFC 6238)
}
// Digits returns the digits value.
func (k *Key) Digits() uint {
if digits := k.values.Get("digits"); digits != "" {
if val, err := strconv.ParseUint(digits, 10, 32); err == nil {
return uint(val)
}
}
return 0
}
// Counter returns the counter value.
func (k *Key) Counter() uint64 {
if counter := k.values.Get("counter"); counter != "" {
if val, err := strconv.ParseUint(counter, 10, 64); err == nil {
return val
}
}
return 0
}
// Algorithm returns the algorithm type.
func (k *Key) Algorithm() Algorithm {
if algorithm := k.values.Get("algorithm"); algorithm != "" {
switch strings.ToUpper(algorithm) {
case "SHA1":
return AlgorithmSHA1
case "SHA256":
return AlgorithmSHA256
case "SHA512":
return AlgorithmSHA512
}
}
return AlgorithmUnknown
}
func b32Dec(s string) ([]byte, error) {
return base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(s)
}
func b32Enc(src []byte) string {
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(src)
}
func atoi(v uint64) string {
return strconv.FormatUint(v, 10)
}