mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-20 05:19:11 +08:00
fix: send 403 on wrong password entry during change password operation (#4733)
This commit is contained in:
parent
f24135f5b0
commit
994814864c
@ -2363,10 +2363,9 @@ func (aH *APIHandler) changePassword(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := auth.ChangePassword(context.Background(), req); err != nil {
|
||||
if aH.HandleError(w, err, http.StatusInternalServerError) {
|
||||
return
|
||||
}
|
||||
if apiErr := auth.ChangePassword(context.Background(), req); apiErr != nil {
|
||||
RespondError(w, apiErr, nil)
|
||||
return
|
||||
|
||||
}
|
||||
aH.WriteJSON(w, r, map[string]string{"data": "password changed successfully"})
|
||||
|
@ -234,24 +234,23 @@ func ResetPassword(ctx context.Context, req *model.ResetPasswordRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func ChangePassword(ctx context.Context, req *model.ChangePasswordRequest) error {
|
||||
|
||||
func ChangePassword(ctx context.Context, req *model.ChangePasswordRequest) *model.ApiError {
|
||||
user, apiErr := dao.DB().GetUser(ctx, req.UserId)
|
||||
if apiErr != nil {
|
||||
return errors.Wrap(apiErr.Err, "failed to query user from the DB")
|
||||
return apiErr
|
||||
}
|
||||
|
||||
if user == nil || !passwordMatch(user.Password, req.OldPassword) {
|
||||
return ErrorInvalidCreds
|
||||
return model.ForbiddenError(ErrorInvalidCreds)
|
||||
}
|
||||
|
||||
hash, err := PasswordHash(req.NewPassword)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Failed to generate password hash")
|
||||
return model.InternalError(errors.New("Failed to generate password hash"))
|
||||
}
|
||||
|
||||
if apiErr := dao.DB().UpdateUserPassword(ctx, hash, user.Id); apiErr != nil {
|
||||
return apiErr.Err
|
||||
return apiErr
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -112,6 +112,13 @@ func UnavailableError(err error) *ApiError {
|
||||
}
|
||||
}
|
||||
|
||||
func ForbiddenError(err error) *ApiError {
|
||||
return &ApiError{
|
||||
Typ: ErrorForbidden,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
func WrapApiError(err *ApiError, msg string) *ApiError {
|
||||
return &ApiError{
|
||||
Typ: err.Type(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user