From df6dcd1d840c5d20c5ba474ada00ea07f251a2c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Thu, 1 Oct 2026 15:57:55 +0300 Subject: [PATCH] feat(service): lab2 UserService.Create hash password --- backend/internal/httpapi/dto/user.go | 2 +- backend/internal/httpapi/handler/user.go | 2 +- backend/internal/service/user.go | 24 +++++++++++++++++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/backend/internal/httpapi/dto/user.go b/backend/internal/httpapi/dto/user.go index 0a027c1..0851941 100644 --- a/backend/internal/httpapi/dto/user.go +++ b/backend/internal/httpapi/dto/user.go @@ -9,7 +9,7 @@ const timeFormat = "2006-01-02T15:04:05.000000Z07:00" type CreateUserRequest struct { Username string `json:"username" binding:"required,min=3,max=32"` DisplayName string `json:"display_name" binding:"required,min=1,max=100"` - Password string `json:"password" binding:"required,min=12,max=128"` + Password string `json:"password" binding:"required,min=12,max=512"` } type UserResponse struct { diff --git a/backend/internal/httpapi/handler/user.go b/backend/internal/httpapi/handler/user.go index 35ad917..7697b50 100644 --- a/backend/internal/httpapi/handler/user.go +++ b/backend/internal/httpapi/handler/user.go @@ -25,7 +25,7 @@ func (h *UserHandler) Create(c *gin.Context) { return } - u, err := h.users.Create(c.Request.Context(), req.Username, req.DisplayName) + u, err := h.users.Create(c.Request.Context(), req.Username, req.DisplayName, req.Password) if err != nil { respondError(c, err) return diff --git a/backend/internal/service/user.go b/backend/internal/service/user.go index ef5e9ba..e7b7547 100644 --- a/backend/internal/service/user.go +++ b/backend/internal/service/user.go @@ -23,7 +23,7 @@ func NewUserService(users domain.UserRepository, hasher PasswordHasher) *UserSer return &UserService{users: users, hasher: hasher} } -func (s *UserService) Create(ctx context.Context, username, displayName string) (*domain.User, error) { +func (s *UserService) Create(ctx context.Context, username, displayName, password string) (*domain.User, error) { if !usernamePattern.MatchString(username) { return nil, &domain.ValidationError{ Field: "username", @@ -43,11 +43,25 @@ func (s *UserService) Create(ctx context.Context, username, displayName string) } } + passwordRunes := utf8.RuneCountInString(password) + if passwordRunes < 12 || passwordRunes > 128 { + return nil, &domain.ValidationError{ + Field: "password", + Message: "длина 12–128 символов", + } + } + + hash, err := s.hasher.Hash(password) + if err != nil { + return nil, err + } + u := &domain.User{ - ID: uuid.NewString(), - Username: username, - DisplayName: displayName, - CreatedAt: time.Now().UTC(), + ID: uuid.NewString(), + Username: username, + DisplayName: displayName, + PasswordHash: hash, + CreatedAt: time.Now().UTC(), } if err := s.users.Create(ctx, u); err != nil { return nil, err