1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Authentication\Middleware; |
5
|
|
|
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
9
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
10
|
|
|
use SlayerBirden\DataFlowServer\Authentication\PasswordManagerInterface; |
11
|
|
|
use SlayerBirden\DataFlowServer\Notification\DangerMessage; |
12
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
13
|
|
|
|
14
|
|
|
class PasswordConfirmationMiddleware implements MiddlewareInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var PasswordManagerInterface |
18
|
|
|
*/ |
19
|
|
|
private $passwordManager; |
20
|
|
|
|
21
|
5 |
|
public function __construct(PasswordManagerInterface $passwordManager) |
22
|
|
|
{ |
23
|
5 |
|
$this->passwordManager = $passwordManager; |
24
|
5 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
5 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
30
|
|
|
{ |
31
|
5 |
|
$data = $request->getParsedBody(); |
32
|
|
|
|
33
|
5 |
|
$password = $data['password'] ?? null; |
34
|
|
|
|
35
|
5 |
|
if (empty($password)) { |
36
|
1 |
|
return new JsonResponse([ |
37
|
1 |
|
'data' => [], |
38
|
|
|
'success' => false, |
39
|
1 |
|
'msg' => new DangerMessage('The action requires password confirmation. No password provided.'), |
40
|
1 |
|
], 412); |
41
|
|
|
} else { |
42
|
4 |
|
unset($data['password']); |
43
|
|
|
} |
44
|
|
|
|
45
|
4 |
|
$user = $request->getAttribute(TokenMiddleware::USER_PARAM); |
46
|
4 |
|
if (!$this->passwordManager->isValidForUser((string)$password, $user)) { |
47
|
1 |
|
return new JsonResponse([ |
48
|
1 |
|
'data' => [], |
49
|
|
|
'success' => false, |
50
|
1 |
|
'msg' => new DangerMessage('Invalid password provided.'), |
51
|
1 |
|
], 412); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// serve down the pipe without password data |
55
|
3 |
|
return $handler->handle($request->withParsedBody($data)); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|