Skip to content
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

API Make token regeneration optional during autologin session renewal #11299

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/Security/MemberAuthenticator/CookieAuthenticationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use SilverStripe\Security\Member;
use SilverStripe\Security\RememberLoginHash;
use SilverStripe\Security\Security;
use SilverStripe\Dev\Deprecation;

/**
* Authenticate a member passed on a session cookie
Expand Down Expand Up @@ -175,17 +176,21 @@ public function authenticateRequest(HTTPRequest $request)
}

// Renew the token
$rememberLoginHash->renew();
$tokenExpiryDays = RememberLoginHash::config()->uninherited('token_expiry_days');
Cookie::set(
$this->getTokenCookieName(),
$member->ID . ':' . $rememberLoginHash->getToken(),
$tokenExpiryDays,
null,
null,
false,
true
);
Deprecation::withNoReplacement(fn() => $rememberLoginHash->renew());

// Send the new token to the client if it was changed
if ($rememberLoginHash->getToken()) {
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
$tokenExpiryDays = RememberLoginHash::config()->uninherited('token_expiry_days');
Cookie::set(
$this->getTokenCookieName(),
$member->ID . ':' . $rememberLoginHash->getToken(),
$tokenExpiryDays,
null,
null,
false,
true
);
}

// Audit logging hook
$member->extend('memberAutoLoggedIn');
Expand Down
28 changes: 24 additions & 4 deletions src/Security/RememberLoginHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DateTime;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Dev\Deprecation;

/**
* Persists a token associated with a device for users who opted for the "Remember Me"
Expand Down Expand Up @@ -80,7 +81,18 @@ class RememberLoginHash extends DataObject
private static $force_single_token = false;

/**
* The token used for the hash
* If true, the token will be replaced during session renewal. This can cause unexpected
* logouts if the new token does not reach the client (e.g. due to a network error).
*
* This can be disabled as of CMS 5.3, and renewal will be removed entirely in CMS 6.
* @deprecated 5.3.0 Will be removed without equivalent functionality
*/
private static bool $replace_token_during_session_renewal = true;

/**
* The token used for the hash. Only present during the lifetime of the request
* that generates it, as the hash representation is stored in the database and
* the token itself is sent to the client.
*/
private $token = null;

Expand Down Expand Up @@ -190,14 +202,22 @@ public static function generate(Member $member)
/**
* Generates a new hash for this member but keeps the device ID intact
*
* @deprecated 5.3.0 Will be removed without equivalent functionality
* @return RememberLoginHash
*/
public function renew()
{
$hash = $this->getNewHash($this->Member());
$this->Hash = $hash;
$this->extend('onAfterRenewToken');
// Only regenerate token if configured to do so
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
Deprecation::notice('5.3.0', 'Will be removed without equivalent functionality');
$replaceToken = RememberLoginHash::config()->get('replace_token_during_session_renewal');
if ($replaceToken) {
$hash = $this->getNewHash($this->Member());
$this->Hash = $hash;
}

$this->extend('onAfterRenewToken', $replaceToken);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GuySartorelli Just considering how we'll handle this extension point once we drop the method; My current thought is that we shift the call-site up to CookieAuthenticationHandler::authenticateRequest(), but still have it be an extension of RememberLoginHash (possibly with a tweaked name to reflect that the token itself is not being renewed.)

There's precedent in the authenticateRequest() method for calling an extension point externally on an object instance (see the $member->extend('memberAutoLoggedIn'); call) but not sure if this falls within our general best practices?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the idea with deprecating and eventually removing the renew() method that there will be no renewal necessary? If we don't renew the token, we don't need to have a onAfterRenewToken extension hook, right?

$this->write();

return $this;
}

Expand Down
39 changes: 39 additions & 0 deletions tests/php/Security/RememberLoginHashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SilverStripe\Security\Member;
use SilverStripe\Security\RememberLoginHash;
use SilverStripe\SessionManager\Models\LoginSession;
use SilverStripe\Dev\Deprecation;

class RememberLoginHashTest extends SapphireTest
{
Expand Down Expand Up @@ -95,4 +96,42 @@ public function testGetSetLogoutAcrossDevices()
RememberLoginHash::setLogoutAcrossDevices(false);
$this->assertFalse(RememberLoginHash::getLogoutAcrossDevices());
}

/**
* @dataProvider provideRenew
* @param bool $replaceToken
*/
public function testRenew($replaceToken)
{
// If session-manager module is installed it expects an active request during renewal
if (class_exists(LoginSession::class)) {
$this->markTestSkipped();
}

$member = $this->objFromFixture(Member::class, 'main');

RememberLoginHash::config()->set('replace_token_during_session_renewal', $replaceToken);

$hash = RememberLoginHash::generate($member);
$oldToken = $hash->getToken();
$oldHash = $hash->Hash;

Deprecation::withNoReplacement(fn() => $hash->renew());

if ($replaceToken) {
$this->assertNotEquals($oldToken, $hash->getToken());
$this->assertNotEquals($oldHash, $hash->Hash);
} else {
$this->assertEmpty($hash->getToken());
$this->assertEquals($oldHash, $hash->Hash);
}
}

public function provideRenew(): array
{
return [
[true],
[false],
];
}
}
Loading