|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Security; |
|
15
|
|
|
|
|
16
|
|
|
use DateTime; |
|
17
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
18
|
|
|
use Silverback\ApiComponentBundle\Entity\User\User; |
|
19
|
|
|
use Silverback\ApiComponentBundle\Exception\InvalidParameterException; |
|
20
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
22
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
|
23
|
|
|
use Symfony\Component\Mime\Address; |
|
24
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
25
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
26
|
|
|
|
|
27
|
|
|
class PasswordManager |
|
28
|
|
|
{ |
|
29
|
|
|
private MailerInterface $mailer; |
|
30
|
|
|
private EntityManagerInterface $entityManager; |
|
31
|
|
|
private ValidatorInterface $validator; |
|
32
|
|
|
private TokenGenerator $tokenGenerator; |
|
33
|
|
|
private RequestStack $requestStack; |
|
34
|
|
|
private string $websiteEmailAddress; |
|
35
|
|
|
private int $tokenTtl; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
MailerInterface $mailer, |
|
39
|
|
|
EntityManagerInterface $entityManager, |
|
40
|
|
|
ValidatorInterface $validator, |
|
41
|
|
|
TokenGenerator $tokenGenerator, |
|
42
|
|
|
RequestStack $requestStack, |
|
43
|
|
|
string $websiteEmailAddress = '[email protected]', |
|
44
|
|
|
int $tokenTtl = 8600 |
|
45
|
|
|
) { |
|
46
|
|
|
$this->mailer = $mailer; |
|
47
|
|
|
$this->entityManager = $entityManager; |
|
48
|
|
|
$this->validator = $validator; |
|
49
|
|
|
$this->tokenGenerator = $tokenGenerator; |
|
50
|
|
|
$this->requestStack = $requestStack; |
|
51
|
|
|
$this->websiteEmailAddress = $websiteEmailAddress; |
|
52
|
|
|
$this->tokenTtl = $tokenTtl; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function requestResetEmail(User $user, string $resetUrl): void |
|
56
|
|
|
{ |
|
57
|
|
|
if ($user->isPasswordRequestLimitReached($this->tokenTtl)) { |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
$confirmationToken = $user->getPasswordResetConfirmationToken(); |
|
61
|
|
|
if (!$confirmationToken) { |
|
62
|
|
|
throw new InvalidParameterException(sprintf('The entity %s should have a confirmation token set to send a password reset email.', User::class)); |
|
63
|
|
|
} |
|
64
|
|
|
$username = $user->getUsername(); |
|
65
|
|
|
if (!$username) { |
|
66
|
|
|
throw new InvalidParameterException(sprintf('The entity %s should have a username set to send a password reset email.', User::class)); |
|
67
|
|
|
} |
|
68
|
|
|
$user->setPasswordResetConfirmationToken($this->tokenGenerator->generateToken()); |
|
69
|
|
|
$user->setPasswordRequestedAt(new DateTime()); |
|
70
|
|
|
$this->passwordResetEmail($user, $this->pathToAppUrl($resetUrl, $confirmationToken, $username)); |
|
71
|
|
|
$this->entityManager->flush(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function passwordResetEmail(User $user, string $resetUrl): void |
|
75
|
|
|
{ |
|
76
|
|
|
$email = (new TemplatedEmail()) |
|
77
|
|
|
->from(Address::fromString($this->websiteEmailAddress)) |
|
78
|
|
|
->to(Address::fromString($user->getUsername())) |
|
|
|
|
|
|
79
|
|
|
->subject('Your password reset request') |
|
80
|
|
|
// path of the Twig template to render |
|
81
|
|
|
->htmlTemplate('api-component-bundle/emails/forgot_password.html.twig') |
|
82
|
|
|
// pass variables (name => value) to the template |
|
83
|
|
|
->context([ |
|
84
|
|
|
'user' => $user, |
|
85
|
|
|
'reset_url' => $resetUrl, |
|
86
|
|
|
]); |
|
87
|
|
|
$this->mailer->send($email); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function passwordReset(User $user, string $newPassword): void |
|
91
|
|
|
{ |
|
92
|
|
|
$user->setPlainPassword($newPassword); |
|
93
|
|
|
$user->setPasswordResetConfirmationToken(null); |
|
94
|
|
|
$user->setPasswordRequestedAt(null); |
|
95
|
|
|
$errors = $this->validator->validate($user, null, ['password_reset']); |
|
96
|
|
|
if (\count($errors)) { |
|
97
|
|
|
throw new AuthenticationException($errors, 'The password entered is not valid'); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
$this->persistPlainPassword($user); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function persistPlainPassword(User $user): User |
|
103
|
|
|
{ |
|
104
|
|
|
$this->entityManager->persist($user); |
|
105
|
|
|
$this->entityManager->flush(); |
|
106
|
|
|
$user->eraseCredentials(); |
|
107
|
|
|
|
|
108
|
|
|
return $user; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
private function pathToAppUrl(string $path, string $token, string $email): string |
|
112
|
|
|
{ |
|
113
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
|
114
|
|
|
$urlParts = $request ? parse_url($request->headers->get('referer')) : ['scheme' => 'https', 'host' => 'no-referrer']; |
|
115
|
|
|
$path = str_replace(['{{ token }}', '{{ email }}'], [$token, $email], $path); |
|
116
|
|
|
|
|
117
|
|
|
return sprintf( |
|
118
|
|
|
'%s://%s/%s', |
|
119
|
|
|
$urlParts['scheme'], |
|
120
|
|
|
$urlParts['host'], |
|
121
|
|
|
ltrim($path, '/') |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|