-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathPasswordResetController.php
More file actions
184 lines (168 loc) · 6.69 KB
/
PasswordResetController.php
File metadata and controls
184 lines (168 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
declare(strict_types=1);
namespace PhpList\RestBundle\Identity\Controller;
use Doctrine\ORM\EntityManagerInterface;
use OpenApi\Attributes as OA;
use PhpList\Core\Domain\Identity\Service\PasswordManager;
use PhpList\Core\Security\Authentication;
use PhpList\RestBundle\Common\Controller\BaseController;
use PhpList\RestBundle\Common\Validator\RequestValidator;
use PhpList\RestBundle\Identity\Request\RequestPasswordResetRequest;
use PhpList\RestBundle\Identity\Request\ResetPasswordRequest;
use PhpList\RestBundle\Identity\Request\ValidateTokenRequest;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
/**
* This controller provides methods to reset admin passwords.
*/
#[Route('/password-reset', name: 'password_reset_')]
class PasswordResetController extends BaseController
{
private PasswordManager $passwordManager;
public function __construct(
Authentication $authentication,
RequestValidator $validator,
PasswordManager $passwordManager,
private readonly EntityManagerInterface $entityManager,
) {
parent::__construct($authentication, $validator);
$this->passwordManager = $passwordManager;
}
#[Route('/request', name: 'request', methods: ['POST'])]
#[OA\Post(
path: '/api/v2/password-reset/request',
description: 'Request a password reset token for an administrator account.',
summary: 'Request a password reset.',
requestBody: new OA\RequestBody(
description: 'Administrator email',
required: true,
content: new OA\JsonContent(
required: ['email'],
properties: [
new OA\Property(property: 'email', type: 'string', format: 'email', example: 'admin@example.com'),
]
)
),
tags: ['password-reset'],
responses: [
new OA\Response(
response: 204,
description: 'Password reset token generated',
),
new OA\Response(
response: 400,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/BadRequestResponse')
),
new OA\Response(
response: 404,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
)
]
)]
public function requestPasswordReset(Request $request): JsonResponse
{
/** @var RequestPasswordResetRequest $resetRequest */
$resetRequest = $this->validator->validate($request, RequestPasswordResetRequest::class);
$this->passwordManager->generatePasswordResetToken($resetRequest->email);
$this->entityManager->flush();
return $this->json(null, Response::HTTP_NO_CONTENT);
}
#[Route('/validate', name: 'validate', methods: ['POST'])]
#[OA\Post(
path: '/api/v2/password-reset/validate',
description: 'Validate a password reset token.',
summary: 'Validate a password reset token.',
requestBody: new OA\RequestBody(
description: 'Password reset token',
required: true,
content: new OA\JsonContent(
required: ['token'],
properties: [
new OA\Property(property: 'token', type: 'string', example: 'a1b2c3d4e5f6'),
]
)
),
tags: ['password-reset'],
responses: [
new OA\Response(
response: 200,
description: 'Success',
content: new OA\JsonContent(
properties: [
new OA\Property(property: 'valid', type: 'boolean', example: true),
]
)
),
new OA\Response(
response: 400,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/BadRequestResponse')
)
]
)]
public function validateToken(Request $request): JsonResponse
{
/** @var ValidateTokenRequest $validateRequest */
$validateRequest = $this->validator->validate($request, ValidateTokenRequest::class);
$administrator = $this->passwordManager->validatePasswordResetToken($validateRequest->token);
$this->entityManager->flush();
return $this->json([ 'valid' => $administrator !== null]);
}
#[Route('/reset', name: 'reset', methods: ['POST'])]
#[OA\Post(
path: '/api/v2/password-reset/reset',
description: 'Reset an administrator password using a token.',
summary: 'Reset password with token.',
requestBody: new OA\RequestBody(
description: 'Password reset information',
required: true,
content: new OA\JsonContent(
required: ['token', 'newPassword'],
properties: [
new OA\Property(property: 'token', type: 'string', example: 'a1b2c3d4e5f6'),
new OA\Property(
property: 'newPassword',
type: 'string',
format: 'password',
example: 'newSecurePassword123',
),
]
)
),
tags: ['password-reset'],
responses: [
new OA\Response(
response: 200,
description: 'Success',
content: new OA\JsonContent(
properties: [
new OA\Property(property: 'message', type: 'string', example: 'Password updated successfully'),
]
)
),
new OA\Response(
response: 400,
description: 'Invalid or expired token',
content: new OA\JsonContent(ref: '#/components/schemas/BadRequestResponse')
)
]
)]
public function resetPassword(Request $request): JsonResponse
{
/** @var ResetPasswordRequest $resetRequest */
$resetRequest = $this->validator->validate($request, ResetPasswordRequest::class);
$success = $this->passwordManager->updatePasswordWithToken(
$resetRequest->token,
$resetRequest->newPassword
);
$this->entityManager->flush();
if ($success) {
return $this->json([ 'message' => 'Password updated successfully']);
}
return $this->json(['message' => 'Invalid or expired token'], Response::HTTP_BAD_REQUEST);
}
}