2024-01-14 01:04:01 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-01-15 23:18:34 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-01-14 01:04:01 +01:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
2024-01-31 17:11:15 +01:00
|
|
|
"git.hamburg.ccc.de/ccchh/spaceapid/config"
|
|
|
|
"git.hamburg.ccc.de/ccchh/spaceapid/util"
|
2024-01-14 01:04:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// updateEndpointValidator checks BasicAuth credentials,
|
|
|
|
// checks for correct HTTP method and then returns the request body
|
|
|
|
func updateEndpointValidator(
|
|
|
|
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
|
|
|
|
w http.ResponseWriter, r *http.Request,
|
2024-01-15 23:18:34 +01:00
|
|
|
) ([]byte, error) {
|
2024-01-14 01:04:01 +01:00
|
|
|
// Check BasicAuth credentials
|
|
|
|
username, password, ok := r.BasicAuth()
|
|
|
|
if !ok || !util.CheckCredentials(authDB, validCredentials, username, password) {
|
2024-01-16 01:37:54 +01:00
|
|
|
w.Header()["WWW-Authenticate"] = []string{"Basic realm=\"spaceapid\""}
|
2024-01-15 23:18:34 +01:00
|
|
|
http.Error(w, "", http.StatusUnauthorized)
|
|
|
|
return []byte{}, errors.New(fmt.Sprintf("Unauthorized request from %s Username: %s Password: %s", r.RemoteAddr, username, password))
|
2024-01-14 01:04:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if PUT method
|
|
|
|
if r.Method != http.MethodPut {
|
|
|
|
w.Header().Set("Allow", http.MethodPut)
|
2024-01-15 23:18:34 +01:00
|
|
|
http.Error(w, "", http.StatusMethodNotAllowed)
|
|
|
|
return []byte{}, errors.New(fmt.Sprintf("Wrong Method: %s from %s at %s", r.Method, r.RemoteAddr, r.RequestURI))
|
2024-01-14 01:04:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read request body
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2024-01-15 23:18:34 +01:00
|
|
|
http.Error(w, "", http.StatusInternalServerError)
|
|
|
|
return []byte{}, errors.New(fmt.Sprintf("Failed to read request body from %s with error: %s", r.RemoteAddr, err))
|
2024-01-14 01:04:01 +01:00
|
|
|
}
|
|
|
|
|
2024-01-15 23:18:34 +01:00
|
|
|
return body, nil
|
2024-01-14 01:04:01 +01:00
|
|
|
}
|