-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp.go
55 lines (44 loc) · 1.18 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
package utils
import (
"bytes"
"encoding/base32"
"image/png"
"net/url"
"github.com/gravitational/trace"
"github.com/pquerna/otp"
)
// GenerateQRCode takes in a OTP Key URL and returns a PNG-encoded QR code.
func GenerateQRCode(u string) ([]byte, error) {
otpKey, err := otp.NewKeyFromURL(u)
if err != nil {
return nil, trace.Wrap(err)
}
otpImage, err := otpKey.Image(450, 450)
if err != nil {
return nil, trace.Wrap(err)
}
var otpQR bytes.Buffer
err = png.Encode(&otpQR, otpImage)
if err != nil {
return nil, trace.Wrap(err)
}
return otpQR.Bytes(), nil
}
// GenerateOTPURL returns a OTP Key URL that can be used to construct a HOTP or TOTP key. For more
// details see: https://github.com/google/google-authenticator/wiki/Key-Uri-Format
// Example: otpauth://totp/foo:bar@baz.com?secret=qux
func GenerateOTPURL(typ string, label string, parameters map[string][]byte) string {
var u url.URL
u.Scheme = "otpauth"
u.Host = typ
u.Path = label
var params url.Values = make(url.Values)
for k, v := range parameters {
if k == "secret" {
v = []byte(base32.StdEncoding.EncodeToString(v))
}
params.Add(k, string(v))
}
u.RawQuery = params.Encode()
return u.String()
}