-
Notifications
You must be signed in to change notification settings - Fork 823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH Refactor password reset to support SameSite=Strict #11566
Draft
Cheddam
wants to merge
1
commit into
silverstripe:5
Choose a base branch
from
Cheddam:pulls/5/password-reset-samesite-strict-compatibility
base: 5
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,14 +75,6 @@ public function changepassword() | |
|
||
// Check whether we are merely changing password, or resetting. | ||
if ($token !== null && $member && $member->validateAutoLoginToken($token)) { | ||
$this->setSessionToken($member, $token); | ||
|
||
// Redirect to myself, but without the hash in the URL | ||
return $this->redirect($this->link); | ||
} | ||
|
||
$session = $this->getRequest()->getSession(); | ||
if ($session->get('AutoLoginHash')) { | ||
$message = DBField::create_field( | ||
'HTMLFragment', | ||
'<p>' . _t( | ||
|
@@ -91,10 +83,12 @@ public function changepassword() | |
) . '</p>' | ||
); | ||
|
||
// Subsequent request after the "first load with hash" (see previous if clause). | ||
$form = $this->changePasswordForm(); | ||
$form->addAutoLoginHash($member->encryptWithUserSettings($token)); | ||
|
||
return [ | ||
'Content' => $message, | ||
'Form' => $this->changePasswordForm() | ||
'Form' => $form, | ||
]; | ||
} | ||
|
||
|
@@ -110,7 +104,7 @@ public function changepassword() | |
|
||
return [ | ||
'Content' => $message, | ||
'Form' => $this->changePasswordForm() | ||
'Form' => $this->changePasswordForm(), | ||
]; | ||
} | ||
// Show a friendly message saying the login token has expired | ||
|
@@ -144,23 +138,6 @@ public function changepassword() | |
); | ||
} | ||
|
||
|
||
/** | ||
* @param Member $member | ||
* @param string $token | ||
*/ | ||
protected function setSessionToken($member, $token) | ||
{ | ||
// if there is a current member, they should be logged out | ||
if ($curMember = Security::getCurrentUser()) { | ||
Injector::inst()->get(IdentityStore::class)->logOut(); | ||
} | ||
|
||
$this->getRequest()->getSession()->regenerateSessionId(); | ||
// Store the hash for the change password form. Will be unset after reload within the ChangePasswordForm. | ||
$this->getRequest()->getSession()->set('AutoLoginHash', $member->encryptWithUserSettings($token)); | ||
} | ||
|
||
/** | ||
* Return a link to this request handler. | ||
* The link returned is supplied in the constructor | ||
|
@@ -215,16 +192,13 @@ public function doChangePassword(array $data, $form) | |
return $this->redirectBackToForm(); | ||
} | ||
|
||
$session = $this->getRequest()->getSession(); | ||
if (!$member) { | ||
if ($session->get('AutoLoginHash')) { | ||
$member = Member::member_from_autologinhash($session->get('AutoLoginHash')); | ||
if (isset($data['AutoLoginHash'])) { | ||
$member = Member::member_from_autologinhash($data['AutoLoginHash']); | ||
} | ||
|
||
// The user is not logged in and no valid auto login hash is available | ||
// The user is not logged in and no valid token was provided | ||
if (!$member) { | ||
$session->clear('AutoLoginHash'); | ||
|
||
return $this->redirect($this->addBackURLParam(Security::singleton()->Link('login'))); | ||
} | ||
} | ||
|
@@ -240,7 +214,7 @@ public function doChangePassword(array $data, $form) | |
); | ||
|
||
// redirect back to the form, instead of using redirectBack() which could send the user elsewhere. | ||
return $this->redirectBackToForm(); | ||
return $this->redirectBackToForm($member->ID, $data['AutoLoginHash']); | ||
} | ||
|
||
// Fail if passwords do not match | ||
|
@@ -254,15 +228,15 @@ public function doChangePassword(array $data, $form) | |
); | ||
|
||
// redirect back to the form, instead of using redirectBack() which could send the user elsewhere. | ||
return $this->redirectBackToForm(); | ||
return $this->redirectBackToForm($member->ID, $data['AutoLoginHash']); | ||
} | ||
|
||
// Check if the new password is accepted | ||
$validationResult = $member->changePassword($data['NewPassword1']); | ||
if (!$validationResult->isValid()) { | ||
$form->setSessionValidationResult($validationResult); | ||
|
||
return $this->redirectBackToForm(); | ||
return $this->redirectBackToForm($member->ID, $data['AutoLoginHash']); | ||
} | ||
|
||
// Clear locked out status | ||
|
@@ -293,8 +267,6 @@ public function doChangePassword(array $data, $form) | |
$identityStore->logIn($member, false, $this->getRequest()); | ||
} | ||
|
||
$session->clear('AutoLoginHash'); | ||
|
||
// Redirect to backurl | ||
$backURL = $this->getBackURL(); | ||
if ($backURL | ||
|
@@ -319,10 +291,18 @@ public function doChangePassword(array $data, $form) | |
* | ||
* @return HTTPResponse | ||
*/ | ||
public function redirectBackToForm() | ||
public function redirectBackToForm(?int $withMemberID = null, ?string $withToken = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't amend public method signatures in a minor release, as they count as part of our "Public API" - see https://docs.silverstripe.org/en/5/project_governance/public_api/ |
||
{ | ||
// Redirect back to form | ||
$url = $this->addBackURLParam(Security::singleton()->Link('changepassword')); | ||
$url = Security::singleton()->Link('changepassword'); | ||
|
||
// Include token data if performing an unauthenticated password reset | ||
if ($withMemberID && $withToken) { | ||
$url = Controller::join_links($url, "?m={$withMemberID}&t={$withToken}"); | ||
} | ||
|
||
// Add Back URL if available | ||
$url = $this->addBackURLParam($url); | ||
|
||
return $this->redirect($url); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,3 @@ SilverStripe\Security\Member: | |
sarah: | ||
FirstName: Sarah | ||
Surname: Smith | ||
AutoLoginToken: foobar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't remove
protected
functions in a minor release, as they count as part of our "Public API" - see https://docs.silverstripe.org/en/5/project_governance/public_api/