diff options
author | Julien Dessaux | 2025-01-03 00:54:15 +0100 |
---|---|---|
committer | Julien Dessaux | 2025-01-03 00:54:15 +0100 |
commit | c18d03d4049e7fe2e032ba448e88a44671dfdbeb (patch) | |
tree | 09cfe305f3bb468b9a3a750c92c9b54e574a21ab /pkg/basic_auth | |
parent | feat(tfstated): bootstrap webui listening on a second port (diff) | |
download | tfstated-c18d03d4049e7fe2e032ba448e88a44671dfdbeb.tar.gz tfstated-c18d03d4049e7fe2e032ba448e88a44671dfdbeb.tar.bz2 tfstated-c18d03d4049e7fe2e032ba448e88a44671dfdbeb.zip |
chore(tfstated): refactor middlewares
Diffstat (limited to 'pkg/basic_auth')
-rw-r--r-- | pkg/basic_auth/middleware.go | 39 |
1 files changed, 0 insertions, 39 deletions
diff --git a/pkg/basic_auth/middleware.go b/pkg/basic_auth/middleware.go deleted file mode 100644 index cb2dcf0..0000000 --- a/pkg/basic_auth/middleware.go +++ /dev/null @@ -1,39 +0,0 @@ -package basic_auth - -import ( - "context" - "fmt" - "net/http" - - "git.adyxax.org/adyxax/tfstated/pkg/database" - "git.adyxax.org/adyxax/tfstated/pkg/helpers" - "git.adyxax.org/adyxax/tfstated/pkg/model" -) - -func Middleware(db *database.DB) func(http.Handler) http.Handler { - return func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - username, password, ok := r.BasicAuth() - if !ok { - w.Header().Set("WWW-Authenticate", `Basic realm="tfstated", charset="UTF-8"`) - helpers.ErrorResponse(w, http.StatusUnauthorized, fmt.Errorf("Unauthorized")) - return - } - account, err := db.LoadAccountByUsername(username) - if err != nil { - helpers.ErrorResponse(w, http.StatusInternalServerError, err) - return - } - if account == nil || !account.CheckPassword(password) { - helpers.ErrorResponse(w, http.StatusForbidden, fmt.Errorf("Forbidden")) - return - } - if err := db.TouchAccount(account); err != nil { - helpers.ErrorResponse(w, http.StatusInternalServerError, err) - return - } - ctx := context.WithValue(r.Context(), model.AccountContextKey{}, account) - next.ServeHTTP(w, r.WithContext(ctx)) - }) - } -} |