-
Notifications
You must be signed in to change notification settings - Fork 3
/
gf_users_email.go
319 lines (259 loc) · 7.96 KB
/
gf_users_email.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
/*
GloFlow application and media management/publishing platform
Copyright (C) 2022 Ivan Trajkovic
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package gf_identity_core
import (
"fmt"
"context"
"time"
"github.com/gloflow/gloflow/go/gf_core"
"github.com/gloflow/gloflow/go/gf_extern_services/gf_aws"
)
//---------------------------------------------------
func UsersEmailPipelineVerify(pEmailAddressStr string,
pUserNameStr GFuserName,
pUserIDstr gf_core.GF_ID,
pDomainBaseStr string,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
// EMAIL_CONFIRMM
confirmCodeStr := usersEmailGenerateConfirmationCode()
// DB
gfErr := dbSQLuserEmailConfirmCreate(pUserNameStr,
pUserIDstr,
confirmCodeStr,
pCtx,
pRuntimeSys)
if gfErr != nil {
return gfErr
}
msgSubjectStr, msgBodyHTMLstr, msgBodyTextStr := usersEmailGetConfirmMsgInfo(pUserNameStr,
confirmCodeStr,
pDomainBaseStr)
// sender address
senderAddressStr := fmt.Sprintf("gf-email-confirm@%s", pDomainBaseStr)
//------------------------
// PLUGIN
//------------------------
// EMAIL_PLUGIN
gfErr = pRuntimeSys.ExternalPlugins.EmailSendingCallback(pEmailAddressStr,
senderAddressStr,
msgSubjectStr,
"email_address_confirmation",
pRuntimeSys)
if gfErr != nil {
return gfErr
}
//------------------------
// AWS
gfErr = gf_aws.SESsendMessage(pEmailAddressStr,
senderAddressStr,
msgSubjectStr,
msgBodyHTMLstr,
msgBodyTextStr,
pRuntimeSys)
if gfErr != nil {
return gfErr
}
//------------------------
return nil
}
//---------------------------------------------------
func UsersEmailPipelineConfirm(pInput *GFuserHTTPinputEmailConfirm,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) (bool, string, *gf_core.GFerror) {
dbConfirmCodeStr, expiredBool, gfErr := usersEmailGetConfirmationCode(pInput.UserNameStr,
pCtx,
pRuntimeSys)
if gfErr != nil {
return false, "", gfErr
}
if expiredBool {
return false, "email confirmation code has expired", nil
}
// confirm_code is correct
if pInput.ConfirmCodeStr == dbConfirmCodeStr {
// GET_USER_ID
userIDstr, gfErr := DBsqlGetBasicInfoByUsername(pInput.UserNameStr,
pCtx,
pRuntimeSys)
if gfErr != nil {
return false, "", gfErr
}
//------------------------
// initial user email confirmation. only for new users.
// user confirmed their email as valid.
userEmailConfirmedBool, gfErr := DBsqlUserEmailIsConfirmed(pInput.UserNameStr, pCtx, pRuntimeSys)
if gfErr != nil {
return false, "", gfErr
}
// if users email is not already marked as confirmed in the DB, updated
// it to mark it as confirmed.
if !userEmailConfirmedBool {
emailIsConfirmedBool := true
updateOp := &GFuserUpdateOp{
EmailConfirmedBool: &emailIsConfirmedBool,
}
// UPDATE_USER - mark user as email_confirmed
gfErr = DBsqlUserUpdate(userIDstr,
updateOp,
pCtx,
pRuntimeSys)
if gfErr != nil {
return false, "", gfErr
}
}
//------------------------
//------------------------
// UPDATE_LOGIN_ATTEMPT
// if email is confirmed then update the login_attempt
// get a preexisting login_attempt if one exists and hasnt expired for this user.
// if it has then a new one will have to be created.
var loginAttempt *GFloginAttempt
loginAttempt, gfErr = LoginAttemptGetIfValid(GFuserName(pInput.UserNameStr),
pCtx,
pRuntimeSys)
if gfErr != nil {
return false, "", gfErr
}
if loginAttempt == nil {
return false, "login_attempt for this user has not been found in the DB, to mark its email_confirmed flag as true", nil
}
loginEmailConfirmedBool := true
updateOp := &GFloginAttemptUpdateOp{EmailConfirmedBool: &loginEmailConfirmedBool}
gfErr = DBsqlLoginAttemptUpdate(loginAttempt.ID,
updateOp,
pCtx,
pRuntimeSys)
if gfErr != nil {
return false, "", gfErr
}
//------------------------
return true, "", nil
} else {
return false, "received confirm code and DB confirm code are not the same", nil
}
return false, "", nil
}
//---------------------------------------------------
func usersEmailGetConfirmationCode(pUserNameStr GFuserName,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) (string, bool, *gf_core.GFerror) {
expiredBool := false
confirmCodeStr, confirmCodeCreationTimeF, gfErr := dbSQLuserEmailConfirmGetCode(pUserNameStr,
pCtx,
pRuntimeSys)
if gfErr != nil {
return "", expiredBool, gfErr
}
//------------------------
// check confirm_code didnt expire
currentUNIXtimeF := float64(time.Now().UnixNano())/1000000000.0
confirmCodeAgeTimeF := currentUNIXtimeF - confirmCodeCreationTimeF
// check if older than 5min
if (5.0 < confirmCodeAgeTimeF / 60) {
expiredBool = true
return "", expiredBool, nil
}
//------------------------
return confirmCodeStr, expiredBool, nil
}
//---------------------------------------------------
func usersEmailGenerateConfirmationCode() string {
cStr := fmt.Sprintf("%s:%s", gf_core.StrRandom(), gf_core.StrRandom())
return cStr
}
//---------------------------------------------------
func usersEmailGetConfirmMsgInfo(pUserNameStr GFuserName,
pConfirmCodeStr string,
pDomainStr string) (string, string, string) {
subjectStr := fmt.Sprintf("%s - confirm your email", pDomainStr)
welcomeMsgStr := fmt.Sprintf(`
<div id="welcome_message" style="
margin-left: 10px;
padding-top: 9px;
margin-top: 33px;">
Hello <span style="font-weight: bold;">%s</span>.
Welcome to %s!</div>
<div>`,
pUserNameStr,
pDomainStr)
confirmBtnStr := fmt.Sprintf(`
<div id="confirm_btn" style="
width: 100%%;
background-color: #e17d44;
text-align: center;
padding-top: 12px;
padding-bottom: 14px;
cursor: pointer;">
<a style="color: white; cursor: pointer;text-decoration: none;" href="https://%s/v1/identity/email_confirm?u=%s&c=%s">confirm email</a>
</div>`,
pDomainStr,
pUserNameStr,
pConfirmCodeStr)
htmlStr := fmt.Sprintf(`
<div>
<style>
body {
margin: 0px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
/*turn off horizontal scroll*/
overflow-x: hidden;
background-color: #d7d7d7;
}
</style>
<div id='gf_logo' style="margin-top: 75px;">
<img src="https://gloflow.com/images/d/gf_logo_0.3.png"></img>
</div>
<div>
%s
</div>
<div id="confirm_email" style="
background-color: rgb(234, 206, 196);
margin-top: 50px;
padding: 10px;
width: 360px;
margin-bottom: 50px;">
<div style="
font-size: 12px;
margin-bottom: 8px;">Please click on the bellow link to confirm your email address.
</div>
%s
</div>
<div>
<div id="message" style="
margin-top: 5px;
margin-bottom: 5px;
padding-left: 11px;">
"There is no spoon ...it is only yourself."
</div>
<img src="https://gloflow.com/images/d/thumbnails/b2373f98d61208c60155fce191399f9f_thumb_large.png"></img>
</div>
<div style="font-size: 10px; padding: 3px; padding-left: 7px; margin-top: 140px;">
don't reply to this email
</div>
</div>`,
welcomeMsgStr,
confirmBtnStr)
textStr := fmt.Sprintf(`
Welcome to %s!
There is no spoon. ...it is only yourself.
Please open the following link in your browser to confirm your email address.
https://%s/v1/identity/email_confirm?c=%s`,
pDomainStr,
pDomainStr,
pConfirmCodeStr)
return subjectStr, htmlStr, textStr
}