End other sessions on password change and give admin links 72 hours
- AuthenticateSession in the web group logs out a user's other sessions when their password changes. - Set-password links made by an admin use a new admin_links broker with its own password_setup_tokens table and a 72-hour expiry, so links handed over by hand don't expire in an hour. Forgot-password links keep 60 minutes. The reset page accepts both, and issuing or using a link ends the user's earlier links. - Admin links are emailed with PasswordSetupLinkNotification; the email body is shared with the forgot-password email via PasswordLinkMail. - Spec chapter 3 and CLAUDE.md describe the link lifetimes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
use Illuminate\Auth\Notifications\ResetPassword;
|
||||
use App\Modules\Users\Infrastructure\Auth\PasswordSetupLinkNotification;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
|
||||
@@ -20,7 +20,7 @@ it('creates an admin, emails a set-password link and prints it', function () {
|
||||
expect($admin->role)->toBe(Role::Admin)
|
||||
->and($admin->email)->toBe('[email protected]');
|
||||
|
||||
Notification::assertSentTo($admin, ResetPassword::class);
|
||||
Notification::assertSentTo($admin, PasswordSetupLinkNotification::class);
|
||||
});
|
||||
|
||||
it('fails for an email address that is in use', function () {
|
||||
|
||||
@@ -6,6 +6,8 @@ use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\Facades\Password;
|
||||
use Illuminate\Support\Str;
|
||||
use Livewire\Features\SupportTesting\Testable;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
@@ -123,3 +125,65 @@ it('rejects a short password and an invalid link', function () {
|
||||
|
||||
expect(Hash::check('password', $user->fresh()->password))->toBeTrue();
|
||||
});
|
||||
|
||||
/**
|
||||
* Sets the user's password through the reset page with the given token.
|
||||
*/
|
||||
function setPasswordWithToken(string $token, string $email = '[email protected]'): Testable
|
||||
{
|
||||
return Livewire::test('users::reset-password', ['token' => $token])
|
||||
->set('email', $email)
|
||||
->set('password', 'Uusi-salasana-123')
|
||||
->set('password_confirmation', 'Uusi-salasana-123')
|
||||
->call('resetPassword');
|
||||
}
|
||||
|
||||
it('accepts a link made by an admin for 72 hours, but a forgot-password link only for 60 minutes', function (string $broker, int $minutes, bool $valid) {
|
||||
$user = User::factory()->create(['email' => '[email protected]']);
|
||||
$token = Password::broker($broker)->createToken($user);
|
||||
|
||||
$this->travel($minutes)->minutes();
|
||||
|
||||
$reset = setPasswordWithToken($token);
|
||||
|
||||
$valid ? $reset->assertHasNoErrors() : $reset->assertHasErrors(['email']);
|
||||
expect(Hash::check('Uusi-salasana-123', $user->fresh()->password))->toBe($valid);
|
||||
})->with([
|
||||
'admin link after 2 hours' => ['admin_links', 120, true],
|
||||
'admin link after 73 hours' => ['admin_links', 73 * 60, false],
|
||||
'forgot-password link after 59 minutes' => ['users', 59, true],
|
||||
'forgot-password link after 61 minutes' => ['users', 61, false],
|
||||
]);
|
||||
|
||||
it('ends earlier links when an admin makes a new one, and every link when one is used', function () {
|
||||
Notification::fake();
|
||||
$admin = User::factory()->admin()->create();
|
||||
$user = User::factory()->create(['email' => '[email protected]']);
|
||||
$forgotToken = Password::createToken($user);
|
||||
|
||||
$page = Livewire::actingAs($admin)->test('users::form', ['user' => $user->id])->call('sendPasswordLink');
|
||||
$adminToken = Str::between($page->get('setupLink'), '/reset-password/', '?');
|
||||
|
||||
expect(Password::tokenExists($user, $forgotToken))->toBeFalse();
|
||||
|
||||
auth()->logout();
|
||||
setPasswordWithToken($adminToken)->assertHasNoErrors();
|
||||
expect(Password::broker('admin_links')->tokenExists($user, $adminToken))->toBeFalse();
|
||||
|
||||
$newForgotToken = Password::createToken($user);
|
||||
$newAdminToken = Password::broker('admin_links')->createToken($user);
|
||||
setPasswordWithToken($newForgotToken)->assertHasNoErrors();
|
||||
|
||||
expect(Password::broker('admin_links')->tokenExists($user, $newAdminToken))->toBeFalse();
|
||||
});
|
||||
|
||||
it('ends the other sessions of a user whose password changes', function () {
|
||||
$user = User::factory()->create(['email' => '[email protected]']);
|
||||
|
||||
$this->actingAs($user)->get(route('home'))->assertOk();
|
||||
|
||||
$user->forceFill(['password' => 'Uusi-salasana-123'])->save();
|
||||
|
||||
$this->get(route('home'))->assertRedirect(route('login'));
|
||||
$this->assertGuest();
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
use Illuminate\Auth\Notifications\ResetPassword;
|
||||
use App\Modules\Users\Infrastructure\Auth\PasswordSetupLinkNotification;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\Facades\Password;
|
||||
@@ -68,7 +68,7 @@ it('creates an intern, emails a set-password link and shows it to copy', functio
|
||||
->and($intern->employment_ends_on?->toDateString())->toBe('2027-05-31')
|
||||
->and($intern->supervisor_id)->toBe($supervisor->id);
|
||||
|
||||
Notification::assertSentTo($intern, ResetPassword::class, function (ResetPassword $notification) use ($component) {
|
||||
Notification::assertSentTo($intern, PasswordSetupLinkNotification::class, function (PasswordSetupLinkNotification $notification) use ($component) {
|
||||
return str_contains($component->get('setupLink'), '/reset-password/'.$notification->token);
|
||||
});
|
||||
});
|
||||
@@ -154,8 +154,8 @@ it('makes a new set-password link on the edit page, also right after an earlier
|
||||
|
||||
$token = Str::between($component->get('setupLink'), '/reset-password/', '?');
|
||||
|
||||
Notification::assertSentToTimes($user, ResetPassword::class, 2);
|
||||
expect(Password::tokenExists($user, $token))->toBeTrue();
|
||||
Notification::assertSentToTimes($user, PasswordSetupLinkNotification::class, 2);
|
||||
expect(Password::broker('admin_links')->tokenExists($user, $token))->toBeTrue();
|
||||
});
|
||||
|
||||
it('does not let the set-password link be changed in the browser', function () {
|
||||
|
||||
Reference in New Issue
Block a user