/
handlers.go
167 lines (111 loc) · 2.98 KB
/
handlers.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
159
160
161
162
163
164
165
166
167
package server
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"image/png"
"time"
"github.com/savsgio/atreugo"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
"github.com/caeril/totpd/data"
)
func NewSHA256(data []byte) []byte {
hash := sha256.Sum256(data)
return hash[:]
}
func addUser(org string, uid string) string {
user := data.User{Organization: org, Username: uid}
key, err := totp.Generate(totp.GenerateOpts{
Issuer: user.Organization,
AccountName: user.Username,
})
if err != nil {
panic(err)
}
user.Id = hex.EncodeToString(NewSHA256([]byte(key.URL())))
user.Secret = key.Secret()
user.URL = key.URL()
data.PutUser(user)
return user.Id
}
func InitHandlers() {
}
func _Post_Validation() error {
return nil
}
func _Get_Index(ctx *atreugo.RequestCtx) error {
page := get_markup("Index.html")
page.vars.Set("title", "Home")
users := []data.User{}
for _, uid := range data.ListUsers() {
user := data.GetUser(uid)
users = append(users, user)
}
page.vars.Set("users", users)
return render_markup(ctx, page)
}
func _Post_User(ctx *atreugo.RequestCtx) error {
sUsername := string(ctx.Request.PostArgs().Peek("uid"))
sOrganization := string(ctx.Request.PostArgs().Peek("org"))
uid := addUser(sOrganization, sUsername)
ctx.Redirect("/users/"+uid, 302)
return nil
}
func _Get_User(ctx *atreugo.RequestCtx) error {
sValid := string(ctx.QueryArgs().Peek("valid"))
sInvalid := string(ctx.QueryArgs().Peek("invalid"))
valid := sValid == "true"
invalid := sInvalid == "true"
iId := ctx.UserValue("id")
sId := fmt.Sprintf("%s", iId)
user := data.GetUser(sId)
if len(user.Id) == 0 {
return ctx.HTTPResponse("404 Nuh uh", 404)
}
page := get_markup("User.html")
page.vars.Set("title", "User")
page.vars.Set("user", user)
futureCodes := []string{}
t := time.Now()
for i := 0; i < 6; i++ {
code, _ := totp.GenerateCode(user.Secret, t)
futureCodes = append(futureCodes, code)
t = t.Add(time.Second * 30)
}
page.vars.Set("codes", futureCodes)
page.vars.Set("invalid", invalid)
page.vars.Set("valid", valid)
return render_markup(ctx, page)
}
func _Get_QR(ctx *atreugo.RequestCtx) error {
iId := ctx.UserValue("id")
sId := fmt.Sprintf("%s", iId)
user := data.GetUser(sId)
if len(user.Id) == 0 {
return ctx.HTTPResponse("404 Nuh uh", 404)
}
key, err := otp.NewKeyFromURL(user.URL)
if err != nil {
panic(err)
}
w := bytes.Buffer{}
image, _ := key.Image(512, 512)
png.Encode(&w, image)
ctx.SetContentType("image/png")
ctx.Response.Header.Add("Content-Type", "image/png")
return ctx.HTTPResponseBytes(w.Bytes(), 200)
}
func _Post_Validate(ctx *atreugo.RequestCtx) error {
sUserId := string(ctx.Request.PostArgs().Peek("uid"))
sCode := string(ctx.Request.PostArgs().Peek("code"))
user := data.GetUser(sUserId)
result := totp.Validate(sCode, user.Secret)
if result {
ctx.Redirect("/users/"+user.Id+"?valid=true", 302)
} else {
ctx.Redirect("/users/"+user.Id+"?invalid=true", 302)
}
return nil
}