diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-08-06 14:14:48 -0400 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-08-06 15:48:41 -0400 |
commit | 34ba44274bcb67f4598ee2e18feb5b68a3984e48 (patch) | |
tree | 102bf0d649749ed7f4bf409866c2ead12d3211cf | |
parent | 77c49c4c86130385065b940b75282d1745a6473f (diff) |
it works ??
-rw-r--r-- | main.go | 29 |
1 files changed, 18 insertions, 11 deletions
@@ -7,6 +7,7 @@ import ( "net" "net/http" "regexp" + // "strconv" "strings" "time" ) @@ -21,11 +22,11 @@ type accessLog struct { } type loginAttemptLog struct { - Email json.RawMessage `json:"email"` - Password string `json:"password"` - LoginTime time.Time `json:"loginTime"` - Success bool `json:"success"` - RememberMe bool `json:"rememberMe"` + Email string `json:"email"` + Password string `json:"password"` + LoginTime time.Time `json:"loginTime"` + Success bool `json:"success"` + RememberMe bool `json:"rememberMe"` } func main() { @@ -50,18 +51,24 @@ func main() { http.HandleFunc("/logs", createGetLogs(&logs)) http.HandleFunc("/v1/auth/login", func(w http.ResponseWriter, r *http.Request) { - var data map[string]any - body, _ := io.ReadAll(r.Body) - defer r.Body.Close() - err := json.Unmarshal([]byte(body), &data) + body, err := io.ReadAll(r.Body) if err != nil { - addRotLog(&logs, &n, string(body)) + http.Error(w, "Bad Request", http.StatusBadRequest) + return + } + defer r.Body.Close() + + // Attempt to parse JSON (optional, if you still want to try) + var data map[string]any + if json.Unmarshal(body, &data) != nil { + addRotLog(&logs, &n, string(body)) // Logs unmodified string http.Error(w, "Forbidden", http.StatusForbidden) return } if email, ok := data["email"].(string); ok { if rememberMe, ok := data["rememberMe"].(bool); ok { + fmt.Println("Successfully unmarshalled json") addRotLog(&logs, &n, fmt.Sprintf(`{"email": "%s", "password": "XXXXXXXX", "loginTime": "%s", "success": false, "rememberMe": %t}`, email, time.Now().UTC(), rememberMe)) } } @@ -86,7 +93,7 @@ func createGetLogs(logs *[log_length]string) func(http.ResponseWriter, *http.Req } func addRotLog(logs *[log_length]string, last *int, value string) { - // fmt.Printf("%d %t %s\n", *last, strings.Contains(value, "\n"), value) + fmt.Printf("%d %t %v\n", *last, strings.Contains(value, "\n"), value) if strings.Contains(value, "\n") { for _, v := range strings.Split(value, "\n") { addRotLog(logs, last, v) |