feat(service): lab2 UserService.Create hash password

This commit is contained in:
Голиков Maximillian Антон
2026-10-01 15:57:55 +03:00
parent cfabbfb5a0
commit df6dcd1d84
3 changed files with 21 additions and 7 deletions
+1 -1
View File
@@ -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 {
+1 -1
View File
@@ -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
+19 -5
View File
@@ -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