From 5221b02af6227edcb6d063fdaeb9c07a0436e9c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Wed, 30 Oct 2024 10:23:49 +0100 Subject: [PATCH] feat: add 'remember me' functionality --- config/packages/security.yaml | 5 +++++ migrations/Version20241030091854.php | 31 ++++++++++++++++++++++++++++ src/Security/BpnAuthenticator.php | 6 +++++- templates/security/login.html.twig | 8 +++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 migrations/Version20241030091854.php diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 04d7aa8..560c7a3 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -26,6 +26,11 @@ security: logout: path: app_security_logout target: app_security_login + remember_me: + secret: '%kernel.secret%' + lifetime: 604800 + token_provider: + doctrine: true access_control: - { path: '^/', roles: PUBLIC_ACCESS, requires_channel: https } diff --git a/migrations/Version20241030091854.php b/migrations/Version20241030091854.php new file mode 100644 index 0000000..ffd9d02 --- /dev/null +++ b/migrations/Version20241030091854.php @@ -0,0 +1,31 @@ +addSql('CREATE TABLE rememberme_token (series VARCHAR(88) NOT NULL, value VARCHAR(88) NOT NULL, lastUsed DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', class VARCHAR(100) NOT NULL, username VARCHAR(200) NOT NULL, PRIMARY KEY(series)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE rememberme_token'); + } +} diff --git a/src/Security/BpnAuthenticator.php b/src/Security/BpnAuthenticator.php index 547bb9d..21f2a0e 100644 --- a/src/Security/BpnAuthenticator.php +++ b/src/Security/BpnAuthenticator.php @@ -17,6 +17,7 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator; use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge; +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge; use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; use Symfony\Component\Security\Http\Authenticator\Passport\Passport; use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; @@ -73,7 +74,10 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent return $user; }), - [new CsrfTokenBadge('authenticate', $csrfToken)] + [ + new CsrfTokenBadge('authenticate', $csrfToken), + new RememberMeBadge(), + ] ); } diff --git a/templates/security/login.html.twig b/templates/security/login.html.twig index c4955de..39261c9 100644 --- a/templates/security/login.html.twig +++ b/templates/security/login.html.twig @@ -40,6 +40,14 @@ required autocomplete="current-password"> +
+ +