пока что простая валидация пароля
This commit is contained in:
parent
fcbf9b62a2
commit
139f19614c
4
main.go
4
main.go
@ -31,11 +31,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
jwtUtil := src.NewJwtUtil(key)
|
jwtUtil := src.NewJwtUtil(key)
|
||||||
bcryptUtil := src.NewBcrypt()
|
passwordUtil := src.NewPasswordUtil()
|
||||||
db := src.NewDB(sqlDb)
|
db := src.NewDB(sqlDb)
|
||||||
userService := src.NewUserService(src.UserServiceDeps{
|
userService := src.NewUserService(src.UserServiceDeps{
|
||||||
Jwt: jwtUtil,
|
Jwt: jwtUtil,
|
||||||
Bcrypt: bcryptUtil,
|
Password: passwordUtil,
|
||||||
Db: db,
|
Db: db,
|
||||||
Cache: src.NewCacheInmem[string, src.UserDTO](60 * 60),
|
Cache: src.NewCacheInmem[string, src.UserDTO](60 * 60),
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,23 +0,0 @@
|
|||||||
package src
|
|
||||||
|
|
||||||
import "golang.org/x/crypto/bcrypt"
|
|
||||||
|
|
||||||
type BCryptUtil interface {
|
|
||||||
HashPassword(password string) (string, error)
|
|
||||||
IsPasswordsEqual(password, hash string) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBcrypt() BCryptUtil {
|
|
||||||
return &bcryptImpl{}
|
|
||||||
}
|
|
||||||
|
|
||||||
type bcryptImpl struct{}
|
|
||||||
|
|
||||||
func (b *bcryptImpl) HashPassword(password string) (string, error) {
|
|
||||||
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
||||||
return string(bytes), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *bcryptImpl) IsPasswordsEqual(password, hash string) bool {
|
|
||||||
return nil == bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
||||||
}
|
|
||||||
35
src/password_util.go
Normal file
35
src/password_util.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package src
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PasswordUtil interface {
|
||||||
|
Hash(password string) (string, error)
|
||||||
|
Compare(password, hash string) bool
|
||||||
|
Validate(password string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPasswordUtil() PasswordUtil {
|
||||||
|
return &passwordUtil{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type passwordUtil struct{}
|
||||||
|
|
||||||
|
func (b *passwordUtil) Hash(password string) (string, error) {
|
||||||
|
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
|
return string(bytes), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *passwordUtil) Compare(password, hash string) bool {
|
||||||
|
return nil == bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *passwordUtil) Validate(password string) error {
|
||||||
|
if len(password) < 8 {
|
||||||
|
return fmt.Errorf("password must contain 8 or more characters")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@ -10,6 +10,7 @@ var (
|
|||||||
ErrUserExists = fmt.Errorf("user with this login already exists")
|
ErrUserExists = fmt.Errorf("user with this login already exists")
|
||||||
ErrUserWrongPassword = fmt.Errorf("wrong password")
|
ErrUserWrongPassword = fmt.Errorf("wrong password")
|
||||||
ErrUserWrongToken = fmt.Errorf("bad user token")
|
ErrUserWrongToken = fmt.Errorf("bad user token")
|
||||||
|
ErrUserBadPassword = fmt.Errorf("password must contain at least 8 characters")
|
||||||
// ErrUserInternal = fmt.Errorf("unexpected error. contact tech support")
|
// ErrUserInternal = fmt.Errorf("unexpected error. contact tech support")
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,7 +27,7 @@ func NewUserService(deps UserServiceDeps) UserService {
|
|||||||
type UserServiceDeps struct {
|
type UserServiceDeps struct {
|
||||||
Db DB
|
Db DB
|
||||||
Jwt JwtUtil
|
Jwt JwtUtil
|
||||||
Bcrypt BCryptUtil
|
Password PasswordUtil
|
||||||
Cache Cache[string, UserDTO]
|
Cache Cache[string, UserDTO]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,7 +50,11 @@ func (u *userService) CreateUser(ctx context.Context, params UserCreateParams) (
|
|||||||
return nil, ErrUserExists
|
return nil, ErrUserExists
|
||||||
}
|
}
|
||||||
|
|
||||||
secret, err := u.deps.Bcrypt.HashPassword(params.Password)
|
if err := u.deps.Password.Validate(params.Password); err != nil {
|
||||||
|
return nil, ErrUserBadPassword
|
||||||
|
}
|
||||||
|
|
||||||
|
secret, err := u.deps.Password.Hash(params.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -79,7 +84,7 @@ func (u *userService) AuthenticateUser(ctx context.Context, login, password stri
|
|||||||
return "", ErrUserNotExists
|
return "", ErrUserNotExists
|
||||||
}
|
}
|
||||||
|
|
||||||
if !u.deps.Bcrypt.IsPasswordsEqual(password, user.Secret) {
|
if !u.deps.Password.Compare(password, user.Secret) {
|
||||||
return "", ErrUserWrongPassword
|
return "", ErrUserWrongPassword
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -31,7 +31,7 @@ func NewUserCreateHandler(userService UserService) gin.HandlerFunc {
|
|||||||
Password: params.Password,
|
Password: params.Password,
|
||||||
Name: params.Name,
|
Name: params.Name,
|
||||||
})
|
})
|
||||||
if err == ErrUserExists {
|
if err == ErrUserExists || err == ErrUserBadPassword {
|
||||||
ctx.Data(400, "plain/text", []byte(err.Error()))
|
ctx.Data(400, "plain/text", []byte(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user