Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
stremovsky committed Aug 12, 2024
1 parent 0a537e9 commit cb7df56
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 41 deletions.
26 changes: 13 additions & 13 deletions src/sessions_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
44 changes: 22 additions & 22 deletions src/users_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions src/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion src/xtokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
Expand Down

0 comments on commit cb7df56

Please sign in to comment.