-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
90 lines (78 loc) · 2.55 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
package gotp
import (
"fmt"
"math/rand"
"net/url"
"strings"
"time"
)
const (
OtpTypeTotp = "totp"
OtpTypeHotp = "hotp"
)
/*
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, "&"))
}
// get current timestamp
func currentTimestamp() int {
return int(time.Now().Unix())
}
// 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
}
// generate a random secret of given length
func RandomSecret(length int) string {
rand.Seed(time.Now().UnixNano())
letterRunes := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")
bytes := make([]rune, length)
for i := range bytes {
bytes[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(bytes)
}