diff --git a/Go/internal/handlers/endpoint/dashboard/dashboards_id_handler.go b/Go/internal/handlers/endpoint/dashboard/dashboards_id_handler.go index db30e52dc0e2871e8e1bd4ca9d161116caf1dbbb..903c63e21b7aefa3666929620a877ad17145a19c 100644 --- a/Go/internal/handlers/endpoint/dashboard/dashboards_id_handler.go +++ b/Go/internal/handlers/endpoint/dashboard/dashboards_id_handler.go @@ -27,7 +27,7 @@ func DashboardsIdHandler(w http.ResponseWriter, r *http.Request) { handleDashboardGetRequest(w, r) default: // Log and return an error for unsupported HTTP methods - log.Printf(constants.ClientConnectUnsupported, Endpoints.DashboardsID, r.Method) + log.Printf(constants.ClientConnectUnsupported, r.RemoteAddr, Endpoints.DashboardsID, r.Method) http.Error(w, "REST Method: "+r.Method+" not supported. Only supported methods for this endpoint is:\n"+http.MethodGet, http.StatusNotImplemented) return } @@ -39,26 +39,26 @@ func handleDashboardGetRequest(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() // Extract the query parameters. token := query.Get("token") // Retrieve token from URL query parameters. if token == "" { // Check if a token is provided. - log.Printf(constants.ClientConnectNoToken, r.Method, Endpoints.DashboardsID) + log.Printf(constants.ClientConnectNoToken, r.RemoteAddr, r.Method, Endpoints.DashboardsID) http.Error(w, ProvideAPI, http.StatusUnauthorized) return } UUID := db.GetAPIKeyUUID(token) // Retrieve UUID associated with API token. if UUID == "" { // Check if UUID is retrieved. - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.DashboardsID) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.DashboardsID) err := fmt.Sprintf(APINotAccepted) http.Error(w, err, http.StatusNotAcceptable) return } if ID == "" || ID == " " { // Check if the ID is valid. - log.Printf(constants.ClientConnectNoID, r.Method, Endpoints.DashboardsID) + log.Printf(constants.ClientConnectNoID, r.RemoteAddr, r.Method, Endpoints.DashboardsID) http.Error(w, ProvideID, http.StatusBadRequest) return } reg, err := db.GetSpecificRegistration(ID, UUID) // Retrieve registration by ID for user (UUID). if err != nil { - log.Printf("Error getting registration: %v", err) + log.Printf("%s: Error getting registration: %v", r.RemoteAddr, err) err := fmt.Sprintf("Dashboard doesn't exist: %v", err) http.Error(w, err, http.StatusNotFound) return diff --git a/Go/internal/handlers/endpoint/dashboard/notifications_handler.go b/Go/internal/handlers/endpoint/dashboard/notifications_handler.go index 28687ed38b743cdc99869df9918526c5ed752e08..f5464e14ee32e3fdb4f7273770af7c41567ab2b5 100644 --- a/Go/internal/handlers/endpoint/dashboard/notifications_handler.go +++ b/Go/internal/handlers/endpoint/dashboard/notifications_handler.go @@ -22,7 +22,7 @@ func NotificationsHandler(w http.ResponseWriter, r *http.Request) { handleNotifGetAllRequest(w, r) default: // Log and return an error for unsupported HTTP methods - log.Printf(constants.ClientConnectUnsupported, Endpoints.Notifications, r.Method) + log.Printf(constants.ClientConnectUnsupported, r.RemoteAddr, Endpoints.Notifications, r.Method) http.Error(w, "REST Method: "+r.Method+" not supported. Only supported methods for this endpoint is:\n"+http.MethodPost+"\n"+http.MethodGet, http.StatusNotImplemented) return } @@ -33,20 +33,20 @@ func handleNotifPostRequest(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() // Extract the query parameters. token := query.Get("token") // Retrieve token from the URL query parameters. if token == "" { // Check if a token is provided. - log.Printf(constants.ClientConnectNoToken, r.Method, Endpoints.Notifications) + log.Printf(constants.ClientConnectNoToken, r.RemoteAddr, r.Method, Endpoints.Notifications) http.Error(w, ProvideAPI, http.StatusUnauthorized) return } UUID := db.GetAPIKeyUUID(token) // Retrieve UUID associated with the API token. if UUID == "" { // Check if UUID is valid. - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.Notifications) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.Notifications) err := fmt.Sprintf(APINotAccepted) http.Error(w, err, http.StatusNotAcceptable) return } if r.Body == nil { // Check if request body is empty. - log.Printf(constants.ClientConnectEmptyBody, r.Method, Endpoints.Notifications) + log.Printf(constants.ClientConnectEmptyBody, r.RemoteAddr, r.Method, Endpoints.Notifications) err := fmt.Sprintf("Please send a request body") http.Error(w, err, http.StatusBadRequest) return @@ -100,20 +100,20 @@ func handleNotifGetAllRequest(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() // Extract the query parameters. token := query.Get("token") // Retrieve token from the URL query parameters. if token == "" { // Check if a token is provided. - log.Printf(constants.ClientConnectNoToken, r.Method, Endpoints.Notifications) + log.Printf(constants.ClientConnectNoToken, r.RemoteAddr, r.Method, Endpoints.Notifications) http.Error(w, ProvideAPI, http.StatusUnauthorized) return } UUID := db.GetAPIKeyUUID(token) // Retrieve UUID associated with the API token. if UUID == "" { // Check if UUID is valid. - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.Notifications) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.Notifications) err := fmt.Sprintf(APINotAccepted) http.Error(w, err, http.StatusNotAcceptable) return } regs, err := db.GetWebhooksUser(UUID) // Retrieve all webhooks associated with the user (UUID). if err != nil { - log.Printf("Error retrieving webhooks from database: %v", err) + log.Printf("%s: Error retrieving webhooks from database: %v", r.RemoteAddr, err) errmsg := fmt.Sprint("Error retrieving webhooks from database: ", err) http.Error(w, errmsg, http.StatusInternalServerError) return diff --git a/Go/internal/handlers/endpoint/dashboard/notifications_id_handler.go b/Go/internal/handlers/endpoint/dashboard/notifications_id_handler.go index 22ad4d78747e9d86b53ff344468c90f9da96b4ee..bff6ee50c1ec1c266a14eae0cf6df62de3350351 100644 --- a/Go/internal/handlers/endpoint/dashboard/notifications_id_handler.go +++ b/Go/internal/handlers/endpoint/dashboard/notifications_id_handler.go @@ -20,7 +20,7 @@ func NotificationsIdHandler(w http.ResponseWriter, r *http.Request) { handleNotifDeleteRequest(w, r) default: // Log and return an error for unsupported HTTP methods - log.Printf(constants.ClientConnectUnsupported, Endpoints.NotificationsID, r.Method) + log.Printf(constants.ClientConnectUnsupported, r.RemoteAddr, Endpoints.NotificationsID, r.Method) http.Error(w, "REST Method: "+r.Method+" not supported. Only supported methods for this endpoint are:\n"+http.MethodGet+"\n"+http.MethodDelete, http.StatusNotImplemented) return } @@ -32,26 +32,26 @@ func handleNotifGetRequest(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() // Extract the query parameters. token := query.Get("token") // Retrieve token from the URL query parameters. if token == "" { // Check if a token is provided. - log.Printf(constants.ClientConnectNoToken, r.Method, Endpoints.NotificationsID) + log.Printf(constants.ClientConnectNoToken, r.RemoteAddr, r.Method, Endpoints.NotificationsID) http.Error(w, ProvideAPI, http.StatusUnauthorized) return } UUID := db.GetAPIKeyUUID(token) // Retrieve UUID associated with the API token. if UUID == "" { // Check if UUID is valid. - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.NotificationsID) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.NotificationsID) err := fmt.Sprintf(APINotAccepted) http.Error(w, err, http.StatusNotAcceptable) return } if ID == "" || ID == " " { // Check if the ID is valid. - log.Printf(constants.ClientConnectNoID, r.Method, Endpoints.NotificationsID) + log.Printf(constants.ClientConnectNoID, r.RemoteAddr, r.Method, Endpoints.NotificationsID) http.Error(w, ProvideID, http.StatusBadRequest) return } hook, err := db.GetSpecificWebhook(ID, UUID) // Retrieve the specific webhook by ID and UUID. if err != nil { - log.Printf("Error getting webhook from database: %v", err) + log.Printf("%s: Error getting webhook from database: %v", r.RemoteAddr, err) err := fmt.Sprintf("Error getting webhook from database: %v", err) http.Error(w, err, http.StatusNotFound) return @@ -75,26 +75,26 @@ func handleNotifDeleteRequest(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() // Extract the query parameters. token := query.Get("token") // Retrieve token from the URL query parameters. if token == "" { // Check if a token is provided. - log.Printf(constants.ClientConnectNoToken, r.Method, Endpoints.NotificationsID) + log.Printf(constants.ClientConnectNoToken, r.RemoteAddr, r.Method, Endpoints.NotificationsID) http.Error(w, ProvideAPI, http.StatusUnauthorized) return } UUID := db.GetAPIKeyUUID(token) // Retrieve UUID associated with the API token. if UUID == "" { // Check if UUID is valid. - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.NotificationsID) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.NotificationsID) err := fmt.Sprintf(APINotAccepted) http.Error(w, err, http.StatusNotAcceptable) return } if ID == "" || ID == " " { // Check if the ID is valid. - log.Printf(constants.ClientConnectNoID, r.Method, Endpoints.NotificationsID) + log.Printf(constants.ClientConnectNoID, r.RemoteAddr, r.Method, Endpoints.NotificationsID) http.Error(w, ProvideID, http.StatusBadRequest) return } err := db.DeleteWebhook(ID, UUID) // Delete the specific webhook by ID and UUID from the database. if err != nil { - log.Printf("Error deleting data from database: %v", err) + log.Printf(" %s: Error deleting data from database: %v", r.RemoteAddr, err) err := fmt.Sprintf("Error deleting data from database: %v", err) http.Error(w, err, http.StatusInternalServerError) return diff --git a/Go/internal/handlers/endpoint/dashboard/registrations_handler.go b/Go/internal/handlers/endpoint/dashboard/registrations_handler.go index b382dc3230bb77aac1a50ae67004bc529077dd26..e5cab6ae2a41cd204d9d0439d312b5ca6b422fc4 100644 --- a/Go/internal/handlers/endpoint/dashboard/registrations_handler.go +++ b/Go/internal/handlers/endpoint/dashboard/registrations_handler.go @@ -32,7 +32,7 @@ func RegistrationsHandler(w http.ResponseWriter, r *http.Request) { handleRegGetAllRequest(w, r) // Handle GET requests default: // Log and return an error for unsupported HTTP methods - log.Printf(constants.ClientConnectUnsupported, Endpoints.Registrations, r.Method) + log.Printf(constants.ClientConnectUnsupported, r.RemoteAddr, Endpoints.Registrations, r.Method) http.Error(w, "REST Method: "+r.Method+" not supported. Only supported methods for this endpoint is:\n"+http.MethodPost+"\n"+http.MethodGet+"\n"+http.MethodPatch, http.StatusNotImplemented) return } @@ -58,20 +58,20 @@ func handleRegPostRequest(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() // Extract the query parameters. token := query.Get("token") // Extract the 'token' parameter from the query. if token == "" { // Validate token presence. - log.Printf(constants.ClientConnectNoToken, r.Method, Endpoints.Registrations) + log.Printf(constants.ClientConnectNoToken, r.RemoteAddr, r.Method, Endpoints.Registrations) http.Error(w, ProvideAPI, http.StatusUnauthorized) return } UUID := db.GetAPIKeyUUID(token) // Retrieve the UUID for the API key. if UUID == "" { // Validate UUID presence. - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.Registrations) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.Registrations) err := fmt.Sprintf(APINotAccepted) http.Error(w, err, http.StatusNotAcceptable) return } if r.Body == nil { // Validate that the request body is not empty. - log.Printf(constants.ClientConnectEmptyBody, r.Method, Endpoints.Registrations) + log.Printf(constants.ClientConnectEmptyBody, r.RemoteAddr, r.Method, Endpoints.Registrations) err := fmt.Sprintf("Please send a request body") http.Error(w, err, http.StatusBadRequest) return @@ -79,7 +79,7 @@ func handleRegPostRequest(w http.ResponseWriter, r *http.Request) { ci, err := DecodeCountryInfo(r.Body) // Decode request body into CountryInfoInternal struct. if err != nil { - log.Printf("Error decoding request body: %v", err) + log.Printf("%s: Error decoding request body: %v", r.RemoteAddr, err) err := fmt.Sprintf("Error decoding request body: %v", err) http.Error(w, err, http.StatusBadRequest) return @@ -136,20 +136,20 @@ func handleRegGetAllRequest(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() // Extract the query parameters. token := query.Get("token") // Extract the 'token' parameter from the query. if token == "" { // Validate token presence. - log.Printf(constants.ClientConnectNoToken, r.Method, Endpoints.Registrations) + log.Printf(constants.ClientConnectNoToken, r.RemoteAddr, r.Method, Endpoints.Registrations) http.Error(w, ProvideAPI, http.StatusUnauthorized) return } UUID := db.GetAPIKeyUUID(token) // Extract the UUID parameter from the API token. if UUID == "" { // Validate UUID presence. - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.Registrations) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.Registrations) err := fmt.Sprintf(APINotAccepted) http.Error(w, err, http.StatusNotAcceptable) return } regs, err := db.GetRegistrations(UUID) // Retrieve the user's Registrations. if err != nil { - log.Printf("Error retrieving documents from database: %s", err) + log.Printf("%s: Error retrieving documents from database: %s", r.RemoteAddr, err) errmsg := fmt.Sprint("Error retrieving documents from database: ", err) http.Error(w, errmsg, http.StatusInternalServerError) return diff --git a/Go/internal/handlers/endpoint/dashboard/registrations_id_handler.go b/Go/internal/handlers/endpoint/dashboard/registrations_id_handler.go index 98ebc1b5c8e6cfa8e49dac6f26dc05207c5a6979..2ae36ac6e33d3c049a79937dd1b0001fef167f9c 100644 --- a/Go/internal/handlers/endpoint/dashboard/registrations_id_handler.go +++ b/Go/internal/handlers/endpoint/dashboard/registrations_id_handler.go @@ -18,9 +18,9 @@ import ( // Constants for error and informational messages. const ( - ProvideID = "Please Provide ID" // Message to request ID provision when missing. - RegistrationRetrivalError = "Error getting registration: %v" // Error message template for retrieval issues. - RegistrationPatchError = "Error patching registration: %v" // Error message template for patching issues. + ProvideID = "Please Provide ID" // Message to request ID provision when missing. + RegistrationRetrivalError = "%s: Error getting registration: %v" // Error message template for retrieval issues. + RegistrationPatchError = "%s: Error patching registration: %v" // Error message template for patching issues. ) // RegistrationsIdHandler handles requests for the /registrations/{ID} endpoint. @@ -34,7 +34,7 @@ func RegistrationsIdHandler(w http.ResponseWriter, r *http.Request) { handleRegDeleteRequest(w, r) default: // Log and return an error for unsupported HTTP methods - log.Printf(constants.ClientConnectUnsupported, Endpoints.RegistrationsID, r.Method) + log.Printf(constants.ClientConnectUnsupported, r.RemoteAddr, Endpoints.RegistrationsID, r.Method) http.Error(w, "REST Method: "+r.Method+" not supported. Only supported methods for this endpoint is:\n"+http.MethodGet+"\n"+http.MethodPatch+"\n"+http.MethodDelete, http.StatusNotImplemented) return } @@ -46,26 +46,26 @@ func handleRegGetRequest(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() // Extract the query parameters. token := query.Get("token") // Extract the 'token' parameter from the query. if token == "" { // Validate token presence. - log.Printf(constants.ClientConnectNoToken, r.Method, Endpoints.RegistrationsID) + log.Printf(constants.ClientConnectNoToken, r.RemoteAddr, r.Method, Endpoints.RegistrationsID) http.Error(w, ProvideAPI, http.StatusUnauthorized) return } UUID := db.GetAPIKeyUUID(token) // Retrieve the UUID for the API key. if UUID == "" { // Validate UUID presence. - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.RegistrationsID) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.RegistrationsID) err := fmt.Sprintf(APINotAccepted) http.Error(w, err, http.StatusNotAcceptable) return } if ID == "" || ID == " " { // Validate ID presence. - log.Printf(constants.ClientConnectNoID, r.Method, Endpoints.RegistrationsID) + log.Printf(constants.ClientConnectNoID, r.RemoteAddr, r.Method, Endpoints.RegistrationsID) http.Error(w, ProvideID, http.StatusBadRequest) return } reg, err := db.GetSpecificRegistration(ID, UUID) // Retrieve registration data from the database. if err != nil { - log.Printf(RegistrationRetrivalError, err) + log.Printf(RegistrationRetrivalError, r.RemoteAddr, err) http.Error(w, "Error retrieving data from database", http.StatusNotFound) return } @@ -97,25 +97,25 @@ func handleRegPatchRequest(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() // Extract the query parameters. token := query.Get("token") // Extract the 'token' parameter from the query. if token == "" { // Validate token presence. - log.Printf(constants.ClientConnectNoToken, r.Method, Endpoints.RegistrationsID) + log.Printf(constants.ClientConnectNoToken, r.RemoteAddr, r.Method, Endpoints.RegistrationsID) http.Error(w, ProvideAPI, http.StatusUnauthorized) return } UUID := db.GetAPIKeyUUID(token) // Retrieve the UUID for the API key. if UUID == "" { // Validate UUID presence. - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.RegistrationsID) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.RegistrationsID) err := fmt.Sprintf(APINotAccepted) http.Error(w, err, http.StatusNotAcceptable) return } if ID == "" || ID == " " { // Validate ID presence. - log.Printf(constants.ClientConnectNoID, r.Method, Endpoints.RegistrationsID) + log.Printf(constants.ClientConnectNoID, r.RemoteAddr, r.Method, Endpoints.RegistrationsID) http.Error(w, ProvideID, http.StatusBadRequest) return } if r.Body == nil { // Validate that the request body is not empty. - log.Printf(constants.ClientConnectEmptyBody, r.Method, Endpoints.RegistrationsID) + log.Printf(constants.ClientConnectEmptyBody, r.RemoteAddr, r.Method, Endpoints.RegistrationsID) err := fmt.Sprintf("Please send a request body") http.Error(w, err, http.StatusBadRequest) return @@ -123,23 +123,23 @@ func handleRegPatchRequest(w http.ResponseWriter, r *http.Request) { ci, err, errcode := patchCountryInformation(r, ID, UUID) // Process the patch request. if err != nil { - log.Printf(RegistrationPatchError, err) - err := fmt.Sprintf(RegistrationPatchError, err) + log.Printf(RegistrationPatchError, r.RemoteAddr, err) + err := fmt.Sprintf("Error patching registration: %v", err) http.Error(w, err, errcode) return } err = _func.ValidateCountryInfo(ci) // Validate the patched country information. if err != nil { - log.Printf(RegistrationPatchError, err) - err := fmt.Sprintf(RegistrationPatchError, err) + log.Printf(RegistrationPatchError, r.RemoteAddr, err) + err := fmt.Sprintf("Error patching registration: %v", err) http.Error(w, err, http.StatusBadRequest) return } err = db.UpdateRegistration(ID, UUID, ci) // Update the registration in the database. if err != nil { - log.Printf("Error saving patched data to database: %v", err) + log.Printf("%s: Error saving patched data to database: %v", r.RemoteAddr, err) err := fmt.Sprintf("Error saving patched data to database: %v", err) http.Error(w, err, http.StatusInternalServerError) return @@ -147,7 +147,7 @@ func handleRegPatchRequest(w http.ResponseWriter, r *http.Request) { reg, err := db.GetSpecificRegistration(ID, UUID) // Retrieve the updated registration. if err != nil { - log.Printf(RegistrationRetrivalError, err) + log.Printf(RegistrationRetrivalError, r.RemoteAddr, err) err := fmt.Sprint("Error retrieving updated document: ", err) http.Error(w, err, http.StatusNotFound) return @@ -181,7 +181,7 @@ func handleRegPatchRequest(w http.ResponseWriter, r *http.Request) { func patchCountryInformation(r *http.Request, ID, UUID string) (*structs.CountryInfoInternal, error, int) { reg, err := db.GetSpecificRegistration(ID, UUID) // Retrieve the specific registration. if err != nil { - log.Printf(RegistrationRetrivalError, err) + log.Printf(RegistrationRetrivalError, r.RemoteAddr, err) return nil, err, http.StatusNotFound } @@ -271,26 +271,26 @@ func handleRegDeleteRequest(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() // Extract the query parameters. token := query.Get("token") // Extract the 'token' parameter from the query. if token == "" { // Validate token presence. - log.Printf(constants.ClientConnectNoToken, r.Method, Endpoints.RegistrationsID) + log.Printf(constants.ClientConnectNoToken, r.RemoteAddr, r.Method, Endpoints.RegistrationsID) http.Error(w, ProvideAPI, http.StatusUnauthorized) return } UUID := db.GetAPIKeyUUID(token) // Retrieve the UUID for the API key. if UUID == "" { // Validate UUID presence. - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.RegistrationsID) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.RegistrationsID) err := fmt.Sprintf(APINotAccepted) http.Error(w, err, http.StatusNotAcceptable) return } if ID == "" || ID == " " { // Validate ID presence. - log.Printf(constants.ClientConnectNoID, r.Method, Endpoints.RegistrationsID) + log.Printf(constants.ClientConnectNoID, r.RemoteAddr, r.Method, Endpoints.RegistrationsID) http.Error(w, ProvideID, http.StatusBadRequest) return } reg, err := db.GetSpecificRegistration(ID, UUID) // Retrieve the specific registration to be deleted. if err != nil { - log.Printf(RegistrationRetrivalError, err) + log.Printf(RegistrationRetrivalError, r.RemoteAddr, err) err := fmt.Sprint("Error getting registration: ", err) http.Error(w, err, http.StatusNotFound) return @@ -298,7 +298,7 @@ func handleRegDeleteRequest(w http.ResponseWriter, r *http.Request) { err = db.DeleteRegistration(ID, UUID) // Delete the registration from the database. if err != nil { - log.Printf("Error deleting registration from database: %v", err) + log.Printf("%s: Error deleting registration from database: %v", r.RemoteAddr, err) err := fmt.Sprintf("Error deleting registration from database: %v", err) http.Error(w, err, http.StatusInternalServerError) return diff --git a/Go/internal/handlers/endpoint/dashboard/status_handler.go b/Go/internal/handlers/endpoint/dashboard/status_handler.go index 11dc1a199b711329f8ebf515fecbd4affe87aaf7..e51190b53a292b028f1c6178c448b80bb2789e01 100644 --- a/Go/internal/handlers/endpoint/dashboard/status_handler.go +++ b/Go/internal/handlers/endpoint/dashboard/status_handler.go @@ -18,7 +18,7 @@ import ( func getEndpointStatus(endpointURL string) string { r, err := http.NewRequest(http.MethodGet, endpointURL, nil) // Create a new GET request for the endpoint URL. if err != nil { - log.Printf("error in creating request: %v", err) + log.Printf("%s: Error in creating request: %v", r.RemoteAddr, err) return "Failed to create request" } @@ -29,7 +29,7 @@ func getEndpointStatus(endpointURL string) string { res, err := client.Do(r) // Execute the request. if err != nil { - log.Printf("error in receiving response: %v", err) + log.Printf("%s: Error in receiving response: %v", r.RemoteAddr, err) return "Failed to connect" } // Ensure the response body is closed after the function exits, checking for errors. @@ -49,7 +49,7 @@ func StatusHandler(w http.ResponseWriter, r *http.Request) { handleStatusGetRequest(w, r) // Handle GET requests with handleStatusGetRequest. default: // Log and return an error for unsupported HTTP methods - log.Printf(constants.ClientConnectUnsupported, Endpoints.Status, r.Method) + log.Printf(constants.ClientConnectUnsupported, r.RemoteAddr, Endpoints.Status, r.Method) http.Error(w, fmt.Sprintf("REST Method: %s not supported. Only GET is supported for this endpoint", r.Method), http.StatusNotImplemented) } } @@ -58,21 +58,21 @@ func StatusHandler(w http.ResponseWriter, r *http.Request) { func handleStatusGetRequest(w http.ResponseWriter, r *http.Request) { token := r.URL.Query().Get("token") // Retrieve the API token from query parameters. if token == "" { // Validate token presence. - log.Printf(constants.ClientConnectNoToken, r.Method, Endpoints.Status) + log.Printf(constants.ClientConnectNoToken, r.RemoteAddr, r.Method, Endpoints.Status) http.Error(w, "Please provide API Token", http.StatusUnauthorized) return } UUID := db.GetAPIKeyUUID(token) // Retrieve the UUID associated with the API token. if UUID == "" { // Validate UUID presence. - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.Status) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.Status) http.Error(w, "API key not accepted", http.StatusNotAcceptable) return } webhooksUser, err := db.GetWebhooksUser(UUID) // Retrieve user data associated with webhooks. if err != nil { - log.Printf("Error retrieving user's webhooks: %v", err) + log.Printf("%s: Error retrieving user's webhooks: %v", r.RemoteAddr, err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } diff --git a/Go/internal/handlers/endpoint/util/apikey_handler.go b/Go/internal/handlers/endpoint/util/apikey_handler.go index 206c1586593d4f36403f2ac1c210d1007fd15bb3..ebafc5bd65a707e635d26101775fe4697431aae5 100644 --- a/Go/internal/handlers/endpoint/util/apikey_handler.go +++ b/Go/internal/handlers/endpoint/util/apikey_handler.go @@ -23,7 +23,7 @@ func APIKeyHandler(w http.ResponseWriter, r *http.Request) { handleApiKeyDeleteRequest(w, r) default: // Log and return an error for unsupported HTTP methods - log.Printf(constants.ClientConnectUnsupported, Endpoints.ApiKey, r.Method) + log.Printf(constants.ClientConnectUnsupported, r.RemoteAddr, Endpoints.ApiKey, r.Method) http.Error(w, "REST Method: "+r.Method+" not supported. Only supported methods for this endpoint are: GET, DELETE", http.StatusNotImplemented) return } @@ -40,14 +40,14 @@ func handleApiKeyDeleteRequest(w http.ResponseWriter, r *http.Request) { _, err := authenticate.Client.GetUser(ctx, UUID) // Verify the UUID with Firebase Authentication. if err != nil { - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.ApiKey) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.ApiKey) log.Printf("Error verifying UUID: %v\n", err) http.Error(w, "Not Authorized", http.StatusUnauthorized) // Respond with unauthorized if UUID is invalid. return } if token == "" || token == " " { // Validate token presence. - log.Printf(constants.ClientConnectNoToken, r.Method, Endpoints.ApiKey) + log.Printf(constants.ClientConnectNoToken, r.RemoteAddr, r.Method, Endpoints.ApiKey) http.Error(w, "Please specify API Key to delete: '?token={API_Key}'", http.StatusBadRequest) return } @@ -75,7 +75,7 @@ func handleApiKeyGetRequest(w http.ResponseWriter, r *http.Request) { _, err := authenticate.Client.GetUser(ctx, UUID) // Verify the UUID with Firebase Authentication. if err != nil { - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.ApiKey) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.ApiKey) log.Printf("Error verifying UUID: %v\n", err) http.Error(w, "Not Authorized", http.StatusUnauthorized) // Respond with unauthorized if UUID is invalid. return diff --git a/Go/internal/handlers/endpoint/util/user_delete_handler.go b/Go/internal/handlers/endpoint/util/user_delete_handler.go index 3f944797ed57ac956bcb9a87298207f1281c059a..799cceb57522279cad72cc856a893ebd083c523b 100644 --- a/Go/internal/handlers/endpoint/util/user_delete_handler.go +++ b/Go/internal/handlers/endpoint/util/user_delete_handler.go @@ -17,7 +17,7 @@ func UserDeletionHandler(w http.ResponseWriter, r *http.Request) { deleteUser(w, r) // Handle DELETE requests default: // Log and return an error for unsupported HTTP methods - log.Printf(constants.ClientConnectUnsupported, Endpoints.UserDeletionID, r.Method) + log.Printf(constants.ClientConnectUnsupported, r.RemoteAddr, Endpoints.UserDeletionID, r.Method) http.Error(w, "REST Method: "+r.Method+" not supported. Only supported method for this endpoint is:\n"+http.MethodDelete, http.StatusNotImplemented) return } @@ -27,7 +27,7 @@ func UserDeletionHandler(w http.ResponseWriter, r *http.Request) { func deleteUser(w http.ResponseWriter, r *http.Request) { ID := r.PathValue("ID") // Extract user ID from the URL path. if ID == "" || ID == " " { // Check if the user ID is provided. - log.Printf(constants.ClientConnectUnauthorized, r.Method, Endpoints.UserDeletionID) + log.Printf(constants.ClientConnectUnauthorized, r.RemoteAddr, r.Method, Endpoints.UserDeletionID) http.Error(w, "Please provide User ID", http.StatusBadRequest) // Return an error if user ID is missing. return } diff --git a/Go/internal/handlers/endpoint/util/user_register_handler.go b/Go/internal/handlers/endpoint/util/user_register_handler.go index f6939c464c1db57587b40379aab6c5f5614cffde..5c6f341911771a8d6f05bd3b94ea6d430bbadc23 100644 --- a/Go/internal/handlers/endpoint/util/user_register_handler.go +++ b/Go/internal/handlers/endpoint/util/user_register_handler.go @@ -26,7 +26,7 @@ func UserRegistrationHandler(w http.ResponseWriter, r *http.Request) { registerUser(w, r) // Handle POST requests default: // Log and return an error for unsupported HTTP methods - log.Printf(constants.ClientConnectUnsupported, Endpoints.UserRegistration, r.Method) + log.Printf(constants.ClientConnectUnsupported, r.RemoteAddr, Endpoints.UserRegistration, r.Method) http.Error(w, "REST Method: "+r.Method+" not supported. Only supported methods for this endpoint is:\n"+http.MethodPost, http.StatusNotImplemented) return } diff --git a/Go/internal/utils/constants/constants.go b/Go/internal/utils/constants/constants.go index ea1de77d4e70da4c506584fe6ab16b4ee2bb0f90..3732e00a804d7357e7e20b0dc56deabb1f143043 100644 --- a/Go/internal/utils/constants/constants.go +++ b/Go/internal/utils/constants/constants.go @@ -8,13 +8,13 @@ const ( IdLength = 20 // IdLength specifies the length of general purpose identifiers. // ClientConnectUnsupported formats an error message for when a client tries to connect using an unsupported method. - ClientConnectUnsupported = "Client attempted to connect to %s with unsupported method: %s\n" + ClientConnectUnsupported = "%s attempted to connect to %s with unsupported method: %s\n" // ClientConnectNoToken formats an error message for connection attempts where no token is provided. - ClientConnectNoToken = "Failed %s attempt to %s: No Token.\n" + ClientConnectNoToken = "%s: Failed %s attempt to %s: No Token.\n" // ClientConnectNoID formats an error message for connection attempts where no ID is provided. - ClientConnectNoID = "Failed %s attempt to %s: No ID.\n" + ClientConnectNoID = "%s: Failed %s attempt to %s: No ID.\n" // ClientConnectUnauthorized formats an error message for unauthorized connection attempts. - ClientConnectUnauthorized = "Unauthorized %s attempted to %s.\n" + ClientConnectUnauthorized = "%s: Unauthorized %s attempted to %s.\n" // ClientConnectEmptyBody formats an error message for connection attempts with no body content. - ClientConnectEmptyBody = "Failed %s attempt to %s: No Body.\n" + ClientConnectEmptyBody = "%s: Failed %s attempt to %s: No Body.\n" ) diff --git a/README.md b/README.md index 579238b57daec4f6dcfb2e3eb5ab7f98116a65c1..a980059714b59d87e20d093bcf82c32fb1b3d597 100644 --- a/README.md +++ b/README.md @@ -732,6 +732,7 @@ cd globeboard/Go/ - Implement more secure registration for users to the application. - Actual authentication of users before registration. - Implement check if user has authenticated before registration. +- Allow user groups to share API token. ## Contributing