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