This commit is contained in:
@@ -3,41 +3,37 @@ package chat
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/password"
|
||||
)
|
||||
|
||||
type UseCase struct {
|
||||
repository Repository
|
||||
sessions Sessions
|
||||
sessionTTL time.Duration
|
||||
}
|
||||
|
||||
func New(repository Repository) *UseCase {
|
||||
return &UseCase{repository: repository}
|
||||
func New(repository Repository, sessions Sessions, ttl time.Duration) *UseCase {
|
||||
return &UseCase{repository: repository, sessions: sessions, sessionTTL: ttl}
|
||||
}
|
||||
|
||||
func (u *UseCase) Authenticate(ctx context.Context, rawUserID string) (domain.User, error) {
|
||||
userID, valid := normalizeUUID(rawUserID)
|
||||
if !valid {
|
||||
return domain.User{}, unauthorized()
|
||||
}
|
||||
user, exists, err := u.repository.User(ctx, userID)
|
||||
if err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
if !exists {
|
||||
return domain.User{}, unauthorized()
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (u *UseCase) CreateUser(ctx context.Context, user domain.User) (domain.User, error) {
|
||||
func (u *UseCase) CreateUser(ctx context.Context, user domain.User, secret string) (domain.User, error) {
|
||||
if !validUsername(user.Username) {
|
||||
return domain.User{}, invalid("username должен соответствовать [a-z0-9_]{3,32}")
|
||||
}
|
||||
if !validRequiredText(user.DisplayName, 100) {
|
||||
return domain.User{}, invalid("display_name должен содержать от 1 до 100 символов")
|
||||
}
|
||||
created, err := u.repository.CreateUser(ctx, user.Username, user.DisplayName)
|
||||
if !validPassword(secret) {
|
||||
return domain.User{}, invalid("password должен содержать от 12 до 128 символов")
|
||||
}
|
||||
hash, err := password.Hash(ctx, secret)
|
||||
if err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
created, err := u.repository.CreateUser(ctx, user.Username, user.DisplayName, hash)
|
||||
if errors.Is(err, ErrUsernameTaken) {
|
||||
return domain.User{}, domain.NewApplicationError(domain.ErrorConflict, "Пользователь с таким username уже существует")
|
||||
}
|
||||
@@ -154,7 +150,7 @@ func invalid(message string) error {
|
||||
}
|
||||
|
||||
func unauthorized() error {
|
||||
return domain.NewApplicationError(domain.ErrorUnauthorized, "Требуется действительный X-User-Id")
|
||||
return domain.NewApplicationError(domain.ErrorUnauthorized, "Требуется действительная сессия")
|
||||
}
|
||||
|
||||
func notFound() error {
|
||||
|
||||
Reference in New Issue
Block a user