From 3739c9e14b0c65a59a520dbfefa459e43af3bf20 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Wed, 4 Jun 2025 12:51:47 +0200 Subject: feat: Wrap request Since request is not a interface I need to create a wraper for it so I can extend it later. --- pkg/ext/auth.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'pkg/ext/auth.go') diff --git a/pkg/ext/auth.go b/pkg/ext/auth.go index 5c3070e..ef126ec 100644 --- a/pkg/ext/auth.go +++ b/pkg/ext/auth.go @@ -14,19 +14,20 @@ type authService interface { ValidateToken(token []byte) (bool, error) } -func DisableAuthentication(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { +func DisableAuthentication(next HandlerFunc) HandlerFunc { + return func(w http.ResponseWriter, r *Request) { ctx := r.Context() ctx = context.WithValue(ctx, "disableAuthentication", true) - next(w, r.WithContext(ctx)) + r.Request = r.WithContext(ctx) + next(w, r) } } func VerifyRespository( config *serverconfig.ConfigurationRepository, -) func(next http.HandlerFunc) http.HandlerFunc { - return func(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { +) func(next HandlerFunc) HandlerFunc { + return func(next HandlerFunc) HandlerFunc { + return func(w http.ResponseWriter, r *Request) { name := r.PathValue("name") if name != "" { repo := config.GetByName(name) @@ -41,9 +42,9 @@ func VerifyRespository( } } -func Authenticate(auth authService) func(next http.HandlerFunc) http.HandlerFunc { - return func(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { +func Authenticate(auth authService) func(next HandlerFunc) HandlerFunc { + return func(next HandlerFunc) HandlerFunc { + return func(w http.ResponseWriter, r *Request) { cookie, err := r.Cookie("auth") if err != nil { if !errors.Is(err, http.ErrNoCookie) { @@ -70,9 +71,10 @@ func Authenticate(auth authService) func(next http.HandlerFunc) http.HandlerFunc ctx := r.Context() ctx = context.WithValue(ctx, "logged", valid) + r.Request = r.WithContext(ctx) slog.Info("Validated token", "valid?", valid) - next(w, r.WithContext(ctx)) + next(w, r) } } } -- cgit v1.2.3