refactor: remove repeating code etc

This commit is contained in:
2025-03-12 13:52:34 +00:00
parent 294047a2b0
commit b9c900578d
20 changed files with 529 additions and 683 deletions
@@ -2,7 +2,7 @@ package middleware
import (
"github.com/gin-gonic/gin"
"github.com/timetracker/backend/internal/api/utils"
"github.com/timetracker/backend/internal/api/responses"
"github.com/timetracker/backend/internal/config"
)
@@ -18,14 +18,14 @@ func APIKeyMiddleware(cfg *config.Config) gin.HandlerFunc {
// Get API key from header
apiKey := c.GetHeader("X-API-Key")
if apiKey == "" {
utils.UnauthorizedResponse(c, "API key is required")
responses.UnauthorizedResponse(c, "API key is required")
c.Abort()
return
}
// Validate API key
if apiKey != cfg.APIKey {
utils.UnauthorizedResponse(c, "Invalid API key")
responses.UnauthorizedResponse(c, "Invalid API key")
c.Abort()
return
}
+16 -11
View File
@@ -14,7 +14,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
"github.com/oklog/ulid/v2"
"github.com/timetracker/backend/internal/api/utils"
"github.com/timetracker/backend/internal/api/responses"
"github.com/timetracker/backend/internal/config"
"github.com/timetracker/backend/internal/models"
"github.com/timetracker/backend/internal/types"
@@ -164,10 +164,10 @@ func loadPublicKey(path string) (*rsa.PublicKey, error) {
// Claims represents the JWT claims
type Claims struct {
UserID string `json:"userId"`
Email string `json:"email"`
Role string `json:"role"`
CompanyID string `json:"companyId"`
UserID string `json:"userId"`
Email string `json:"email"`
Role string `json:"role"`
CompanyID *string `json:"companyId"`
jwt.RegisteredClaims
}
@@ -177,14 +177,14 @@ func AuthMiddleware() gin.HandlerFunc {
// Get the token from cookie
tokenString, err := c.Cookie("jwt")
if err != nil {
utils.UnauthorizedResponse(c, "Authentication cookie is required")
responses.UnauthorizedResponse(c, "Authentication cookie is required")
c.Abort()
return
}
claims, err := validateToken(tokenString)
if err != nil {
utils.UnauthorizedResponse(c, "Invalid or expired token")
responses.UnauthorizedResponse(c, "Invalid or expired token")
c.Abort()
return
}
@@ -204,7 +204,7 @@ func RoleMiddleware(roles ...string) gin.HandlerFunc {
return func(c *gin.Context) {
userRole, exists := c.Get("role")
if !exists {
utils.UnauthorizedResponse(c, "User role not found in context")
responses.UnauthorizedResponse(c, "User role not found in context")
c.Abort()
return
}
@@ -212,7 +212,7 @@ func RoleMiddleware(roles ...string) gin.HandlerFunc {
// Check if the user's role is in the allowed roles
roleStr, ok := userRole.(string)
if !ok {
utils.InternalErrorResponse(c, "Invalid role type in context")
responses.InternalErrorResponse(c, "Invalid role type in context")
c.Abort()
return
}
@@ -226,7 +226,7 @@ func RoleMiddleware(roles ...string) gin.HandlerFunc {
}
if !allowed {
utils.ForbiddenResponse(c, "Insufficient permissions")
responses.ForbiddenResponse(c, "Insufficient permissions")
c.Abort()
return
}
@@ -238,11 +238,16 @@ func RoleMiddleware(roles ...string) gin.HandlerFunc {
// GenerateToken creates a new JWT token for a user
func GenerateToken(user *models.User, c *gin.Context) (string, error) {
// Create the claims
var companyId *string
if user.CompanyID != nil {
wrapper := user.CompanyID.String()
companyId = &wrapper
}
claims := Claims{
UserID: user.ID.String(),
Email: user.Email,
Role: user.Role,
CompanyID: user.CompanyID.String(),
CompanyID: companyId,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(config.MustLoadConfig().JWTConfig.TokenDuration)),
IssuedAt: jwt.NewNumericDate(time.Now()),