-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateOtpAuthUrl.go
66 lines (56 loc) · 1.28 KB
/
createOtpAuthUrl.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
package main
import (
"bufio"
"fmt"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
"image/png"
"os"
"strings"
)
func WriteQrFile(filePath, otpAuthUrl string) error {
f, err := os.OpenFile(filePath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
ErrorLog.Println(err.Error())
return err
}
defer f.Close()
key, err := otp.NewKeyFromURL(otpAuthUrl)
if err != nil {
ErrorLog.Println(err.Error())
return err
}
img, err := key.Image(200, 200)
if err != nil {
ErrorLog.Println(err.Error())
return err
}
if err := png.Encode(f, img); err != nil {
ErrorLog.Println(err.Error())
return err
}
return nil
}
func CreateOtpAuthUrl(qrcodeFilePath string) error {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Issuer: ")
issuer, _ := reader.ReadString('\n')
issuer = strings.TrimRight(issuer, "\n")
fmt.Print("Enter account name: ")
accountName, _ := reader.ReadString('\n')
accountName = strings.TrimRight(accountName, "\n")
opts := totp.GenerateOpts{
Issuer: issuer,
AccountName: accountName,
}
key, err := totp.Generate(opts)
if err !=nil {
ErrorLog.Println(err.Error())
return err
}
fmt.Println(key)
if qrcodeFilePath != `` {
if err := WriteQrFile(qrcodeFilePath, key.String()); err != nil { return err }
}
return nil
}