sonarqube-badges/router/middlewares/checkAuth.go
2025-03-13 08:25:39 +01:00

43 lines
874 B
Go

package middlewares
import (
"context"
"net/http"
"sonarqube-badge/config"
"sonarqube-badge/security"
"strings"
)
func CheckAuth(ctx context.Context, r *http.Request) bool {
config := ctx.Value("config").(config.Config)
if r.Header.Get("Authorization") == "" {
return false
}
token := r.Header.Get("Authorization")
if strings.HasPrefix(token, "Bearer ") {
token = token[7:]
} else {
return false
}
return token == config.AppPassword
}
func CheckJwtToken(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tokenCookie, err := r.Cookie("jwt-token")
if err != nil || tokenCookie == nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
_, err = security.VerifyToken(tokenCookie.Value)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
h.ServeHTTP(w, r)
})
}