-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (130 loc) · 3.64 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"github.com/manuwell/guardian/Godeps/_workspace/src/github.com/joho/godotenv"
"github.com/manuwell/guardian/Godeps/_workspace/src/github.com/julienschmidt/httprouter"
"github.com/manuwell/guardian/Godeps/_workspace/src/github.com/pquerna/otp"
"github.com/manuwell/guardian/Godeps/_workspace/src/github.com/pquerna/otp/totp"
"bytes"
"encoding/base64"
"encoding/json"
"html/template"
"image/png"
"log"
"net/http"
"os"
)
type TokenValidationStruct struct {
Valid bool
}
func checkAuth(w http.ResponseWriter, r *http.Request, ps httprouter.Params, handler func(http.ResponseWriter, *http.Request, httprouter.Params)) {
user, pass, _ := r.BasicAuth()
if (user == os.Getenv("GUARDIAN_HTTP_USER")) && (pass == os.Getenv("GUARDIAN_HTTP_PASS")) {
handler(w, r, ps)
} else {
renderForbidden(w, r)
}
}
func tokenValidation(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
token := ps.ByName("token")
secret := os.Getenv("GUARDIAN_SECRET")
valid := totp.Validate(token, secret)
validator := TokenValidationStruct{valid}
response, err := json.MarshalIndent(validator, "", "\t")
if err != nil {
log.Fatal(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
func qrcode(key *otp.Key) string {
// Convert TOTP key into a PNG
var buf bytes.Buffer
img, err := key.Image(400, 400)
if err != nil {
panic(err)
}
png.Encode(&buf, img)
return base64.StdEncoding.EncodeToString(buf.Bytes())
}
func render(key *otp.Key) []byte {
const tpl = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{.Title}}</title>
</head>
<body style="text-align: center">
<img src="data:image/png;base64,{{ .Qrcode }}" />
<p>{{ .Issuer }}</p>
<p>{{ .Account }}</p>
<p>{{ .Secret }}</p>
</body>
</html>`
t, err := template.New("webpage").Parse(tpl)
data := struct {
Title string
Qrcode string
Issuer string
Account string
Secret string
}{
Title: "Guardian Token Generator",
Qrcode: qrcode(key),
Issuer: key.Issuer(),
Account: key.AccountName(),
Secret: key.Secret(),
}
var buf bytes.Buffer
err = t.Execute(&buf, data)
if err != nil {
log.Fatal(err)
}
return buf.Bytes()
}
func tokenGenereation(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
secret := os.Getenv("GUARDIAN_SECRET")
if secret != "" {
w.Write(bytes.NewBufferString("You already set an OTP secret").Bytes())
w.WriteHeader(http.StatusNotAcceptable)
return
}
key, err := totp.Generate(totp.GenerateOpts{
Issuer: r.FormValue("issuer"),
AccountName: r.FormValue("account"),
})
if err != nil {
panic(err)
}
os.Setenv("GUARDIAN_SECRET", key.Secret())
response := render(key)
w.Write(response)
w.Header().Set("Content-Disposition", "attachment; filename='guardian.html'")
w.WriteHeader(http.StatusCreated)
}
func renderInternalServerError(w http.ResponseWriter, r *http.Request, message string) {
w.Write(bytes.NewBufferString(message).Bytes())
w.WriteHeader(http.StatusInternalServerError)
}
func renderForbidden(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
}
func loadEnv() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
func main() {
if os.Getenv("GUARDIAN_ENV") != "production" {
loadEnv()
}
router := httprouter.New()
router.POST("/token", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
checkAuth(w, r, ps, tokenGenereation)
})
router.GET("/token/check/:token", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
checkAuth(w, r, ps, tokenValidation)
})
http.ListenAndServe(":"+os.Getenv("GUARDIAN_HTTP_PORT"), router)
}