- POST /api/v1/auth/register with mandatory consents
- POST /api/v1/auth/login
- GET /api/v1/me (protected)
- GET /api/v1/legal/{terms,privacy,disclaimer}
- Migrations for users, consent_log, audit_log
- bcrypt + JWT (access + refresh)
- Docker Compose stack
41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package auth
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
const UserIDKey = "user_id"
|
|
|
|
func Middleware(svc *Service) fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
h := c.Get("Authorization")
|
|
if h == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "missing Authorization header"})
|
|
}
|
|
parts := strings.SplitN(h, " ", 2)
|
|
if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "invalid Authorization header"})
|
|
}
|
|
claims, err := svc.Parse(c.UserContext(), parts[1])
|
|
if err != nil {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "invalid token"})
|
|
}
|
|
c.Locals(UserIDKey, claims.UserID)
|
|
return c.Next()
|
|
}
|
|
}
|
|
|
|
func UserID(c *fiber.Ctx) (string, error) {
|
|
v := c.Locals(UserIDKey)
|
|
if v == nil {
|
|
return "", errors.New("no user in context")
|
|
}
|
|
if id, ok := v.(interface{ String() string }); ok {
|
|
return id.String(), nil
|
|
}
|
|
return "", errors.New("invalid user id type")
|
|
}
|