aboutsummaryrefslogtreecommitdiff
path: root/pkg/ext/auth.go
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2025-06-04 12:51:47 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2025-06-04 12:51:47 +0200
commit3739c9e14b0c65a59a520dbfefa459e43af3bf20 (patch)
tree3731016eb12180f12817ed1094eee8cb0f67b49b /pkg/ext/auth.go
parent90b2a890096ee9ab3ff84c57542b5220aa9ebe4c (diff)
downloadcerrado-3739c9e14b0c65a59a520dbfefa459e43af3bf20.tar.gz
cerrado-3739c9e14b0c65a59a520dbfefa459e43af3bf20.tar.bz2
cerrado-3739c9e14b0c65a59a520dbfefa459e43af3bf20.zip
feat: Wrap request
Since request is not a interface I need to create a wraper for it so I can extend it later.
Diffstat (limited to 'pkg/ext/auth.go')
-rw-r--r--pkg/ext/auth.go22
1 files changed, 12 insertions, 10 deletions
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)
}
}
}