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