mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
feat(auth): change password and username (#1295)
* feat(backend): added change password api endpoint. * feat(web): added profile UI to change password. I think we can change the username too, but I don't know if we should for now disabled the username field. * refactor: don't leak username or password. * refactor: protect the route. * generic * feat: add ChangeUsername * fix(tests): speculative fix for TestUserRepo_Update * Revert "feat: add ChangeUsername" This reverts commit d4c1645002883a278aa45dec3c8c19fa1cc75d9b. * refactor into 1 endpoint that handles both * feat: added option to change username as well. :pain: * refactor: frontend * refactor: function names in backend I think this makes it more clear what their function is * fix: change to 2 cols with separator * refactor: update user * fix: test db create user --------- Co-authored-by: Kyle Sanderson <kyle.leet@gmail.com> Co-authored-by: soup <soup@r4tio.dev> Co-authored-by: martylukyy <35452459+martylukyy@users.noreply.github.com> Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
parent
d898b3cd8d
commit
df2612602b
17 changed files with 390 additions and 57 deletions
|
@ -19,6 +19,9 @@ type Service interface {
|
|||
GetUserCount(ctx context.Context) (int, error)
|
||||
Login(ctx context.Context, username, password string) (*domain.User, error)
|
||||
CreateUser(ctx context.Context, req domain.CreateUserRequest) error
|
||||
UpdateUser(ctx context.Context, req domain.UpdateUserRequest) error
|
||||
CreateHash(password string) (hash string, err error)
|
||||
ComparePasswordAndHash(password string, hash string) (match bool, err error)
|
||||
}
|
||||
|
||||
type service struct {
|
||||
|
@ -54,7 +57,7 @@ func (s *service) Login(ctx context.Context, username, password string) (*domain
|
|||
}
|
||||
|
||||
// compare password from request and the saved password
|
||||
match, err := argon2id.ComparePasswordAndHash(password, u.Password)
|
||||
match, err := s.ComparePasswordAndHash(password, u.Password)
|
||||
if err != nil {
|
||||
return nil, errors.New("error checking credentials")
|
||||
}
|
||||
|
@ -83,7 +86,7 @@ func (s *service) CreateUser(ctx context.Context, req domain.CreateUserRequest)
|
|||
return errors.New("only 1 user account is supported at the moment")
|
||||
}
|
||||
|
||||
hashed, err := argon2id.CreateHash(req.Password, argon2id.DefaultParams)
|
||||
hashed, err := s.CreateHash(req.Password)
|
||||
if err != nil {
|
||||
return errors.New("failed to hash password")
|
||||
}
|
||||
|
@ -97,3 +100,59 @@ func (s *service) CreateUser(ctx context.Context, req domain.CreateUserRequest)
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) UpdateUser(ctx context.Context, req domain.UpdateUserRequest) error {
|
||||
if req.PasswordCurrent == "" {
|
||||
return errors.New("validation error: empty current password supplied")
|
||||
}
|
||||
|
||||
if req.PasswordNew != "" && req.PasswordCurrent != "" {
|
||||
if req.PasswordNew == req.PasswordCurrent {
|
||||
return errors.New("validation error: new password must be different")
|
||||
}
|
||||
}
|
||||
|
||||
// find user
|
||||
u, err := s.userSvc.FindByUsername(ctx, req.UsernameCurrent)
|
||||
if err != nil {
|
||||
s.log.Trace().Err(err).Msgf("invalid login %v", req.UsernameCurrent)
|
||||
return errors.Wrapf(err, "invalid login: %s", req.UsernameCurrent)
|
||||
}
|
||||
|
||||
if u == nil {
|
||||
return errors.Errorf("invalid login: %s", req.UsernameCurrent)
|
||||
}
|
||||
|
||||
// compare password from request and the saved password
|
||||
match, err := s.ComparePasswordAndHash(req.PasswordCurrent, u.Password)
|
||||
if err != nil {
|
||||
return errors.New("error checking credentials")
|
||||
}
|
||||
|
||||
if !match {
|
||||
s.log.Debug().Msgf("bad credentials: %q | %q", req.UsernameCurrent, req.PasswordCurrent)
|
||||
return errors.Errorf("invalid login: %s", req.UsernameCurrent)
|
||||
}
|
||||
|
||||
hashed, err := s.CreateHash(req.PasswordNew)
|
||||
if err != nil {
|
||||
return errors.New("failed to hash password")
|
||||
}
|
||||
|
||||
req.PasswordNewHash = hashed
|
||||
|
||||
if err := s.userSvc.Update(ctx, req); err != nil {
|
||||
s.log.Error().Err(err).Msgf("could not change password for user: %s", req.UsernameCurrent)
|
||||
return errors.New("failed to change password")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) ComparePasswordAndHash(password string, hash string) (match bool, err error) {
|
||||
return argon2id.ComparePasswordAndHash(password, hash)
|
||||
}
|
||||
|
||||
func (s *service) CreateHash(password string) (hash string, err error) {
|
||||
return argon2id.CreateHash(password, argon2id.DefaultParams)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue