-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
2fa_reset.go
69 lines (55 loc) · 1.51 KB
/
2fa_reset.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
package user
import (
"encoding/json"
"net/http"
"github.com/azukaar/cosmos-server/src/utils"
)
type User2FAResetRequest struct {
Nickname string
}
func Delete2FA(w http.ResponseWriter, req *http.Request) {
if utils.AdminOnly(w, req) != nil {
return
}
var request User2FAResetRequest
errD := json.NewDecoder(req.Body).Decode(&request)
if errD != nil {
utils.Error("2FA Error: Invalid User Request", errD)
utils.HTTPError(w, "2FA Error", http.StatusInternalServerError, "2FA001")
return
}
nickname := request.Nickname
c, errCo := utils.GetCollection(utils.GetRootAppId(), "users")
if errCo != nil {
utils.Error("Database Connect", errCo)
utils.HTTPError(w, "Database", http.StatusInternalServerError, "DB001")
return
}
userInBase := utils.User{}
err := c.FindOne(nil, map[string]interface{}{
"Nickname": nickname,
}).Decode(&userInBase)
if err != nil {
utils.Error("UserGet: Error while getting user", err)
utils.HTTPError(w, "User Get Error", http.StatusInternalServerError, "2FA002")
return
}
toSet := map[string]interface{}{
"Was2FAVerified": false,
"MFAKey": "",
"PasswordCycle": userInBase.PasswordCycle + 1,
}
_, err = c.UpdateOne(nil, map[string]interface{}{
"Nickname": nickname,
}, map[string]interface{}{
"$set": toSet,
})
if err != nil {
utils.Error("UserGet: Error while getting user", err)
utils.HTTPError(w, "User Get Error", http.StatusInternalServerError, "2FA002")
return
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
})
}