auth.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package middleware
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "git.linuxforward.com/byop/byop-engine/auth"
  7. "github.com/gin-gonic/gin"
  8. "github.com/golang-jwt/jwt/v5"
  9. )
  10. // debugMode is a flag to enable or disable debug logging
  11. var debug = true
  12. // JWT secret key - in production, this should be loaded from environment variables
  13. var jwtSecret = []byte("your-secret-key-here")
  14. // Claims represents the JWT claims
  15. type Claims struct {
  16. UserID string `json:"user_id"`
  17. Role string `json:"role"`
  18. jwt.RegisteredClaims
  19. }
  20. // Auth middleware that accepts the auth service as dependency
  21. func Auth(authService auth.Service) gin.HandlerFunc {
  22. if debug {
  23. return func(c *gin.Context) {
  24. c.Set("clientID", "debug_user")
  25. c.Set("user_id", "debug_user")
  26. c.Set("role", "admin")
  27. c.Next()
  28. }
  29. }
  30. return func(c *gin.Context) {
  31. // Get token from request
  32. token := extractTokenFromHeader(c)
  33. if token == "" {
  34. c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header required"})
  35. c.Abort()
  36. return
  37. }
  38. // Validate token using the auth service
  39. clientID, role, err := authService.ValidateToken(c.Request.Context(), token)
  40. if err != nil {
  41. c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
  42. c.Abort()
  43. return
  44. }
  45. fmt.Println("Client ID from token:", clientID)
  46. fmt.Println("Role from token:", role)
  47. // Set client ID and role in context for later use
  48. c.Set("clientID", clientID)
  49. c.Set("user_id", clientID) // Set user_id for backward compatibility
  50. c.Set("role", role)
  51. c.Next()
  52. }
  53. }
  54. // GetUserIDFromContext retrieves the user ID from the request context
  55. func GetUserIDFromContext(ctx context.Context) string {
  56. userID, ok := ctx.Value("user_id").(string)
  57. if !ok {
  58. return ""
  59. }
  60. return userID
  61. }
  62. // GetRoleFromContext retrieves the user role from the request context
  63. func GetRoleFromContext(ctx context.Context) string {
  64. role, ok := ctx.Value("role").(string)
  65. if !ok {
  66. return ""
  67. }
  68. return role
  69. }
  70. // IsAdmin checks if the user in the context has admin role
  71. func IsAdmin(ctx context.Context) bool {
  72. role := GetRoleFromContext(ctx)
  73. fmt.Println("Role from context:", role)
  74. return role == "admin"
  75. }
  76. // extractTokenFromHeader gets the JWT token from the Authorization header
  77. func extractTokenFromHeader(c *gin.Context) string {
  78. authHeader := c.GetHeader("Authorization")
  79. if authHeader == "" {
  80. return ""
  81. }
  82. // Check if the header has the format "Bearer <token>"
  83. if len(authHeader) > 7 && authHeader[:7] == "Bearer " {
  84. return authHeader[7:]
  85. }
  86. return ""
  87. }
  88. // AdminAuth middleware checks if the user has admin role
  89. func AdminAuth(authService auth.Service) gin.HandlerFunc {
  90. return func(c *gin.Context) {
  91. // Get token from request
  92. token := extractTokenFromHeader(c)
  93. if token == "" {
  94. c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header required"})
  95. c.Abort()
  96. return
  97. }
  98. // Validate token using the auth service
  99. clientID, role, err := authService.ValidateToken(c.Request.Context(), token)
  100. if err != nil {
  101. c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
  102. c.Abort()
  103. return
  104. }
  105. // Check if the user has admin role
  106. if role != "admin" {
  107. c.JSON(http.StatusForbidden, gin.H{"error": "Admin access required"})
  108. c.Abort()
  109. return
  110. }
  111. // Set client ID and role in context for later use
  112. c.Set("clientID", clientID)
  113. c.Set("user_id", clientID) // Set user_id for backward compatibility
  114. c.Set("role", role)
  115. c.Next()
  116. }
  117. }