-
Notifications
You must be signed in to change notification settings - Fork 54
/
2fa.go
385 lines (338 loc) · 9.56 KB
/
2fa.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"golang.org/x/crypto/bcrypt"
"github.com/gin-gonic/gin"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
"zxq.co/ripple/rippleapi/common"
"zxq.co/x/rs"
)
var allowedPaths = [...]string{
"/logout",
"/2fa_gateway",
"/2fa_gateway/verify",
"/2fa_gateway/clear",
"/2fa_gateway/recover",
"/favicon.ico",
}
// middleware to deny all requests to non-allowed pages
func twoFALock(c *gin.Context) {
// check it's not a static file
if len(c.Request.URL.Path) >= 8 && c.Request.URL.Path[:8] == "/static/" {
c.Next()
return
}
ctx := getContext(c)
if ctx.User.ID == 0 {
c.Next()
return
}
sess := getSession(c)
if v, _ := sess.Get("2fa_must_validate").(bool); !v {
// * check 2fa is enabled.
// if it is,
// * check whether the current ip is found in the database.
// if it is, move on and show the page.
// if it isn't, set 2fa_must_validate
// if it isn't, move on.
if is2faEnabled(ctx.User.ID) > 0 {
err := db.QueryRow("SELECT 1 FROM ip_user WHERE userid = ? AND ip = ? LIMIT 1", ctx.User.ID, clientIP(c)).Scan(new(int))
if err != sql.ErrNoRows {
c.Next()
return
}
sess.Set("2fa_must_validate", true)
} else {
c.Next()
return
}
}
// check it's one of the few approved paths
for _, a := range allowedPaths {
if a == c.Request.URL.Path {
sess.Save()
c.Next()
return
}
}
addMessage(c, warningMessage{T(c, "You need to complete the 2fa challenge first.")})
sess.Save()
query := c.Request.URL.RawQuery
if query != "" {
query = "?" + query
}
c.Redirect(302, "/2fa_gateway?redir="+url.QueryEscape(c.Request.URL.Path+query))
c.Abort()
}
const (
tfaEnabledTelegram = 1 << iota
tfaEnabledTOTP
)
// is2faEnabled checks 2fa is enabled for an user.
func is2faEnabled(user int) int {
var enabled int
db.QueryRow("SELECT IFNULL((SELECT 1 FROM 2fa_telegram WHERE userid = ?), 0) | IFNULL((SELECT 2 FROM 2fa_totp WHERE userid = ? AND enabled = 1), 0) as x", user, user).
Scan(&enabled)
return enabled
}
func tfaGateway(c *gin.Context) {
sess := getSession(c)
redir := c.Query("redir")
switch {
case redir == "":
redir = "/"
case redir[0] != '/':
redir = "/"
}
i, _ := sess.Get("userid").(int)
if i == 0 {
c.Redirect(302, redir)
}
// check 2fa hasn't been disabled
e := is2faEnabled(i)
if e == 0 {
sess.Delete("2fa_must_validate")
sess.Save()
c.Redirect(302, redir)
return
}
if e == 1 {
// check previous 2fa thing is still valid
err := db.QueryRow("SELECT 1 FROM 2fa WHERE userid = ? AND ip = ? AND expire > ?",
i, clientIP(c), time.Now().Unix()).Scan(new(int))
if err != nil {
db.Exec("INSERT INTO 2fa(userid, token, ip, expire, sent) VALUES (?, ?, ?, ?, 0);",
i, strings.ToUpper(rs.String(8)), clientIP(c), time.Now().Add(time.Hour).Unix())
http.Get("http://127.0.0.1:8888/update")
}
}
resp(c, 200, "2fa_gateway.html", &baseTemplateData{
TitleBar: "Two Factor Authentication",
KyutGrill: "2fa.jpg",
RequestInfo: map[string]interface{}{
"redir": redir,
},
})
}
func clientIP(c *gin.Context) string {
ff := c.Request.Header.Get("CF-Connecting-IP")
if ff != "" {
return ff
}
return c.ClientIP()
}
func clear2fa(c *gin.Context) {
// basically deletes from db 2fa tokens, so that it gets regenerated when user hits gateway page
sess := getSession(c)
i, _ := sess.Get("userid").(int)
if i == 0 {
c.Redirect(302, "/")
}
db.Exec("DELETE FROM 2fa WHERE userid = ? AND ip = ?", i, clientIP(c))
addMessage(c, successMessage{T(c, "A new code has been generated and sent to you through Telegram.")})
sess.Save()
c.Redirect(302, "/2fa_gateway")
}
func verify2fa(c *gin.Context) {
sess := getSession(c)
i, _ := sess.Get("userid").(int)
if i == 0 {
c.Redirect(302, "/")
}
e := is2faEnabled(i)
switch e {
case 1:
var id int
var expire common.UnixTimestamp
err := db.QueryRow("SELECT id, expire FROM 2fa WHERE userid = ? AND ip = ? AND token = ?", i, clientIP(c), strings.ToUpper(c.Query("token"))).Scan(&id, &expire)
if err == sql.ErrNoRows {
c.String(200, "1")
return
}
if time.Now().After(time.Time(expire)) {
c.String(200, "1")
db.Exec("INSERT INTO 2fa(userid, token, ip, expire, sent) VALUES (?, ?, ?, ?, 0);",
i, strings.ToUpper(rs.String(8)), clientIP(c), time.Now().Add(time.Hour).Unix())
http.Get("http://127.0.0.1:8888/update")
return
}
case 2:
var secret string
db.Get(&secret, "SELECT secret FROM 2fa_totp WHERE userid = ?", i)
if !totp.Validate(strings.Replace(c.Query("token"), " ", "", -1), secret) {
c.String(200, "1")
return
}
}
loginUser(c, i)
db.Exec("DELETE FROM 2fa WHERE id = ?", i)
c.String(200, "0")
}
func loginUser(c *gin.Context, i int) {
var d struct {
Country string
Flags uint
}
err := db.Get(&d, "SELECT users_stats.country, users.flags FROM users_stats "+
"LEFT JOIN users ON users.id = users_stats.id WHERE users_stats.id = ?", i)
if err != nil {
c.Error(err)
}
afterLogin(c, i, d.Country, d.Flags)
addMessage(c, successMessage{T(c, "You've been successfully logged in.")})
sess := getSession(c)
sess.Delete("2fa_must_validate")
sess.Save()
}
func recover2fa(c *gin.Context) {
sess := getSession(c)
i, _ := sess.Get("userid").(int)
if i == 0 {
c.Redirect(302, "/")
}
e := is2faEnabled(i)
if e != 2 {
respEmpty(c, "Recover account", warningMessage{T(c, "Oh no you don't.")})
return
}
resp(c, 200, "2fa_gateway_recover.html", &baseTemplateData{
TitleBar: T(c, "Recover account"),
KyutGrill: "2fa.jpg",
})
}
func recover2faSubmit(c *gin.Context) {
sess := getSession(c)
i, _ := sess.Get("userid").(int)
if i == 0 {
c.Redirect(302, "/")
}
if is2faEnabled(i) != 2 {
respEmpty(c, T(c, "Recover account"), warningMessage{T(c, "Get out.")})
return
}
var codesRaw string
db.Get(&codesRaw, "SELECT recovery FROM 2fa_totp WHERE userid = ?", i)
var codes []string
json.Unmarshal([]byte(codesRaw), &codes)
for k, v := range codes {
if v == c.PostForm("recovery_code") {
codes[k] = codes[len(codes)-1]
codes = codes[:len(codes)-1]
b, _ := json.Marshal(codes)
db.Exec("UPDATE 2fa_totp SET recovery = ? WHERE userid = ?", string(b), i)
loginUser(c, i)
c.Redirect(302, "/")
return
}
}
resp(c, 200, "2fa_gateway_recover.html", &baseTemplateData{
TitleBar: T(c, "Recover account"),
KyutGrill: "2fa.jpg",
Messages: []message{errorMessage{T(c, "Recovery code is invalid.")}},
})
}
// deletes expired 2fa confirmation tokens. gets current confirmation token.
// if it does not exist, generates one.
func get2faConfirmationToken(user int) (token string) {
db.Exec("DELETE FROM 2fa_confirmation WHERE expire < ?", time.Now().Unix())
db.Get(&token, "SELECT token FROM 2fa_confirmation WHERE userid = ? LIMIT 1", user)
if token != "" {
return
}
token = rs.String(32)
db.Exec("INSERT INTO 2fa_confirmation (userid, token, expire) VALUES (?, ?, ?)",
user, token, time.Now().Add(time.Hour).Unix())
return
}
func disable2fa(c *gin.Context) {
ctx := getContext(c)
if ctx.User.ID == 0 {
resp403(c)
return
}
s := getSession(c)
var m message
defer func() {
addMessage(c, m)
s.Save()
c.Redirect(302, "/settings/2fa")
}()
if ok, _ := CSRF.Validate(ctx.User.ID, c.PostForm("csrf")); !ok {
m = errorMessage{T(c, "Your session has expired. Please try redoing what you were trying to do.")}
return
}
var pass string
db.Get(&pass, "SELECT password_md5 FROM users WHERE id = ?", ctx.User.ID)
if err := bcrypt.CompareHashAndPassword(
[]byte(pass),
[]byte(cmd5(c.PostForm("password"))),
); err != nil {
m = errorMessage{"Wrong password."}
return
}
db.Exec("DELETE FROM 2fa_telegram WHERE userid = ?", ctx.User.ID)
db.Exec("DELETE FROM 2fa_totp WHERE userid = ?", ctx.User.ID)
m = successMessage{T(c, "2FA disabled successfully.")}
}
func totpSetup(c *gin.Context) {
ctx := getContext(c)
sess := getSession(c)
if ctx.User.ID == 0 {
resp403(c)
return
}
defer c.Redirect(302, "/settings/2fa")
defer sess.Save()
if ok, _ := CSRF.Validate(ctx.User.ID, c.PostForm("csrf")); !ok {
addMessage(c, errorMessage{T(c, "Your session has expired. Please try redoing what you were trying to do.")})
return
}
switch is2faEnabled(ctx.User.ID) {
case 1:
addMessage(c, errorMessage{T(c, "You currently have Telegram 2FA enabled. You first need to disable that if you want to use TOTP-based 2FA.")})
return
case 2:
addMessage(c, errorMessage{T(c, "TOTP-based 2FA is already enabled!")})
return
}
pc := strings.Replace(c.PostForm("passcode"), " ", "", -1)
var secret string
db.Get(&secret, "SELECT secret FROM 2fa_totp WHERE userid = ?", ctx.User.ID)
if secret == "" || pc == "" {
addMessage(c, errorMessage{T(c, "No passcode/secret was given. Please try again")})
return
}
fmt.Println(pc, secret)
if !totp.Validate(pc, secret) {
addMessage(c, errorMessage{T(c, "Passcode is invalid. Perhaps it expired?")})
return
}
codes, _ := json.Marshal(generateRecoveryCodes())
db.Exec("UPDATE 2fa_totp SET recovery = ?, enabled = 1 WHERE userid = ?", string(codes), ctx.User.ID)
addMessage(c, successMessage{T(c, "TOTP-based 2FA has been enabled on your account.")})
}
func generateRecoveryCodes() []string {
x := make([]string, 8)
for i := range x {
x[i] = rs.StringFromChars(6, "QWERTYUIOPASDFGHJKLZXCVBNM1234567890")
}
return x
}
func generateKey(ctx context) *otp.Key {
k, err := totp.Generate(totp.GenerateOpts{
Issuer: "Ripple",
AccountName: ctx.User.Username,
})
if err != nil {
return nil
}
db.Exec("INSERT INTO 2fa_totp(userid, secret) VALUES (?, ?) ON DUPLICATE KEY UPDATE secret = VALUES(secret)", ctx.User.ID, k.Secret())
return k
}