Skip to content

Commit 1a8818d

Browse files
committed
Implement proper serialization of passwords for User entity
1 parent c91376d commit 1a8818d

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

src/Doctrine/Entity/User.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface, TwoFact
5151
#[Column(nullable: true)]
5252
private ?string $totpSecret = null;
5353

54+
public function __serialize(): array
55+
{
56+
// todo move plain password to DTO
57+
$this->clearPlainPassword();
58+
59+
// Prevent storing the hashed password directly in the user session
60+
$data = (array) $this;
61+
$data["\0" . self::class . "\0password"] = hash('crc32c', (string) $this->password);
62+
63+
return $data;
64+
}
65+
5466
public function getId(): ?int
5567
{
5668
return $this->id;
@@ -124,6 +136,11 @@ public function setPlainPassword(string $password): self
124136
return $this;
125137
}
126138

139+
public function clearPlainPassword(): void
140+
{
141+
$this->plainPassword = null;
142+
}
143+
127144
public function getTotpSecret(): ?string
128145
{
129146
return $this->totpSecret;
@@ -144,11 +161,6 @@ public function getSalt(): ?string
144161
return null;
145162
}
146163

147-
public function eraseCredentials(): void
148-
{
149-
$this->plainPassword = null;
150-
}
151-
152164
public function isAdmin(): bool
153165
{
154166
return $this->role->isAdmin();

src/Doctrine/EventListener/UserListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ private function hashPassword(User $user): void
3939
$password = $this->passwordHasher->hashPassword($user, $user->getPlainPassword());
4040
$user->setPassword($password);
4141

42-
$user->eraseCredentials();
42+
$user->clearPlainPassword();
4343
}
4444
}

0 commit comments

Comments
 (0)