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:
2026-11-23 17:42:37 +02:00
co-authored by Claude Opus 5.5
parent 9af0641e17
commit 8dfc02c4e5
14 changed files with 223 additions and 35 deletions
@@ -12,13 +12,21 @@ use LogicException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
/**
* Uses Laravel's password reset tokens and notification, which the Users
* module provider words as a "set your password" email.
* Uses Laravel's password reset tokens. Forgot-password links use the
* default broker (60 minutes); links made by an admin use the admin_links
* broker, which has its own table and a longer expiry.
*/
class LaravelPasswordLinkSender implements PasswordLinkSender
{
public const string ADMIN_BROKER = 'admin_links';
public function __construct(private PasswordBroker $passwords) {}
public static function adminLinkHours(): int
{
return intdiv((int) config('auth.passwords.'.self::ADMIN_BROKER.'.expire'), 60);
}
public function sendSetupLink(EmailAddress $email): bool
{
try {
@@ -37,7 +45,9 @@ class LaravelPasswordLinkSender implements PasswordLinkSender
$user = User::query()->where('email', $email->value)->first()
?? throw new LogicException('A set-password link was asked for an unknown user.');
$token = Password::createToken($user);
// The new link replaces every earlier link of the user.
Password::broker()->deleteToken($user);
$token = Password::broker(self::ADMIN_BROKER)->createToken($user);
$url = route('password.reset', [
'token' => $token,
@@ -45,7 +55,7 @@ class LaravelPasswordLinkSender implements PasswordLinkSender
]);
try {
$user->sendPasswordResetNotification($token);
$user->notify(new PasswordSetupLinkNotification($token));
$emailSent = true;
} catch (TransportExceptionInterface $exception) {
// The admin still gets the link to hand over another way.
@@ -0,0 +1,28 @@
<?php
namespace App\Modules\Users\Infrastructure\Auth;
use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Notifications\Messages\MailMessage;
/**
* The "set your password" email, shared by forgot-password links and links
* made by an admin. Only the expiry line differs.
*/
final class PasswordLinkMail
{
public static function build(CanResetPassword $user, string $token, string $expiryLine): MailMessage
{
return (new MailMessage)
->subject(__('users.password_mail.subject'))
->greeting(__('users.password_mail.greeting'))
->line(__('users.password_mail.intro'))
->action(__('users.password_mail.action'), route('password.reset', [
'token' => $token,
'email' => $user->getEmailForPasswordReset(),
]))
->line($expiryLine)
->line(__('users.password_mail.ignore'))
->salutation(config('app.name'));
}
}
@@ -0,0 +1,31 @@
<?php
namespace App\Modules\Users\Infrastructure\Auth;
use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
/**
* Emails a set-password link made by an admin. It is valid longer than a
* forgot-password link, because the admin may also hand it over by hand.
*/
class PasswordSetupLinkNotification extends Notification
{
public function __construct(public string $token) {}
/**
* @return list<string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}
public function toMail(CanResetPassword $notifiable): MailMessage
{
return PasswordLinkMail::build($notifiable, $this->token, __('users.password_mail.expires_hours', [
'count' => LaravelPasswordLinkSender::adminLinkHours(),
]));
}
}