Reintroduced checking of bearer token to keep api intact

pull/2864/head
Eicke Hauck 2023-05-16 22:09:25 +02:00
parent 508ca998a6
commit 38a0a5e92a
2 changed files with 13 additions and 3 deletions

View File

@ -71,7 +71,8 @@ func GetContext(handler http.Handler) http.HandlerFunc {
} }
} }
// RequireAPIKey ensures that a valid API key or login cookie is set // RequireAPIKey ensures that a valid login cookie or API key is set (either
// the api_key GET parameter, or a Bearer token)
func RequireAPIKey(handler http.Handler) http.Handler { func RequireAPIKey(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Origin", "*")
@ -83,6 +84,15 @@ func RequireAPIKey(handler http.Handler) http.Handler {
} }
r.ParseForm() r.ParseForm()
ak := r.Form.Get("api_key") ak := r.Form.Get("api_key")
// If we can't get the API key, we'll also check for the
// Authorization Bearer token
if ak == "" {
tokens, ok := r.Header["Authorization"]
if ok && len(tokens) >= 1 {
ak = tokens[0]
ak = strings.TrimPrefix(ak, "Bearer ")
}
}
// If we can't get the API key, we'll also check if user is logged in // If we can't get the API key, we'll also check if user is logged in
// via the web interface // via the web interface
if ak == "" { if ak == "" {
@ -91,7 +101,7 @@ func RequireAPIKey(handler http.Handler) http.Handler {
} }
} }
if ak == "" { if ak == "" {
JSONError(w, http.StatusUnauthorized, "Logged out") //API Key not set JSONError(w, http.StatusUnauthorized, "Not logged in") //API Key not set
return return
} }
u, err := models.GetUserByAPIKey(ak) u, err := models.GetUserByAPIKey(ak)

View File

@ -16,7 +16,7 @@
"@babel/preset-env": "^7.4.5", "@babel/preset-env": "^7.4.5",
"babel-loader": "^8.0.6", "babel-loader": "^8.0.6",
"clean-css": "^4.2.1", "clean-css": "^4.2.1",
"gulp": "^4.0.2", "gulp": "^4.0.0",
"gulp-babel": "^8.0.0", "gulp-babel": "^8.0.0",
"gulp-clean-css": "^4.0.0", "gulp-clean-css": "^4.0.0",
"gulp-cli": "^2.2.0", "gulp-cli": "^2.2.0",