Skip to content

Commit 78a56ba

Browse files
committed
chore: be more specific about pwd requirement
1 parent 78ca01e commit 78a56ba

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

internal/api/apierrors/errorcode.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ const (
7171
ErrorCodeReauthenticationNeeded ErrorCode = "reauthentication_needed"
7272
ErrorCodeSamePassword ErrorCode = "same_password"
7373
ErrorCodeReauthenticationNotValid ErrorCode = "reauthentication_not_valid"
74-
ErrorCodeCurrentPasswordMismatch ErrorCode = "current_password_required"
74+
ErrorCodeCurrentPasswordMismatch ErrorCode = "current_password_invalid"
75+
ErrorCodeCurrentPasswordRequired ErrorCode = "current_password_required"
7576
ErrorCodeOTPExpired ErrorCode = "otp_expired"
7677
ErrorCodeOTPDisabled ErrorCode = "otp_disabled"
7778
ErrorCodeIdentityNotFound ErrorCode = "identity_not_found"

internal/api/user.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,12 @@ func (a *API) UserUpdate(w http.ResponseWriter, r *http.Request) error {
168168
if user.HasPassword() {
169169
// current password required when updating password
170170
if config.Security.UpdatePasswordRequireCurrentPassword {
171-
isCurrentPasswordCorrect := false
172-
if params.CurrentPassword != nil && *params.CurrentPassword != "" {
173-
auth, _, err := user.Authenticate(ctx, db, *params.CurrentPassword, config.Security.DBEncryption.DecryptionKeys, false, "")
174-
if err != nil {
175-
return err
176-
}
177-
isCurrentPasswordCorrect = auth
171+
if params.CurrentPassword == nil || *params.CurrentPassword == "" {
172+
return apierrors.NewBadRequestError(apierrors.ErrorCodeCurrentPasswordRequired, "Current password required when setting new password.")
173+
}
174+
isCurrentPasswordCorrect, _, err := user.Authenticate(ctx, db, *params.CurrentPassword, config.Security.DBEncryption.DecryptionKeys, false, "")
175+
if err != nil {
176+
return err
178177
}
179178
if !isCurrentPasswordCorrect {
180179
return apierrors.NewBadRequestError(apierrors.ErrorCodeCurrentPasswordMismatch, "Current password required when setting new password.")

internal/api/user_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,15 @@ func (ts *UserTestSuite) TestUserUpdatePassword() {
356356
sessionId: r.SessionId,
357357
expected: expected{code: http.StatusBadRequest, isAuthenticated: false},
358358
},
359+
{
360+
desc: "Fails if current password not set when required",
361+
newPassword: "newpassword123",
362+
nonce: "",
363+
requireReauthentication: false,
364+
requireCurrentPassword: true,
365+
sessionId: r.SessionId,
366+
expected: expected{code: http.StatusBadRequest, isAuthenticated: false},
367+
},
359368
}
360369

361370
for _, c := range cases {

0 commit comments

Comments
 (0)