Files
egutierrez af2366acb5 feat: rbac_check (pure), jwt_middleware, rbac_middleware
Fase 5 del issue 0010 — RBAC y middlewares de auth.
- rbac_check es pura: solo recorre la matriz roles/permisos
- jwt_middleware extrae token del header Authorization: Bearer, valida e
  inyecta claims en el context con una key privada (jwtCtxKey struct{})
- rbac_middleware requiere jwt_middleware antes; lee role de claims.Custom
- Helper JWTClaimsFromContext para acceder a las claims desde handlers
- 401 claro si RBAC se usa sin JWT antes (code: no_claims)
2026-04-18 17:44:04 +02:00

59 lines
1.9 KiB
Go

package infra
import (
"context"
"net/http"
"strings"
)
// jwtCtxKey es el tipo no exportado usado como key del context para las claims
// inyectadas por JWTMiddleware. Usar un tipo dedicado evita colisiones con
// otros middlewares que guarden valores en el context.
type jwtCtxKey struct{}
// JWTClaimsFromContext extrae las claims inyectadas por JWTMiddleware.
// Retorna (claims, true) si existen en el context, o (zero, false) si no.
func JWTClaimsFromContext(ctx context.Context) (JWTClaims, bool) {
v, ok := ctx.Value(jwtCtxKey{}).(JWTClaims)
return v, ok
}
// JWTMiddleware retorna un Middleware que extrae el JWT del header
// Authorization: Bearer <token>, lo valida con JWTValidate, e inyecta las
// claims en el context para que handlers posteriores las lean con
// JWTClaimsFromContext. Responde 401 si el header falta, tiene formato
// incorrecto o el token es invalido/expirado.
func JWTMiddleware(secret string) Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if auth == "" {
HTTPErrorResponse(w, HTTPError{
Status: http.StatusUnauthorized, Code: "missing_token",
Message: "falta header Authorization",
})
return
}
const prefix = "Bearer "
if !strings.HasPrefix(auth, prefix) {
HTTPErrorResponse(w, HTTPError{
Status: http.StatusUnauthorized, Code: "invalid_token",
Message: "se esperaba Authorization: Bearer <token>",
})
return
}
token := strings.TrimSpace(strings.TrimPrefix(auth, prefix))
claims, err := JWTValidate(token, secret)
if err != nil {
HTTPErrorResponse(w, HTTPError{
Status: http.StatusUnauthorized, Code: "invalid_token",
Message: "token invalido",
})
return
}
ctx := context.WithValue(r.Context(), jwtCtxKey{}, claims)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}