This is a follow up to #2847708: RPC endpoint to reset user password and just adding it for discussion.

I was hoping to submit a patch for this but I found that this will require a bit of rework as how _skipProtectedUserFieldConstraint is worked in the workflow will likely either need duplication or some proper planning on how to rework.

I wonder if the validation of a password reset token should be added to the actual ProtectedUserFieldConstraint instead of outside.

In the meantime, I worked on an alternative to this on https://www.drupal.org/sandbox/hanoii/2904436

It should work for both 8.3.x and 8.4.x.

Issue fork drupal-2904451

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

hanoii created an issue. See original summary.

hanoii’s picture

wim leers’s picture

Title: Allow to update password through RPC endpoints through the reset password workflow » Allow to change password through RPC endpoints through the reset password workflow
Issue tags: +API-First Initiative

Sounds like a sensible feature request :)

wim leers’s picture

Left a comment at #2403307-295: RPC endpoints for user authentication: log in, check login status, log out to people who were on the original issue aware of this follow-up.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

wim leers’s picture

Component: rest.module » user.module

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

joao sausen’s picture

This patch has some security issues.

Same thing as https://www.drupal.org/project/drupal/issues/1521996

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bradjones1’s picture

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Version: 9.5.x-dev » 10.1.x-dev
andypost’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

skyredwang’s picture

Status: Active » Needs review
StatusFileSize
new4.75 KB

Here is the first draft, no tests. I am reusing the /user/password endpoint, if the HTTP method is POST, then this implementation assume it is for resetting password, if the HTTP method is PATCH, then it is for changing password.

_utsavsharma’s picture

StatusFileSize
new4.6 KB
new4.6 KB

As previous patch failed , patch for 11.x.

alexpott’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/user/src/Controller/UserAuthenticationController.php
    @@ -260,15 +262,52 @@ public function resetPassword(Request $request) {
    +        if (!$account->isAuthenticated()) {
    +          throw new BadRequestHttpException('Authentication is required to change your password.');
    +        }
    

    Once the patch request is a separate route you can make this a route requirement.

  2. +++ b/core/modules/user/src/Controller/UserAuthenticationController.php
    @@ -260,15 +262,52 @@ public function resetPassword(Request $request) {
    +        // Set existing password if set in the form state.
    

    No form state here.

  3. +++ b/core/modules/user/user.routing.yml
    @@ -132,8 +132,8 @@ user.pass:
     user.pass.http:
       path: '/user/password'
       defaults:
    -    _controller: \Drupal\user\Controller\UserAuthenticationController::resetPassword
    -  methods: [POST]
    +    _controller: \Drupal\user\Controller\UserAuthenticationController::changeOrResetPassword
    +  methods: [POST, PATCH]
       requirements:
         _access: 'TRUE'
         _format: 'json'
    

    Let's add a different route for just patch. And let it have it's own controller. I don;t think we shoudl be renaming resetPassword here. And I think having a separate changePassword method is okay.

    We can refactor the common parts of both methods into private functions on the controller.

skyredwang’s picture

StatusFileSize
new4.72 KB

The reason I didn't want to use a seperate controller is because the existing resetPassword uses /user/password.

/user/password is generic, if I use a new path like /user/change_password for changing password, then we have two end points for the consumer. One generic, one specific, that's confusing to me. What do you think?

Attached improved the code comment per suggestion 2

skyredwang’s picture

StatusFileSize
new4.76 KB

Corrected the patch path

bradjones1’s picture

Re-queued the patch since it's against 11.x but DrupalCI was running it against 10.1.

Also just want to say I love that this is using HTTP verb semantics to determine the operation to be performed!

skyredwang’s picture

Status: Needs work » Needs review

I spoke with @alexpott on Slack, and understood that I can keep the same endpoint but defining a new route based on the new HTTP method. So, I agree with his suggestions.

I pushed the change to the issue fork 11.x

smustgrave’s picture

Status: Needs review » Needs work

Hiding patches as work is in a MR with correct branch.

But appears to have some failures.

skyredwang’s picture

Status: Needs work » Needs review

The lastest version is functioning. It supports two use cases:

- Change password knowing the existing password
- Change password without knowing the existing password, but have the timestamp and token from forget password URL

Remaining work to do:

- Tests
- Some of the code are copied from /core/modules/user/src/AccountForm.php and /opt/drupal/web/core/modules/user/src/Controller/UserController.php , better to re-organize them, but need suggestions, as I am not familiar with core.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Passed tests, just needs it's own coverage as mentioned.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.