cleanup/small refactor + first tests

This commit is contained in:
2025-07-17 19:28:21 +02:00
parent 6688128d30
commit 68000a0f0a
6 changed files with 99 additions and 61 deletions

View File

@@ -33,7 +33,7 @@ func TimeCreateHandler(w http.ResponseWriter, r *http.Request) {
// Creates a booking from the http query params -> no body needed
// after that entry wi'll be written to database and the booking is returned as json
func createBooking(w http.ResponseWriter, r *http.Request) {
if !checkPassword(r) {
if !verifyToken(r) {
log.Println("Wrong or no API key provided!")
http.Error(w, "Wrong or no API key provided", http.StatusUnauthorized)
return
@@ -58,16 +58,15 @@ func createBooking(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
}
func checkPassword(r *http.Request) bool {
func verifyToken(r *http.Request) bool {
authToken := helper.GetEnv("API_TOKEN", "dont_access")
authHeaders := r.Header.Get("Authorization")
_authStart := len("Bearer ")
if len(authHeaders) <= _authStart {
if len(authHeaders) <= 7 { //len "Bearer "
authHeaders = r.URL.Query().Get("api_key")
_authStart = 0
if len(authHeaders) <= _authStart {
if len(authHeaders) <= 0 {
return false
}
return authToken == authHeaders
}
return authToken == authHeaders[_authStart:]
return authToken == authHeaders[7:]
}