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\Notification\DangerMessage; |
14
|
|
|
use SlayerBirden\DataFlowServer\Notification\SuccessMessage; |
15
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\DataValidationResponseFactory; |
16
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory; |
17
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
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
|
4 |
|
public function __construct( |
37
|
|
|
TokenManagerInterface $tokenManager, |
38
|
|
|
ExtractionInterface $extraction, |
39
|
|
|
InputFilterInterface $inputFilter |
40
|
|
|
) { |
41
|
4 |
|
$this->tokenManager = $tokenManager; |
42
|
4 |
|
$this->extraction = $extraction; |
43
|
4 |
|
$this->inputFilter = $inputFilter; |
44
|
4 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
4 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
50
|
|
|
{ |
51
|
4 |
|
$data = $request->getParsedBody(); |
52
|
4 |
|
if (!is_array($data)) { |
53
|
|
|
return (new DataValidationResponseFactory())('token'); |
54
|
|
|
} |
55
|
4 |
|
$this->inputFilter->setData($data); |
56
|
|
|
|
57
|
4 |
|
if (!$this->inputFilter->isValid()) { |
58
|
1 |
|
return (new ValidationResponseFactory())('token', $this->inputFilter); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
try { |
62
|
3 |
|
$token = $this->tokenManager->getToken($data['user'], $data['password'], $data['resources']); |
63
|
1 |
|
return new JsonResponse([ |
64
|
1 |
|
'data' => [ |
65
|
1 |
|
'token' => $this->extraction->extract($token), |
66
|
|
|
'validation' => [], |
67
|
|
|
], |
68
|
|
|
'success' => true, |
69
|
1 |
|
'msg' => new SuccessMessage('Token successfully creaeted'), |
70
|
1 |
|
], 200); |
71
|
2 |
|
} catch (InvalidCredentialsException $exception) { |
72
|
1 |
|
return new JsonResponse([ |
73
|
1 |
|
'data' => [ |
74
|
|
|
'token' => null, |
75
|
|
|
'validation' => [], |
76
|
|
|
], |
77
|
|
|
'success' => false, |
78
|
1 |
|
'msg' => new DangerMessage( |
79
|
1 |
|
'Invalid credentials provided. Please double check your user and password.' |
80
|
|
|
), |
81
|
1 |
|
], 401); |
82
|
1 |
|
} catch (PermissionDeniedException $exception) { |
83
|
1 |
|
return new JsonResponse([ |
84
|
1 |
|
'data' => [ |
85
|
|
|
'token' => null, |
86
|
|
|
'validation' => [], |
87
|
|
|
], |
88
|
|
|
'success' => false, |
89
|
1 |
|
'msg' => new DangerMessage( |
90
|
1 |
|
'Provided user does not have permission to access requested resources.' |
91
|
|
|
), |
92
|
1 |
|
], 403); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|