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\Stdlib\Request\Parser; |
12
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\ResponseFactory; |
13
|
|
|
|
14
|
|
|
final class PasswordConfirmationMiddleware implements MiddlewareInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var PasswordManagerInterface |
18
|
|
|
*/ |
19
|
|
|
private $passwordManager; |
20
|
|
|
|
21
|
10 |
|
public function __construct(PasswordManagerInterface $passwordManager) |
22
|
|
|
{ |
23
|
10 |
|
$this->passwordManager = $passwordManager; |
24
|
10 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
10 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
30
|
|
|
{ |
31
|
10 |
|
$data = Parser::getRequestBody($request); |
32
|
10 |
|
$password = $data['password'] ?? null; |
33
|
|
|
|
34
|
10 |
|
if (empty($password)) { |
35
|
2 |
|
$msg = 'The action requires password confirmation. No password provided.'; |
36
|
2 |
|
return (new ResponseFactory())($msg, 412); |
37
|
|
|
} else { |
38
|
8 |
|
unset($data['password']); |
39
|
|
|
} |
40
|
|
|
|
41
|
8 |
|
$user = $request->getAttribute(TokenMiddleware::USER_PARAM); |
42
|
8 |
|
if (!$this->passwordManager->isValidForUser((string)$password, $user)) { |
43
|
2 |
|
return (new ResponseFactory())('Invalid password provided.', 412); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// serve down the pipe without password data |
47
|
6 |
|
return $handler->handle($request->withParsedBody($data)); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|