mirror of
https://github.com/optim-enterprises-bv/databunker.git
synced 2025-11-03 19:37:48 +00:00
refactor code
This commit is contained in:
@@ -28,26 +28,26 @@ func (e mainEnv) createSession(w http.ResponseWriter, r *http.Request, ps httpro
|
||||
return
|
||||
}
|
||||
expiration := e.conf.Policy.MaxSessionRetentionPeriod
|
||||
parsedData, err := getJSONPost(r, e.conf.Sms.DefaultCountry)
|
||||
userJSON, err := getUserJSON(r, e.conf.Sms.DefaultCountry)
|
||||
if err != nil {
|
||||
returnError(w, r, "failed to decode request body", 405, err, event)
|
||||
return
|
||||
}
|
||||
if len(parsedData.jsonData) == 0 {
|
||||
if len(userJSON.jsonData) == 0 {
|
||||
returnError(w, r, "empty request body", 405, nil, event)
|
||||
return
|
||||
}
|
||||
var userBson bson.M
|
||||
if len(parsedData.loginIdx) > 0 {
|
||||
userBson, err = e.db.lookupUserRecordByIndex("login", parsedData.loginIdx, e.conf)
|
||||
} else if len(parsedData.emailIdx) > 0 {
|
||||
userBson, err = e.db.lookupUserRecordByIndex("email", parsedData.emailIdx, e.conf)
|
||||
} else if len(parsedData.phoneIdx) > 0 {
|
||||
userBson, err = e.db.lookupUserRecordByIndex("phone", parsedData.phoneIdx, e.conf)
|
||||
} else if len(parsedData.customIdx) > 0 {
|
||||
userBson, err = e.db.lookupUserRecordByIndex("custom", parsedData.customIdx, e.conf)
|
||||
} else if len(parsedData.token) > 0 {
|
||||
userBson, err = e.db.lookupUserRecord(parsedData.token)
|
||||
if len(userJSON.loginIdx) > 0 {
|
||||
userBson, err = e.db.lookupUserRecordByIndex("login", userJSON.loginIdx, e.conf)
|
||||
} else if len(userJSON.emailIdx) > 0 {
|
||||
userBson, err = e.db.lookupUserRecordByIndex("email", userJSON.emailIdx, e.conf)
|
||||
} else if len(userJSON.phoneIdx) > 0 {
|
||||
userBson, err = e.db.lookupUserRecordByIndex("phone", userJSON.phoneIdx, e.conf)
|
||||
} else if len(userJSON.customIdx) > 0 {
|
||||
userBson, err = e.db.lookupUserRecordByIndex("custom", userJSON.customIdx, e.conf)
|
||||
} else if len(userJSON.token) > 0 {
|
||||
userBson, err = e.db.lookupUserRecord(userJSON.token)
|
||||
}
|
||||
if err != nil {
|
||||
returnError(w, r, "internal error", 405, err, event)
|
||||
@@ -59,7 +59,7 @@ func (e mainEnv) createSession(w http.ResponseWriter, r *http.Request, ps httpro
|
||||
userTOKEN = userBson["token"].(string)
|
||||
event.Record = userTOKEN
|
||||
}
|
||||
session, err = e.db.createSessionRecord(session, userTOKEN, expiration, parsedData.jsonData)
|
||||
session, err = e.db.createSessionRecord(session, userTOKEN, expiration, userJSON.jsonData)
|
||||
if err != nil {
|
||||
returnError(w, r, "internal error", 405, err, event)
|
||||
return
|
||||
|
||||
@@ -21,23 +21,23 @@ func (e mainEnv) userCreate(w http.ResponseWriter, r *http.Request, ps httproute
|
||||
return
|
||||
}
|
||||
}
|
||||
parsedData, err := getJSONPost(r, e.conf.Sms.DefaultCountry)
|
||||
userJSON, err := getUserJSON(r, e.conf.Sms.DefaultCountry)
|
||||
if err != nil {
|
||||
returnError(w, r, "failed to decode request body", 405, err, event)
|
||||
return
|
||||
}
|
||||
if len(parsedData.jsonData) == 0 {
|
||||
if len(userJSON.jsonData) == 0 {
|
||||
returnError(w, r, "empty request body", 405, nil, event)
|
||||
return
|
||||
}
|
||||
err = validateUserRecord(parsedData.jsonData)
|
||||
err = validateUserRecord(userJSON.jsonData)
|
||||
if err != nil {
|
||||
returnError(w, r, "user schema error: "+err.Error(), 405, err, event)
|
||||
return
|
||||
}
|
||||
// make sure that login, email and phone are unique
|
||||
if len(parsedData.loginIdx) > 0 {
|
||||
otherUserBson, err := e.db.lookupUserRecordByIndex("login", parsedData.loginIdx, e.conf)
|
||||
if len(userJSON.loginIdx) > 0 {
|
||||
otherUserBson, err := e.db.lookupUserRecordByIndex("login", userJSON.loginIdx, e.conf)
|
||||
if err != nil {
|
||||
returnError(w, r, "internal error", 405, err, event)
|
||||
return
|
||||
@@ -47,8 +47,8 @@ func (e mainEnv) userCreate(w http.ResponseWriter, r *http.Request, ps httproute
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(parsedData.emailIdx) > 0 {
|
||||
otherUserBson, err := e.db.lookupUserRecordByIndex("email", parsedData.emailIdx, e.conf)
|
||||
if len(userJSON.emailIdx) > 0 {
|
||||
otherUserBson, err := e.db.lookupUserRecordByIndex("email", userJSON.emailIdx, e.conf)
|
||||
if err != nil {
|
||||
returnError(w, r, "internal error", 405, err, event)
|
||||
return
|
||||
@@ -58,8 +58,8 @@ func (e mainEnv) userCreate(w http.ResponseWriter, r *http.Request, ps httproute
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(parsedData.phoneIdx) > 0 {
|
||||
otherUserBson, err := e.db.lookupUserRecordByIndex("phone", parsedData.phoneIdx, e.conf)
|
||||
if len(userJSON.phoneIdx) > 0 {
|
||||
otherUserBson, err := e.db.lookupUserRecordByIndex("phone", userJSON.phoneIdx, e.conf)
|
||||
if err != nil {
|
||||
returnError(w, r, "internal error", 405, err, event)
|
||||
return
|
||||
@@ -69,8 +69,8 @@ func (e mainEnv) userCreate(w http.ResponseWriter, r *http.Request, ps httproute
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(parsedData.customIdx) > 0 {
|
||||
otherUserBson, err := e.db.lookupUserRecordByIndex("custom", parsedData.customIdx, e.conf)
|
||||
if len(userJSON.customIdx) > 0 {
|
||||
otherUserBson, err := e.db.lookupUserRecordByIndex("custom", userJSON.customIdx, e.conf)
|
||||
if err != nil {
|
||||
returnError(w, r, "internal error", 405, err, event)
|
||||
return
|
||||
@@ -80,29 +80,29 @@ func (e mainEnv) userCreate(w http.ResponseWriter, r *http.Request, ps httproute
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(parsedData.loginIdx) == 0 &&
|
||||
len(parsedData.emailIdx) == 0 &&
|
||||
len(parsedData.phoneIdx) == 0 &&
|
||||
len(parsedData.customIdx) == 0 {
|
||||
if len(userJSON.loginIdx) == 0 &&
|
||||
len(userJSON.emailIdx) == 0 &&
|
||||
len(userJSON.phoneIdx) == 0 &&
|
||||
len(userJSON.customIdx) == 0 {
|
||||
returnError(w, r, "failed to create user, all user lookup fields are missing", 405, err, event)
|
||||
return
|
||||
}
|
||||
|
||||
userTOKEN, err := e.db.createUserRecord(parsedData, event)
|
||||
userTOKEN, err := e.db.createUserRecord(userJSON, event)
|
||||
if err != nil {
|
||||
returnError(w, r, "internal error", 405, err, event)
|
||||
return
|
||||
}
|
||||
encPhoneIdx := ""
|
||||
if len(parsedData.emailIdx) > 0 {
|
||||
encEmailIdx, _ := basicStringEncrypt(parsedData.emailIdx, e.db.masterKey, e.db.GetCode())
|
||||
if len(userJSON.emailIdx) > 0 {
|
||||
encEmailIdx, _ := basicStringEncrypt(userJSON.emailIdx, e.db.masterKey, e.db.GetCode())
|
||||
e.db.linkAgreementRecords(userTOKEN, encEmailIdx)
|
||||
}
|
||||
if len(parsedData.phoneIdx) > 0 {
|
||||
encPhoneIdx, _ = basicStringEncrypt(parsedData.phoneIdx, e.db.masterKey, e.db.GetCode())
|
||||
if len(userJSON.phoneIdx) > 0 {
|
||||
encPhoneIdx, _ = basicStringEncrypt(userJSON.phoneIdx, e.db.masterKey, e.db.GetCode())
|
||||
e.db.linkAgreementRecords(userTOKEN, encPhoneIdx)
|
||||
}
|
||||
if len(parsedData.emailIdx) > 0 && len(parsedData.phoneIdx) > 0 {
|
||||
if len(userJSON.emailIdx) > 0 && len(userJSON.phoneIdx) > 0 {
|
||||
// delete duplicate consent records for user
|
||||
records, _ := e.db.store.GetList(storage.TblName.Agreements, "token", userTOKEN, 0, 0, "")
|
||||
var briefCodes []string
|
||||
@@ -117,7 +117,7 @@ func (e mainEnv) userCreate(w http.ResponseWriter, r *http.Request, ps httproute
|
||||
event.Record = userTOKEN
|
||||
returnUUID(w, userTOKEN)
|
||||
notifyURL := e.conf.Notification.NotificationURL
|
||||
notifyProfileNew(notifyURL, parsedData.jsonData, "token", userTOKEN)
|
||||
notifyProfileNew(notifyURL, userJSON.jsonData, "token", userTOKEN)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ func normalizeEmail(email0 string) string {
|
||||
email = strings.ToLower(email)
|
||||
email = strings.TrimSpace(email)
|
||||
if email0 != email {
|
||||
log.Printf("email before: %s, after: %s\n", email0, email)
|
||||
log.Printf("Email before normalization: %s, after: %s\n", email0, email)
|
||||
}
|
||||
return email
|
||||
}
|
||||
@@ -350,7 +350,7 @@ func stringPatternMatch(pattern string, value string) bool {
|
||||
}
|
||||
|
||||
func returnError(w http.ResponseWriter, r *http.Request, message string, code int, err error, event *auditEvent) {
|
||||
log.Printf("Return error: %d %s %s\n", code, r.Method, r.URL.Path)
|
||||
log.Printf("[%d] %s %s -> Return error\n", code, r.Method, r.URL.Path)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(code)
|
||||
fmt.Fprintf(w, `{"status":"error","message":%q}`, message)
|
||||
@@ -584,7 +584,7 @@ func getIndexString(val interface{}) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func getJSONPost(r *http.Request, defaultCountry string) (userJSON, error) {
|
||||
func getUserJSON(r *http.Request, defaultCountry string) (userJSON, error) {
|
||||
var result userJSON
|
||||
records, err := getJSONPostMap(r)
|
||||
if err != nil {
|
||||
|
||||
@@ -67,7 +67,7 @@ func TestUtilGetJSONPost(t *testing.T) {
|
||||
for _, value := range goodJsons {
|
||||
request := httptest.NewRequest("POST", "/user", strings.NewReader(value))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
result, err := getJSONPost(request, "IL")
|
||||
result, err := getUserJSON(request, "IL")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse json: %s, err: %s\n", value, err)
|
||||
}
|
||||
@@ -83,7 +83,7 @@ func TestUtilGetJSONPost(t *testing.T) {
|
||||
for _, value := range badJsons {
|
||||
request := httptest.NewRequest("POST", "/user", strings.NewReader(value))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
result, err := getJSONPost(request, "IL")
|
||||
result, err := getUserJSON(request, "IL")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse json: %s, err: %s\n", value, err)
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ func TestUserLoginDelete(t *testing.T) {
|
||||
t.Fatalf("Failed to create user login: %s", raw["message"].(string))
|
||||
}
|
||||
xtoken := raw["xtoken"].(string)
|
||||
log.Printf("User login *** xtoken: %s\n", xtoken)
|
||||
log.Printf("User login *** xtoken: %s...\n", xtoken[0:8])
|
||||
oldRootToken := rootToken
|
||||
rootToken = xtoken
|
||||
raw, _ = helpAcceptAgreement("token", userTOKEN, "contract1", "")
|
||||
|
||||
Reference in New Issue
Block a user