Add hashing for link password and update link update service method
This commit is contained in:
parent
a01f540a29
commit
f4684be37d
4 changed files with 79 additions and 32 deletions
|
|
@ -3,6 +3,7 @@ package link
|
|||
import (
|
||||
apperrors "cgnolink/errors"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
|
|
@ -13,7 +14,14 @@ type Service interface {
|
|||
GetById(id string) (*Link, error)
|
||||
AccessLink(id string, password string) (*url.URL, error)
|
||||
GetAll(limit int, offset int) (Links, error)
|
||||
Update(data *Link) error
|
||||
UpdateById(
|
||||
id string,
|
||||
data struct {
|
||||
Name string
|
||||
Password string
|
||||
RedirectURL *url.URL
|
||||
},
|
||||
) error
|
||||
DeleteById(id string) error
|
||||
}
|
||||
|
||||
|
|
@ -29,7 +37,7 @@ func (service *PgService) AccessLink(id string, password string) (*url.URL, erro
|
|||
}
|
||||
|
||||
if link.Password != "" {
|
||||
if password == "" || link.Password != password {
|
||||
if password == "" || bcrypt.CompareHashAndPassword([]byte(link.Password), []byte(password)) != nil {
|
||||
return nil, linkNotFoundError
|
||||
}
|
||||
}
|
||||
|
|
@ -54,6 +62,15 @@ func (service *PgService) Create(link *Link) error {
|
|||
return apperrors.AlreadyExistsError{Message: "Link with given ID already exists."}
|
||||
}
|
||||
|
||||
if link.Password != "" {
|
||||
hashedPassword, err := HashPassword(link.Password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
link.Password = hashedPassword
|
||||
}
|
||||
|
||||
if err = service.rep.Save(link); err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
|
@ -91,11 +108,46 @@ func (service *PgService) GetAll(limit int, offset int) (Links, error) {
|
|||
return links, nil
|
||||
}
|
||||
|
||||
func (service *PgService) Update(data *Link) error {
|
||||
if err := service.rep.Update(data); err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
func (service *PgService) UpdateById(
|
||||
id string,
|
||||
data struct {
|
||||
Name string
|
||||
Password string
|
||||
RedirectURL *url.URL
|
||||
},
|
||||
) error {
|
||||
link, err := service.GetById(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hasChanges := false
|
||||
switch {
|
||||
case data.Name != "":
|
||||
link.Name = data.Name
|
||||
|
||||
hasChanges = true
|
||||
case data.RedirectURL != nil:
|
||||
link.RedirectURL = *data.RedirectURL
|
||||
|
||||
hasChanges = true
|
||||
case data.Password != "":
|
||||
hashedPw, err := HashPassword(data.Password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
link.Password = hashedPw
|
||||
|
||||
hasChanges = true
|
||||
}
|
||||
|
||||
if hasChanges {
|
||||
if err := service.rep.Update(link); err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
service.cache.Delete(link.Id)
|
||||
}
|
||||
service.cache.Delete(data.Id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -108,3 +160,12 @@ func (service *PgService) DeleteById(id string) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
hashedPw, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return string(hashedPw), nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue