mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-20 12:39:13 +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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := auth.ChangePassword(context.Background(), req); err != nil {
|
if apiErr := auth.ChangePassword(context.Background(), req); apiErr != nil {
|
||||||
if aH.HandleError(w, err, http.StatusInternalServerError) {
|
RespondError(w, apiErr, nil)
|
||||||
return
|
return
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
aH.WriteJSON(w, r, map[string]string{"data": "password changed successfully"})
|
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
|
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)
|
user, apiErr := dao.DB().GetUser(ctx, req.UserId)
|
||||||
if apiErr != nil {
|
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) {
|
if user == nil || !passwordMatch(user.Password, req.OldPassword) {
|
||||||
return ErrorInvalidCreds
|
return model.ForbiddenError(ErrorInvalidCreds)
|
||||||
}
|
}
|
||||||
|
|
||||||
hash, err := PasswordHash(req.NewPassword)
|
hash, err := PasswordHash(req.NewPassword)
|
||||||
if err != nil {
|
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 {
|
if apiErr := dao.DB().UpdateUserPassword(ctx, hash, user.Id); apiErr != nil {
|
||||||
return apiErr.Err
|
return apiErr
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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 {
|
func WrapApiError(err *ApiError, msg string) *ApiError {
|
||||||
return &ApiError{
|
return &ApiError{
|
||||||
Typ: err.Type(),
|
Typ: err.Type(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user