-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
117 lines (101 loc) · 3.38 KB
/
utils.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
package gotp
import (
"encoding/base32"
"fmt"
"math/rand"
"net/url"
"strings"
"time"
)
const (
OtpTypeTotp = "totp"
OtpTypeHotp = "hotp"
)
var (
// slices of possible runes for secret creation.
digitRunes = []rune("0123456789")
lCaseRunes = []rune("abcdefghijklmnopqrstuvwxyz")
uCaseRunes = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
specialRunes = []rune("~=+%^*/()[]{}/!@#$?|")
// A collection of all runes which may be used in creating a new secret.
allRunes = [][]rune{
digitRunes,
lCaseRunes,
uCaseRunes,
specialRunes,
}
)
/*
BuildUri returns the provisioning URI for the OTP; works for either TOTP or HOTP.
This can then be encoded in a QR Code and used to provision the Google Authenticator app.
For module-internal use.
See also:
https://github.com/google/google-authenticator/wiki/Key-Uri-Format
params:
otpType: otp type, must in totp/hotp
secret: the hotp/totp secret used to generate the URI
accountName: name of the account
issuerName: the name of the OTP issuer; this will be the organization title of the OTP entry in Authenticator
algorithm: the algorithm used in the OTP generation
initialCount: starting counter value. Only works for hotp
digits: the length of the OTP generated code.
period: the number of seconds the OTP generator is set to expire every code.
returns: provisioning uri
*/
func BuildUri(otpType, secret, accountName, issuerName, algorithm string, initialCount, digits, period int) string {
if otpType != OtpTypeHotp && otpType != OtpTypeTotp {
panic("otp type error, got " + otpType)
}
urlParams := make([]string, 0)
urlParams = append(urlParams, "secret="+secret)
if otpType == OtpTypeHotp {
urlParams = append(urlParams, fmt.Sprintf("counter=%d", initialCount))
}
label := url.QueryEscape(accountName)
if issuerName != "" {
issuerNameEscape := url.QueryEscape(issuerName)
label = issuerNameEscape + ":" + label
urlParams = append(urlParams, "issuer="+issuerNameEscape)
}
if algorithm != "" && algorithm != "sha1" {
urlParams = append(urlParams, "algorithm="+strings.ToUpper(algorithm))
}
if digits != 0 && digits != 6 {
urlParams = append(urlParams, fmt.Sprintf("digits=%d", digits))
}
if period != 0 && period != 30 {
urlParams = append(urlParams, fmt.Sprintf("period=%d", period))
}
return fmt.Sprintf("otpauth://%s/%s?%s", otpType, label, strings.Join(urlParams, "&"))
}
// currentTimestamp returns the current timestamp.
func currentTimestamp() int {
return int(time.Now().Unix())
}
// Itob converts an integer to byte array
func Itob(integer int) []byte {
byteArr := make([]byte, 8)
for i := 7; i >= 0; i-- {
byteArr[i] = byte(integer & 0xff)
integer = integer >> 8
}
return byteArr
}
// RandomSecret generates a random secret of given length from all possible printable ASCII characters.
func RandomSecret(length int) string {
// secretRunes is all runes which may be used in a valid RandomSecret.
secretRunes := []rune{}
for _, r := range allRunes {
secretRunes = append(secretRunes, r...)
}
rand.Seed(time.Now().UnixNano())
bytes := make([]rune, length)
for i := range bytes {
bytes[i] = secretRunes[rand.Intn(len(secretRunes))]
}
return base32.StdEncoding.EncodeToString(bytes)
}
// UrlSafeRandomSecret generates a RandomSecret and returns it with url-safe encoding.
func UrlSafeRandomSecret(l int) string {
return url.QueryEscape(RandomSecret(l))
}