Skip to content

Commit

Permalink
implement reset password
Browse files Browse the repository at this point in the history
  • Loading branch information
erudenko committed Jul 13, 2023
1 parent 20df54b commit 8edfe10
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion server/controller/user_controller_challenges.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (c *UserStorageController) LoginOrRegisterUserWithChallenge(ctx context.Con
return model.User{}, l.LocalizedError{ErrID: l.ErrorLoginTypeNotSupported}
}

u, _, err := c.VerifyChallenge(ctx, challenge, userIDValue)
u, _, _, err := c.VerifyChallenge(ctx, challenge, userIDValue)
if err != nil {
return model.User{}, err
}
Expand Down
36 changes: 19 additions & 17 deletions web/api/reset_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ func (ar *Router) RequestResetPassword() http.HandlerFunc {
// getting the new password and saving it in the database.
func (ar *Router) ResetPassword() http.HandlerFunc {
type newPassword struct {
Password string `json:"password,omitempty"`
Password string `json:"password,omitempty"`
Scopes []string `json:"scopes,omitempty"`
}

return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -91,37 +92,38 @@ func (ar *Router) ResetPassword() http.HandlerFunc {
if ar.MustParseJSON(w, r, &d) != nil {
return
}
if err := model.StrongPswd(d.Password); err != nil {
ar.Error(w, locale, http.StatusBadRequest, l.ErrorAPIRequestPasswordWeak, err)

token := tokenFromContext(r.Context())
if token == nil {
ar.LocalizedError(w, locale, http.StatusInternalServerError, l.ErrorAPIContextNoToken)
return
}

accessTokenBytes, ok := r.Context().Value(model.TokenRawContextKey).([]byte)
if !ok {
ar.Error(w, locale, http.StatusBadRequest, l.ErrorAPIContextNoToken)
// Validate just in case, middleware should do it for us.
if token.Type() != model.TokenTypeReset {
ar.LocalizedError(w, locale, http.StatusBadRequest, l.ErrorAPIRequestTokenInvalid)
return
}

// Get userID from token and update user with this ID.
userID, err := ar.getTokenSubject(string(accessTokenBytes))
// Let's update the password.
err := ar.server.Storages().UMC.UpdateUserPassword(r.Context(), "", d.Password)
if err != nil {
ar.LocalizedError(w, locale, http.StatusInternalServerError, l.ErrorAPIRequestTokenSubError, err)
ar.HTTPError(w, err, http.StatusInternalServerError)
return
}

user, err := ar.server.Storages().User.UserByID(userID)
app := middleware.AppFromContext(r.Context())
u, err := ar.server.Storages().UC.UserByID(r.Context(), token.UserID())
if err != nil {
ar.LocalizedError(w, locale, http.StatusUnauthorized, l.ErrorStorageFindUserIDError, userID, err)
ar.HTTPError(w, err, http.StatusInternalServerError)
return
}

// Save new password.
if err := ar.server.Storages().User.ResetPassword(user.ID, d.Password); err != nil {
ar.Error(w, locale, http.StatusInternalServerError, l.ErrorStorageResetPasswordUserError, user.ID, err)
loginResponse, err := ar.server.Storages().UC.GetJWTTokens(r.Context(), app, u, d.Scopes)
if err != nil {
ar.HTTPError(w, err, http.StatusInternalServerError)
return
}

result := map[string]string{"result": "ok"}
ar.ServeJSON(w, locale, http.StatusOK, result)
ar.ServeJSON(w, locale, http.StatusOK, loginResponse)
}
}

0 comments on commit 8edfe10

Please sign in to comment.