feat: завершён этап 2 - Аутентификация Core Service

- Реализована JWT аутентификация с organization-scope
- Добавлено хеширование паролей через bcrypt
- Созданы репозитории для организаций и пользователей
- Реализован AuthService с бизнес-логикой
- Добавлен AuthMiddleware для проверки токенов
- Созданы handlers для регистрации и входа
- Обновлён API сервер для использования аутентификации

Готово для этапа 3 - API структура
This commit is contained in:
2025-08-27 14:56:33 +04:00
parent 9777114e16
commit ae84ce74a7
11 changed files with 581 additions and 34 deletions

View File

@@ -0,0 +1,57 @@
package middleware
import (
"net/http"
"strings"
"erp-mvp/core-service/internal/auth"
"github.com/gin-gonic/gin"
)
type AuthMiddleware struct {
jwtService *auth.JWTService
}
func NewAuthMiddleware(jwtService *auth.JWTService) *AuthMiddleware {
return &AuthMiddleware{
jwtService: jwtService,
}
}
func (m *AuthMiddleware) AuthRequired() gin.HandlerFunc {
return func(c *gin.Context) {
// Получаем токен из заголовка Authorization
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header required"})
c.Abort()
return
}
// Проверяем формат "Bearer <token>"
tokenParts := strings.Split(authHeader, " ")
if len(tokenParts) != 2 || tokenParts[0] != "Bearer" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid authorization header format"})
c.Abort()
return
}
tokenString := tokenParts[1]
// Валидируем токен
claims, err := m.jwtService.ValidateToken(tokenString)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
c.Abort()
return
}
// Сохраняем claims в контексте
c.Set("user_id", claims.UserID)
c.Set("organization_id", claims.OrganizationID)
c.Set("email", claims.Email)
c.Set("role", claims.Role)
c.Next()
}
}