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

[10.x] Add custom recaller segments callback to SessionGuard #46237

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 14 additions & 3 deletions src/Illuminate/Auth/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class SessionGuard implements StatefulGuard, SupportsBasicAuth
*/
public readonly string $name;

/**
* Add additional segments to the recaller cookie.
*
* @var callable(\Illuminate\Contracts\Auth\Authenticatable): string
*/
public static $customRecallerSegments;

/**
* The user we last attempted to retrieve.
*
Expand Down Expand Up @@ -545,9 +552,13 @@ protected function ensureRememberTokenIsSet(AuthenticatableContract $user)
*/
protected function queueRecallerCookie(AuthenticatableContract $user)
{
$this->getCookieJar()->queue($this->createRecaller(
$user->getAuthIdentifier().'|'.$user->getRememberToken().'|'.$user->getAuthPassword()
));
$value = $user->getAuthIdentifier().'|'.$user->getRememberToken().'|'.$user->getAuthPassword();

if (isset(static::$customRecallerSegments)) {
$value .= '|'.(static::$customRecallerSegments)($user);
}

$this->getCookieJar()->queue($this->createRecaller($value));
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Auth/AuthGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,27 @@ public function testLoginMethodQueuesCookieWhenRemembering()
$guard->login($user, true);
}

public function testLoginMethodQueuesCookieWhenRememberingWithCustomSegments()
{
SessionGuard::$customRecallerSegments = fn () => 'custom|segments';
[$session, $provider, $request, $cookie] = $this->getMocks();
$guard = new SessionGuard('default', $provider, $session, $request);
$guard->setCookieJar($cookie);
$foreverCookie = new Cookie($guard->getRecallerName(), 'foo');
$cookie->shouldReceive('make')->once()->with($guard->getRecallerName(), 'foo|recaller|bar|custom|segments', 576000)->andReturn($foreverCookie);
$cookie->shouldReceive('queue')->once()->with($foreverCookie);
$guard->getSession()->shouldReceive('put')->once()->with($guard->getName(), 'foo');
$session->shouldReceive('migrate')->once();
$user = m::mock(Authenticatable::class);
$user->shouldReceive('getAuthIdentifier')->andReturn('foo');
$user->shouldReceive('getAuthPassword')->andReturn('bar');
$user->shouldReceive('getRememberToken')->andReturn('recaller');
$user->shouldReceive('setRememberToken')->never();
$provider->shouldReceive('updateRememberToken')->never();
$guard->login($user, true);
SessionGuard::$customRecallerSegments = null;
}

public function testLoginMethodQueuesCookieWhenRememberingAndAllowsOverride()
{
[$session, $provider, $request, $cookie] = $this->getMocks();
Expand Down