-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
113 lines (97 loc) · 2.34 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"image/png"
"log"
"net/http"
"strconv"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
)
const (
// Username - Global Username for testing
Username = "John"
// Password - Global Password for testing
Password = "IamNotACat"
// OTP Required Data --------
// Issuer - The site that is creating the OTP Access Code
Issuer = "localhost:8080"
// AccountName - The account that the OTP Access Code is linked to
AccountName = "John@localhost"
)
var (
// QRCode - in memory representation of the QR Access Code
QRCode bytes.Buffer
// KeyOTP - The One Time Access key for validating the access code
KeyOTP *otp.Key
)
// OTPRequest - The request payload that handles the OTP Access Code
type OTPRequest struct {
Code string `json:"OTP"`
}
func main() {
log.Println("Staring...")
createQRCode()
http.HandleFunc("/login", login)
http.HandleFunc("/QrCode", qrCode)
log.Fatal(http.ListenAndServe("localhost:8080", nil))
}
func login(w http.ResponseWriter, r *http.Request) {
log.Printf("%+v", r)
var otp OTPRequest
err := json.NewDecoder(r.Body).Decode(&otp)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
username, password, ok := r.BasicAuth()
if !ok {
w.WriteHeader(http.StatusBadRequest)
w.Write(nil)
return
}
if check(username, password, otp.Code) {
w.WriteHeader(http.StatusAccepted)
w.Write(nil)
return
}
w.WriteHeader(http.StatusForbidden)
w.Write(nil)
return
}
// check - Check the username and password against some sort of authentication data store
func check(username, password, otp string) bool {
if username == Username && password == Password {
valid := totp.Validate(otp, KeyOTP.Secret())
if valid {
return true
}
}
return false
}
func qrCode(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Content-Length", strconv.Itoa(len(QRCode.Bytes())))
if _, err := w.Write(QRCode.Bytes()); err != nil {
log.Println("unable to write image.")
}
return
}
func createQRCode() {
key, err := totp.Generate(
totp.GenerateOpts{
Issuer: Issuer,
AccountName: AccountName,
})
if err != nil {
log.Fatal(err)
}
img, err := key.Image(200, 200)
if err != nil {
log.Fatal(err)
}
// Create a QR Code and store it in memory
png.Encode(&QRCode, img)
KeyOTP = key
}