From 871c54f35f8651632714ca7d3a3ab0e809549b51 Mon Sep 17 00:00:00 2001 From: Janis Jansons Date: Tue, 27 Feb 2024 19:16:08 +0200 Subject: [PATCH] feat: check for basic auth (#110) (#129) --- internal/auth/middleware.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/auth/middleware.go b/internal/auth/middleware.go index 84435de..22ce652 100644 --- a/internal/auth/middleware.go +++ b/internal/auth/middleware.go @@ -17,6 +17,17 @@ const UserContextKey contextKey = "user" func RequireAuthentication(h http.Handler, auth *Authenticator) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + + username, password, usesBasicAuth := r.BasicAuth() + if usesBasicAuth { + user, err := auth.Login(username, password) + if err == nil { + ctx := context.WithValue(r.Context(), UserContextKey, user) + h.ServeHTTP(w, r.WithContext(ctx)) + return + } + } + token, err := ParseBearerToken(r.Header.Get("Authorization")) if err != nil { http.Error(w, "Unauthorized (No Authorization Header)", http.StatusUnauthorized)