2024-03-26 17:52:15 -07:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2024-03-31 17:47:39 -07:00
|
|
|
"go-cook/api/domain"
|
2024-03-26 17:52:15 -07:00
|
|
|
"go-cook/api/repositories"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ErrPasswordNotLongEnough = "password needs to be 8 character or longer"
|
|
|
|
ErrPasswordMissingSpecialCharacter = "password needs to contain one of the following: !, @, #"
|
|
|
|
ErrInvalidPassword = "invalid password"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This will handle operations that are user related, but one layer higher then the repository
|
|
|
|
type UserService struct {
|
|
|
|
repo repositories.IUserTable
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserService(conn *sql.DB) UserService {
|
|
|
|
return UserService{
|
|
|
|
repo: repositories.NewUserRepository(conn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (us UserService) DoesUserExist(username string) error {
|
|
|
|
_, err := us.repo.GetByName(username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (us UserService) DoesPasswordMatchHash(username, password string) error {
|
|
|
|
model, err := us.GetUser(username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-27 17:24:23 -07:00
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(model.Hash), []byte(password))
|
|
|
|
if err != nil {
|
2024-03-26 17:52:15 -07:00
|
|
|
return errors.New(ErrInvalidPassword)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-31 17:47:39 -07:00
|
|
|
func (us UserService) GetUser(username string) (domain.UserEntity, error) {
|
2024-03-26 17:52:15 -07:00
|
|
|
return us.repo.GetByName(username)
|
|
|
|
}
|
|
|
|
|
2024-03-31 17:47:39 -07:00
|
|
|
func (us UserService) CreateNewUser(name, password string) (domain.UserEntity, error) {
|
2024-03-26 17:52:15 -07:00
|
|
|
err := us.CheckPasswordForRequirements(password)
|
|
|
|
if err != nil {
|
2024-03-31 17:47:39 -07:00
|
|
|
return domain.UserEntity{}, err
|
2024-03-26 17:52:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
us.repo.Create(name, password)
|
2024-03-31 17:47:39 -07:00
|
|
|
return domain.UserEntity{}, nil
|
2024-03-26 17:52:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (us UserService) CheckPasswordForRequirements(password string) error {
|
|
|
|
err := us.checkPasswordLength(password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = us.checkPasswordForSpecialCharacters(password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (us UserService) checkPasswordLength(password string) error {
|
2024-03-27 17:24:23 -07:00
|
|
|
if len(password) < 8 {
|
2024-03-26 17:52:15 -07:00
|
|
|
return errors.New(ErrPasswordNotLongEnough)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (us UserService) checkPasswordForSpecialCharacters(password string) error {
|
|
|
|
var chars []string
|
|
|
|
chars = append(chars, "!")
|
|
|
|
chars = append(chars, "@")
|
|
|
|
chars = append(chars, "#")
|
|
|
|
|
|
|
|
for _, char := range chars {
|
|
|
|
if strings.Contains(password, char) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New(ErrPasswordMissingSpecialCharacter)
|
|
|
|
}
|