/
acct_verification.go
179 lines (165 loc) · 4.08 KB
/
acct_verification.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
package controller
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"html/template"
"net/http"
// "log"
"github.com/teambo-org/teambo/internal/model"
"github.com/teambo-org/teambo/internal/service"
"github.com/teambo-org/teambo/internal/util"
)
func AcctVerification(w http.ResponseWriter, r *http.Request) {
email := r.FormValue("email")
akey := r.FormValue("akey")
id := r.FormValue("id")
vkey := r.FormValue("vkey")
ct := r.FormValue("ct")
beta_code := r.FormValue("beta")
ikey := r.FormValue("ikey")
ihash := r.FormValue("ihash")
pkey := r.FormValue("pkey")
news := r.FormValue("news")
if id == "" && email != "" {
h := sha256.New()
h.Write([]byte(email))
id = base64.StdEncoding.EncodeToString(h.Sum(nil))
}
verification_required := true
if util.Config.Get("acct.verification_required") == "false" {
verification_required = false
}
beta := model.BetaCode{}
invite := model.Invite{}
err := errors.New("")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if vkey != "" {
v, err := model.FindAcctVerification(id, akey)
if err != nil {
error_out(w, "Verification could not be completed", 500)
return
}
if v.Vkey != vkey {
error_out(w, "Verification has expired", 404)
return
}
if pkey == "" {
error_out(w, "Password protection token required", 400)
return
}
if ct == "" {
error_out(w, "Account Ciphertext required", 400)
return
}
_, err = model.CreateAcct(id, akey, pkey, ct)
if err != nil {
error_out(w, "Account could not be created", 500)
return
}
v.Delete()
if news == "true" {
model.NewsletterInsert(email)
}
model.AcctThrottle.Clear(id)
msg, _ := json.Marshal(map[string]bool{
"success": true,
})
http.Error(w, string(msg), 200)
} else {
if email == "" || akey == "" {
error_out(w, "Email and access key required", 400)
return
}
if ikey != "" && ihash != "" {
invite, _ = model.InviteFind(ikey)
if invite.Hash == "" {
error_out(w, "Invite Has Expired", 404)
return
}
if invite.Hash != ihash {
error_out(w, "Invalid Invite Code", 403)
return
}
if invite.Redeemable() {
verification_required = false
} else {
error_out(w, "Invite Code already redeemed", 403)
return
}
}
if verification_required {
if beta_code == "" {
error_out(w, "Beta code required", 400)
return
}
beta, _ = model.FindBetaCode(beta_code)
if beta.Found == "" {
error_out(w, "Invalid Beta Code", 403)
return
}
}
if model.AcctThrottle.Remaining(id) < 1 {
error_data(w, 403, map[string]interface{}{
"error": "Account is locked",
"code": "acct_locked",
"ttl": model.AcctThrottle.TTL,
"resets": model.AcctThrottle.RemainingResets(id),
})
return
}
acct, _ := model.FindAcct(id, akey)
if acct.Ciphertext != "" && acct.Ciphertext != "new" {
error_out(w, "Account already exists", 409)
return
} else {
model.AcctThrottle.Log(id)
}
vkey = util.RandStr(16)
_, err = model.CreateAcctVerification(id, akey, vkey)
if err != nil {
error_out(w, "Verification could not be sent", 500)
return
}
scheme := "http"
if util.Config.Get("ssl.active") == "true" {
scheme = scheme + "s"
}
msg := []byte("")
if !verification_required {
msg, _ = json.Marshal(map[string]interface{}{
"success": true,
"vkey": vkey,
})
} else {
subject := "Teambo Account Verification"
link := scheme + "://" + util.Config.Get("app.host") + "/#/login?vkey=" + vkey
if news == "true" {
link = link + "&news=true"
}
t, err := template.ParseFiles("template/email/verification.html")
data := map[string]interface{}{
"email": email,
"link": link,
}
buf := new(bytes.Buffer)
if err = t.Execute(buf, data); err != nil {
return
}
body := buf.String()
service.EmailQueue.Push(email, subject, body)
msg, _ = json.Marshal(map[string]bool{
"success": true,
})
}
if beta.Code != "" && beta.Found != "" {
beta.Delete()
}
if invite.Id != "" && invite.Hash != "" {
invite.Redeem()
}
http.Error(w, string(msg), 201)
}
}