Files
CaddyProxyManager/backend/internal/api/middleware/auth.go
Pacerino 401e2520c1 [Backend] Modernize toolchain to Go 1.24 and update dependencies
Bump Go version, update all dependencies and migrate jwx v1 (deprecated)
to jwx v3.
2026-06-14 02:34:40 +02:00

59 lines
1.8 KiB
Go

package middleware
import (
"context"
"net/http"
c "github.com/Pacerino/CaddyProxyManager/internal/api/context"
h "github.com/Pacerino/CaddyProxyManager/internal/api/http"
"github.com/Pacerino/CaddyProxyManager/internal/auth"
"github.com/Pacerino/CaddyProxyManager/internal/logger"
"github.com/go-chi/jwtauth/v5"
"github.com/lestrrat-go/jwx/v3/jwt"
)
// DecodeAuth decodes an auth header
func DecodeAuth() func(http.Handler) http.Handler {
privateKey, privateKeyParseErr := auth.GetPrivateKey()
if privateKeyParseErr != nil && privateKey == nil {
logger.Error("PrivateKeyParseError", privateKeyParseErr)
}
/* publicKey, publicKeyParseErr := auth.GetPublicKey()
if publicKeyParseErr != nil && publicKey == nil {
logger.Error("PublicKeyParseError", publicKeyParseErr)
} */
tokenAuth := jwtauth.New("RS256", privateKey, privateKey.PublicKey)
return jwtauth.Verifier(tokenAuth)
}
// Enforce is a authentication middleware to enforce access from the
// jwtauth.Verifier middleware request context values. The Authenticator sends a 401 Unauthorised
// response for any unverified tokens and passes the good ones through.
func Enforce() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
token, claims, err := jwtauth.FromContext(ctx)
if err != nil {
h.ResultErrorJSON(w, r, http.StatusUnauthorized, err.Error(), nil)
return
}
userID := int(claims["uid"].(float64))
if token == nil || jwt.Validate(token) != nil {
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
return
}
// Add claims to context
ctx = context.WithValue(ctx, c.UserIDCtxKey, userID)
// Token is authenticated, continue as normal
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}