refactor to use net/http

This commit is contained in:
2025-05-30 14:43:47 +05:30
parent df9478a920
commit e5d07f0ce6
5 changed files with 121 additions and 129 deletions

View File

@@ -3,10 +3,6 @@ package main
import (
"context"
"net/http"
"slices"
"strings"
"src.opensuse.org/autogits/common"
)
func ConfigMiddleWare(cfg *Config) func(http.Handler) http.Handler {
@@ -17,38 +13,3 @@ func ConfigMiddleWare(cfg *Config) func(http.Handler) http.Handler {
})
}
}
func ProxyAuthMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
header := r.Header.Get("Authorization")
if header == "" {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
token_arr := strings.Split(header, " ")
if len(token_arr) != 2 {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
if !strings.EqualFold(token_arr[0], "Bearer") {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
token := token_arr[1]
config, ok := r.Context().Value(configKey).(*Config)
if !ok {
common.LogError("Config missing from context")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if !slices.Contains(config.Keys, token) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}