summaryrefslogtreecommitdiff
path: root/internal/processing/user/password.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2023-07-23 12:33:17 +0200
committerLibravatar GitHub <noreply@github.com>2023-07-23 12:33:17 +0200
commit5a29a031adbcaca85ad641bf74254d3ea985d03c (patch)
treeeda06b4d3b09207bc6d4dc6e9e659e8f283028dc /internal/processing/user/password.go
parent[feature] Report Masto version in /api/v1/instance (#1977) (diff)
downloadgotosocial-5a29a031adbcaca85ad641bf74254d3ea985d03c.tar.xz
[chore] Admin CLI + new account creation refactoring (#2008)
* set maxPasswordLength to 72 bytes, rename validate function * refactor NewSignup * refactor admin account CLI commands * refactor oidc create user * refactor processor create * tweak password change, check old != new password
Diffstat (limited to 'internal/processing/user/password.go')
-rw-r--r--internal/processing/user/password.go31
1 files changed, 25 insertions, 6 deletions
diff --git a/internal/processing/user/password.go b/internal/processing/user/password.go
index 244a5d349..68bc8ddb5 100644
--- a/internal/processing/user/password.go
+++ b/internal/processing/user/password.go
@@ -28,22 +28,41 @@ import (
// PasswordChange processes a password change request for the given user.
func (p *Processor) PasswordChange(ctx context.Context, user *gtsmodel.User, oldPassword string, newPassword string) gtserror.WithCode {
+ // Ensure provided oldPassword is the correct current password.
if err := bcrypt.CompareHashAndPassword([]byte(user.EncryptedPassword), []byte(oldPassword)); err != nil {
+ err := gtserror.Newf("%w", err)
return gtserror.NewErrorUnauthorized(err, "old password was incorrect")
}
- if err := validate.NewPassword(newPassword); err != nil {
+ // Ensure new password is strong enough.
+ if err := validate.Password(newPassword); err != nil {
return gtserror.NewErrorBadRequest(err, err.Error())
}
- newPasswordHash, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
- if err != nil {
- return gtserror.NewErrorInternalError(err, "error hashing password")
+ // Ensure new password is different from old password.
+ if newPassword == oldPassword {
+ const help = "new password cannot be the same as previous password"
+ err := gtserror.New(help)
+ return gtserror.NewErrorBadRequest(err, help)
}
- user.EncryptedPassword = string(newPasswordHash)
+ // Hash the new password.
+ encryptedPassword, err := bcrypt.GenerateFromPassword(
+ []byte(newPassword),
+ bcrypt.DefaultCost,
+ )
+ if err != nil {
+ err := gtserror.Newf("%w", err)
+ return gtserror.NewErrorInternalError(err)
+ }
- if err := p.state.DB.UpdateUser(ctx, user, "encrypted_password"); err != nil {
+ // Set new password on user.
+ user.EncryptedPassword = string(encryptedPassword)
+ if err := p.state.DB.UpdateUser(
+ ctx, user,
+ "encrypted_password",
+ ); err != nil {
+ err := gtserror.Newf("db error updating user: %w", err)
return gtserror.NewErrorInternalError(err)
}