Files
honeyDueAPI/internal/middleware/admin_auth.go
Trey t 7690f07a2b Harden API security: input validation, safe auth extraction, new tests, and deploy config
Comprehensive security hardening from audit findings:
- Add validation tags to all DTO request structs (max lengths, ranges, enums)
- Replace unsafe type assertions with MustGetAuthUser helper across all handlers
- Remove query-param token auth from admin middleware (prevents URL token leakage)
- Add request validation calls in handlers that were missing c.Validate()
- Remove goroutines in handlers (timezone update now synchronous)
- Add sanitize middleware and path traversal protection (path_utils)
- Stop resetting admin passwords on migration restart
- Warn on well-known default SECRET_KEY
- Add ~30 new test files covering security regressions, auth safety, repos, and services
- Add deploy/ config, audit digests, and AUDIT_FINDINGS documentation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 09:48:01 -06:00

133 lines
4.0 KiB
Go

package middleware
import (
"errors"
"net/http"
"strings"
"time"
"github.com/labstack/echo/v4"
"github.com/golang-jwt/jwt/v5"
"github.com/treytartt/casera-api/internal/config"
"github.com/treytartt/casera-api/internal/models"
"github.com/treytartt/casera-api/internal/repositories"
)
const (
// AdminUserKey is the context key for the authenticated admin user
AdminUserKey = "admin_user"
// AdminClaimsKey is the context key for JWT claims
AdminClaimsKey = "admin_claims"
)
// AdminClaims represents the JWT claims for admin authentication
type AdminClaims struct {
AdminID uint `json:"admin_id"`
Email string `json:"email"`
Role models.AdminRole `json:"role"`
jwt.RegisteredClaims
}
// AdminAuthMiddleware creates a middleware that validates admin JWT tokens
func AdminAuthMiddleware(cfg *config.Config, adminRepo *repositories.AdminRepository) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
var tokenString string
// Get token from Authorization header only.
// Query parameter authentication is intentionally not supported
// because tokens in URLs leak into server logs and browser history.
authHeader := c.Request().Header.Get("Authorization")
if authHeader != "" {
// Check Bearer prefix
parts := strings.SplitN(authHeader, " ", 2)
if len(parts) == 2 && strings.ToLower(parts[0]) == "bearer" {
tokenString = parts[1]
}
}
if tokenString == "" {
return c.JSON(http.StatusUnauthorized, map[string]interface{}{"error": "Authorization required"})
}
// Parse and validate token
claims := &AdminClaims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
// Validate signing method
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, errors.New("invalid signing method")
}
return []byte(cfg.Security.SecretKey), nil
})
if err != nil {
return c.JSON(http.StatusUnauthorized, map[string]interface{}{"error": "Invalid token"})
}
if !token.Valid {
return c.JSON(http.StatusUnauthorized, map[string]interface{}{"error": "Token is not valid"})
}
// Get admin user from database
admin, err := adminRepo.FindByID(claims.AdminID)
if err != nil {
return c.JSON(http.StatusUnauthorized, map[string]interface{}{"error": "Admin user not found"})
}
// Check if admin is active
if !admin.IsActive {
return c.JSON(http.StatusUnauthorized, map[string]interface{}{"error": "Admin account is disabled"})
}
// Store admin and claims in context
c.Set(AdminUserKey, admin)
c.Set(AdminClaimsKey, claims)
return next(c)
}
}
}
// GenerateAdminToken creates a new JWT token for an admin user
func GenerateAdminToken(admin *models.AdminUser, cfg *config.Config) (string, error) {
// Token expires in 24 hours
expirationTime := time.Now().Add(24 * time.Hour)
claims := &AdminClaims{
AdminID: admin.ID,
Email: admin.Email,
Role: admin.Role,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expirationTime),
IssuedAt: jwt.NewNumericDate(time.Now()),
Subject: admin.Email,
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(cfg.Security.SecretKey))
}
// RequireSuperAdmin middleware requires the admin to have super_admin role
func RequireSuperAdmin() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
admin := c.Get(AdminUserKey)
if admin == nil {
return c.JSON(http.StatusUnauthorized, map[string]interface{}{"error": "Admin authentication required"})
}
adminUser, ok := admin.(*models.AdminUser)
if !ok {
return c.JSON(http.StatusUnauthorized, map[string]interface{}{"error": "Admin authentication required"})
}
if !adminUser.IsSuperAdmin() {
return c.JSON(http.StatusForbidden, map[string]interface{}{"error": "Super admin privileges required"})
}
return next(c)
}
}
}