-
Notifications
You must be signed in to change notification settings - Fork 3
/
gf_admin.go
550 lines (440 loc) · 14.5 KB
/
gf_admin.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
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
import (
"context"
"github.com/getsentry/sentry-go"
"github.com/gloflow/gloflow/go/gf_core"
"github.com/gloflow/gloflow/go/gf_events"
"github.com/gloflow/gloflow/go/gf_identity/gf_identity_core"
)
//---------------------------------------------------
type GFadminInputLogin struct {
UserNameStr gf_identity_core.GFuserName `validate:"required,min=3,max=50"`
// pass is not provided if email-login is used
PassStr string `validate:"omitempty,min=8,max=50"`
// admin email
EmailStr string `validate:"omitempty,email"`
}
type GFadminOutputLogin struct {
UserExistsBool bool
EmailConfirmedBool bool
PassValidBool bool
UserIDstr gf_core.GF_ID
}
type GFadminOutputCreateAdmin struct {
General *gf_identity_core.GFuserpassOutputCreate
}
type GFadminInputAddToInviteList struct {
AdminUserID gf_core.GF_ID `validate:"required,min=3,max=50"`
EmailStr string `validate:"required,email"`
}
type GFadminRemoveFromInviteListInput struct {
AdminUserID gf_core.GF_ID `validate:"required,min=3,max=50"`
EmailStr string `validate:"required,email"`
}
type GFadminUserViewOutput struct {
ID gf_core.GF_ID `json:"id_str"`
CreationUNIXtimeF float64 `json:"creation_unix_time_f"`
UserNameStr gf_identity_core.GFuserName `json:"user_name_str"`
ScreenNameStr string `json:"screen_name_str"`
AddressesETHlst []gf_identity_core.GFuserAddressETH `json:"addresses_eth_lst"`
EmailStr string `json:"email_str"`
EmailConfirmedBool bool `json:"email_confirmed_bool"`
ProfileImageURLstr string `json:"profile_image_url_str"`
}
type GFadminResendConfirmEmailInput struct {
UserID gf_core.GF_ID `validate:"required,min=3,max=50"`
UserNameStr gf_identity_core.GFuserName `validate:"required,min=3,max=50"`
EmailStr string `validate:"required,email"`
}
type GFadminUserDeleteInput struct {
UserID gf_core.GF_ID `validate:"required,min=3,max=50"`
UserNameStr gf_identity_core.GFuserName `validate:"required,min=3,max=50"`
}
//------------------------------------------------
// PIPELINE_DELETE_USER
func AdminPipelineDeleteUser(pInput *GFadminUserDeleteInput,
pCtx context.Context,
pServiceInfo *gf_identity_core.GFserviceInfo,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
deletedBool := true
updateOp := &gf_identity_core.GFuserUpdateOp{
DeletedBool: &deletedBool,
}
// UPDATE_USER - mark user as email_confirmed
gfErr := gf_identity_core.DBsqlUserUpdate(pInput.UserID,
updateOp,
pCtx,
pRuntimeSys)
if gfErr != nil {
return gfErr
}
return nil
}
//------------------------------------------------
func AdminPipelineUserResendConfirmEmail(pInput *GFadminResendConfirmEmailInput,
pCtx context.Context,
pServiceInfo *gf_identity_core.GFserviceInfo,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
//------------------------
// VALIDATE_INPUT
gfErr := gf_core.ValidateStruct(pInput, pRuntimeSys)
if gfErr != nil {
return gfErr
}
//------------------------
if pServiceInfo.EnableEmailBool {
gfErr = gf_identity_core.UsersEmailPipelineVerify(pInput.EmailStr,
pInput.UserNameStr,
pInput.UserID,
pServiceInfo.DomainBaseStr,
pCtx,
pRuntimeSys)
if gfErr != nil {
return gfErr
}
}
return nil
}
//------------------------------------------------
func AdminPipelineGetAllUsers(pCtx context.Context,
pServiceInfo *gf_identity_core.GFserviceInfo,
pRuntimeSys *gf_core.RuntimeSys) ([]*GFadminUserViewOutput, *gf_core.GFerror) {
// DB
usersLst, gfErr := gf_identity_core.DBsqlUserGetAll(pCtx, pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
outputLst := []*GFadminUserViewOutput{}
for _, user := range usersLst {
userView := &GFadminUserViewOutput{
ID: user.ID,
CreationUNIXtimeF: user.CreationUNIXtimeF,
UserNameStr: user.UserNameStr,
ScreenNameStr: user.ScreenNameStr,
AddressesETHlst: user.AddressesETHlst,
EmailStr: user.EmailStr,
EmailConfirmedBool: user.EmailConfirmedBool,
ProfileImageURLstr: user.ProfileImageURLstr,
}
outputLst = append(outputLst, userView)
}
return outputLst, nil
}
//------------------------------------------------
func AdminPipelineGetAllInviteList(pCtx context.Context,
pServiceInfo *gf_identity_core.GFserviceInfo,
pRuntimeSys *gf_core.RuntimeSys) ([]map[string]interface{}, *gf_core.GFerror) {
// DB
dbInviteListLst, gfErr := gf_identity_core.DBsqlUserGetAllInInviteList(pCtx, pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
inviteListLst := []map[string]interface{}{}
for _, inviteMap := range dbInviteListLst {
inviteListLst = append(inviteListLst, map[string]interface{}{
"user_email_str": inviteMap["user_email_str"],
"creation_unix_time_f": inviteMap["creation_unix_time_f"],
})
}
return inviteListLst, nil
}
//------------------------------------------------
func AdminPipelineUserAddToInviteList(pInput *GFadminInputAddToInviteList,
pCtx context.Context,
pServiceInfo *gf_identity_core.GFserviceInfo,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
//------------------------
// VALIDATE_INPUT
gfErr := gf_core.ValidateStruct(pInput, pRuntimeSys)
if gfErr != nil {
return gfErr
}
//------------------------
gfErr = gf_identity_core.DBsqlUserAddToInviteList(pInput.EmailStr,
pCtx,
pRuntimeSys)
if gfErr != nil {
return gfErr
}
// EVENT
if pServiceInfo.EnableEventsAppBool {
adminUserNameStr, gfErr := gf_identity_core.DBsqlGetUserNameByID(pInput.AdminUserID, pCtx, pRuntimeSys)
if gfErr != nil {
return gfErr
}
eventMetaMap := map[string]interface{}{
"user_id": pInput.AdminUserID,
"user_name": adminUserNameStr,
"email_added_to_invite_list": pInput.EmailStr,
}
gf_events.EmitApp(gf_identity_core.GF_EVENT_APP__ADMIN_ADDED_USER_TO_INVITE_LIST,
eventMetaMap,
pRuntimeSys)
}
return nil
}
//------------------------------------------------
func AdminPipelineUserRemoveFromInviteList(pInput *GFadminRemoveFromInviteListInput,
pCtx context.Context,
pServiceInfo *gf_identity_core.GFserviceInfo,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
//------------------------
// VALIDATE_INPUT
gfErr := gf_core.ValidateStruct(pInput, pRuntimeSys)
if gfErr != nil {
return gfErr
}
//------------------------
gfErr = gf_identity_core.DBsqlUserRemoveFromInviteList(pInput.EmailStr,
pCtx,
pRuntimeSys)
if gfErr != nil {
return gfErr
}
// EVENT
if pServiceInfo.EnableEventsAppBool {
adminUserNameStr, gfErr := gf_identity_core.DBsqlGetUserNameByID(pInput.AdminUserID, pCtx, pRuntimeSys)
if gfErr != nil {
return gfErr
}
eventMetaMap := map[string]interface{}{
"user_id": pInput.AdminUserID,
"user_name": adminUserNameStr,
"email_added_to_invite_list": pInput.EmailStr,
}
gf_events.EmitApp(gf_identity_core.GF_EVENT_APP__ADMIN_REMOVED_USER_FROM_INVITE_LIST,
eventMetaMap,
pRuntimeSys)
}
return nil
}
//---------------------------------------------------
// PIPELINE__LOGIN
// this function is entered mutliple times for complex logins where not only pass/eth_signature
// are verified, but where email/mfa have to be confirmed as well.
// for each of the login stages this function is entered, and the login_attempt record
// is used to keep track of which stages have completed.
func AdminPipelineLogin(pInput *GFadminInputLogin,
pCtx context.Context,
pLocalHub *sentry.Hub,
pServiceInfo *gf_identity_core.GFserviceInfo,
pRuntimeSys *gf_core.RuntimeSys) (*GFadminOutputLogin, *gf_core.GFerror) {
//------------------------
// VALIDATE_INPUT
gfErr := gf_core.ValidateStruct(pInput, pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
//------------------------
userNameStr := gf_identity_core.GFuserName(pInput.UserNameStr)
output := &GFadminOutputLogin{}
//------------------------
// VERIFY
userExistsBool, gfErr := gf_identity_core.DBsqlUserExistsByUsername(userNameStr,
pCtx,
pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
// BREADCRUMB
gf_core.BreadcrumbsAdd("auth", "admin user checked for existence",
map[string]interface{}{"user_exists_bool": userExistsBool, "user_name_str": pInput.UserNameStr},
pLocalHub)
var userID gf_core.GF_ID
// admin user doesnt exist
if !userExistsBool {
// so create it but only if its the root admin user.
// other admin users have to be created explicitly
if pInput.UserNameStr == "admin" {
//------------------------
// PIPELINE__CREATE_ADMIN
// if the admin user doesnt exist in the DB (most likely on first run of gloflow server),
// create one in the DB
inputCreate := &gf_identity_core.GFuserpassInputCreate{
UserNameStr: userNameStr,
PassStr: pInput.PassStr,
EmailStr: pInput.EmailStr,
UserTypeStr: "admin",
}
// BREADCRUMB
gf_core.BreadcrumbsAdd("auth", "creating new admin user",
map[string]interface{}{"email_str": pInput.EmailStr, "user_name_str": pInput.UserNameStr},
pLocalHub)
// CREATE
output_create, gfErr := adminPipelineCreateAdmin(inputCreate,
pServiceInfo,
pCtx,
pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
//------------------------
userID = output_create.General.UserID
output.UserExistsBool = true
} else {
output.UserExistsBool = false
return output, nil
}
} else {
existingUserIDstr, gfErr := gf_identity_core.DBsqlGetBasicInfoByUsername(userNameStr,
pCtx,
pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
userID = existingUserIDstr
output.UserExistsBool = true
}
// BREADCRUMB
gf_core.BreadcrumbsAdd("auth", "got user_id for admin user",
map[string]interface{}{"user_id_str": userID, "user_name_str": pInput.UserNameStr},
pLocalHub)
//------------------------
// LOGIN_ATTEMPT
userTypeStr := "admin"
loginAttempt, gfErr := gf_identity_core.LoginAttemptGetOrCreate(userNameStr, userTypeStr, pCtx, pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
//------------------------
// VERIFY_PASSWORD
// only verify password if the login_attempt didnt mark it yet as complete
if !loginAttempt.PassConfirmedBool {
passValidBool, gfErr := gf_identity_core.UserpassVerifyPass(userNameStr,
pInput.PassStr,
pServiceInfo,
pCtx,
pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
if !passValidBool {
output.PassValidBool = false
return output, nil
} else {
output.PassValidBool = true
//------------------------
// UPDATE_LOGIN_ATTEMPT
// if password is valid then update the login_attempt
// to indicate that the password has been confirmed
updateOp := &gf_identity_core.GFloginAttemptUpdateOp{PassConfirmedBool: &passValidBool}
gfErr = gf_identity_core.DBsqlLoginAttemptUpdate(loginAttempt.ID,
updateOp,
pCtx,
pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
//------------------------
// EVENT
if pServiceInfo.EnableEventsAppBool {
eventMeta := map[string]interface{}{
"user_id": userID,
"user_name": pInput.UserNameStr,
"domain_base": pServiceInfo.DomainBaseStr,
}
gf_events.EmitApp(gf_identity_core.GF_EVENT_APP__ADMIN_LOGIN_PASS_CONFIRMED,
eventMeta,
pRuntimeSys)
}
}
}
//------------------------
// EMAIL
if pServiceInfo.EnableEmailBool {
// go through the email verification pipeline if the email
// has not yet been confirmed
if !loginAttempt.EmailConfirmedBool {
gfErr = gf_identity_core.UsersEmailPipelineVerify(pInput.EmailStr,
userNameStr,
userID,
pServiceInfo.DomainBaseStr,
pCtx,
pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
// EVENT
if pServiceInfo.EnableEventsAppBool {
eventMeta := map[string]interface{}{
"user_id": userID,
"user_name": pInput.UserNameStr,
"domain_base": pServiceInfo.DomainBaseStr,
}
gf_events.EmitApp(gf_identity_core.GF_EVENT_APP__ADMIN_LOGIN_EMAIL_VERIFICATION_SENT,
eventMeta,
pRuntimeSys)
}
//------------------------
}
}
//------------------------
return output, nil
}
//---------------------------------------------------
func adminPipelineCreateAdmin(pInput *gf_identity_core.GFuserpassInputCreate,
pServiceInfo *gf_identity_core.GFserviceInfo,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) (*GFadminOutputCreateAdmin, *gf_core.GFerror) {
//------------------------
// PIPELINE
output, gfErr := gf_identity_core.UserpassPipelineCreate(pInput,
pServiceInfo,
pCtx,
pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
//------------------------
// EVENT
if pServiceInfo.EnableEventsAppBool {
eventMeta := map[string]interface{}{
"user_id": output.UserID,
"user_name": pInput.UserNameStr,
"user_type": pInput.UserTypeStr,
"domain_base": pServiceInfo.DomainBaseStr,
}
gf_events.EmitApp(gf_identity_core.GF_EVENT_APP__ADMIN_CREATE,
eventMeta,
pRuntimeSys)
}
//------------------------
outputAdmin := &GFadminOutputCreateAdmin{
General: output,
}
return outputAdmin, nil
}
//---------------------------------------------------
func AdminIs(pUserIDstr gf_core.GF_ID,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
userNameStr, gfErr := gf_identity_core.DBsqlGetUserNameByID(pUserIDstr, pCtx, pRuntimeSys)
if gfErr != nil {
return gfErr
}
if string(userNameStr) != "admin" {
gfErr := gf_core.ErrorCreate("username thats not 'admin' is trying to login as admin",
"verify__invalid_value_error",
map[string]interface{}{
"user_name_str": userNameStr,
},
nil, "gf_identity", pRuntimeSys)
return gfErr
}
return nil
}