1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Authentication\Controller; |
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\Exception\InvalidCredentialsException; |
11
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Exception\PermissionDeniedException; |
12
|
|
|
use SlayerBirden\DataFlowServer\Authentication\TokenManagerInterface; |
13
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\DataValidationResponseFactory; |
14
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralErrorResponseFactory; |
15
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory; |
16
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory; |
17
|
|
|
use Zend\Hydrator\ExtractionInterface; |
18
|
|
|
use Zend\InputFilter\InputFilterInterface; |
19
|
|
|
|
20
|
|
|
final class GetTokenAction implements MiddlewareInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var TokenManagerInterface |
24
|
|
|
*/ |
25
|
|
|
private $tokenManager; |
26
|
|
|
/** |
27
|
|
|
* @var ExtractionInterface |
28
|
|
|
*/ |
29
|
|
|
private $extraction; |
30
|
|
|
/** |
31
|
|
|
* @var InputFilterInterface |
32
|
|
|
*/ |
33
|
|
|
private $inputFilter; |
34
|
|
|
|
35
|
4 |
|
public function __construct( |
36
|
|
|
TokenManagerInterface $tokenManager, |
37
|
|
|
ExtractionInterface $extraction, |
38
|
|
|
InputFilterInterface $inputFilter |
39
|
|
|
) { |
40
|
4 |
|
$this->tokenManager = $tokenManager; |
41
|
4 |
|
$this->extraction = $extraction; |
42
|
4 |
|
$this->inputFilter = $inputFilter; |
43
|
4 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
4 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
49
|
|
|
{ |
50
|
4 |
|
$data = $request->getParsedBody(); |
51
|
4 |
|
if (!is_array($data)) { |
52
|
|
|
return (new DataValidationResponseFactory())('token'); |
53
|
|
|
} |
54
|
4 |
|
$this->inputFilter->setData($data); |
55
|
|
|
|
56
|
4 |
|
if (!$this->inputFilter->isValid()) { |
57
|
1 |
|
return (new ValidationResponseFactory())('token', $this->inputFilter); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
try { |
61
|
3 |
|
$token = $this->tokenManager->getToken($data['user'], $data['password'], $data['resources']); |
62
|
1 |
|
$msg = 'Token successfully created'; |
63
|
1 |
|
return (new GeneralSuccessResponseFactory())($msg, 'token', $this->extraction->extract($token)); |
64
|
2 |
|
} catch (InvalidCredentialsException $exception) { |
65
|
1 |
|
$msg = 'Invalid credentials provided. Please double check your user and password.'; |
66
|
1 |
|
return (new GeneralErrorResponseFactory())($msg, 'token', 401); |
67
|
1 |
|
} catch (PermissionDeniedException $exception) { |
68
|
1 |
|
$msg = 'Provided user does not have permission to access requested resources.'; |
69
|
1 |
|
return (new GeneralErrorResponseFactory())($msg, 'token', 403); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|