feat(auth): implement yggdrasil join endpoint
This commit is contained in:
@@ -3,6 +3,8 @@ package core
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
|
||||
"gitea.mrixs.me/minecraft-platform/backend/internal/database"
|
||||
@@ -63,3 +65,41 @@ func (s *UserService) RegisterNewUser(ctx context.Context, req models.RegisterRe
|
||||
// Вызываем метод репозитория для сохранения в БД
|
||||
return s.Repo.CreateUserTx(ctx, user)
|
||||
}
|
||||
|
||||
// ValidateJoinRequest проверяет запрос на присоединение к серверу.
|
||||
func (s *AuthService) ValidateJoinRequest(ctx context.Context, req models.JoinRequest) error {
|
||||
// Преобразуем UUID из строки без дефисов в стандартный формат
|
||||
var uuidStr string
|
||||
if len(req.SelectedProfile) == 32 {
|
||||
uuidStr = fmt.Sprintf("%s-%s-%s-%s-%s",
|
||||
req.SelectedProfile[0:8],
|
||||
req.SelectedProfile[8:12],
|
||||
req.SelectedProfile[12:16],
|
||||
req.SelectedProfile[16:20],
|
||||
req.SelectedProfile[20:32],
|
||||
)
|
||||
} else {
|
||||
return errors.New("invalid profile UUID format")
|
||||
}
|
||||
|
||||
userUUID, err := uuid.Parse(uuidStr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse profile UUID: %w", err)
|
||||
}
|
||||
|
||||
// Проверяем токен в базе данных
|
||||
err = s.UserRepo.ValidateAccessToken(ctx, req.AccessToken, userUUID)
|
||||
if err != nil {
|
||||
if errors.Is(err, database.ErrTokenNotFound) {
|
||||
// Возвращаем ту же ошибку, что и при неверных кредах, чтобы не давать лишней информации
|
||||
return ErrInvalidCredentials
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// В ТЗ указано "привязка serverId". В простой реализации это может быть просто логирование
|
||||
// или запись в кеш (например, Redis). Для начала, просто прохождение валидации достаточно.
|
||||
log.Printf("User %s successfully joined server with serverId %s", userUUID, req.ServerID)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user