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\ValidationResponseFactory; |
16
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
17
|
|
|
use Zend\Hydrator\ExtractionInterface; |
18
|
|
|
use Zend\InputFilter\InputFilterInterface; |
19
|
|
|
|
20
|
|
|
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 |
|
$this->inputFilter->setData($data); |
|
|
|
|
52
|
|
|
|
53
|
4 |
|
if (!$this->inputFilter->isValid()) { |
54
|
1 |
|
return (new ValidationResponseFactory())('token', $this->inputFilter); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
try { |
58
|
3 |
|
$token = $this->tokenManager->getToken($data['user'], $data['password'], $data['resources']); |
59
|
1 |
|
return new JsonResponse([ |
60
|
1 |
|
'data' => [ |
61
|
1 |
|
'token' => $this->extraction->extract($token), |
62
|
|
|
'validation' => [], |
63
|
|
|
], |
64
|
|
|
'success' => true, |
65
|
1 |
|
'msg' => new SuccessMessage('Token successfully creaeted'), |
66
|
1 |
|
], 200); |
67
|
2 |
|
} catch (InvalidCredentialsException $exception) { |
68
|
1 |
|
return new JsonResponse([ |
69
|
1 |
|
'data' => [ |
70
|
|
|
'token' => null, |
71
|
|
|
'validation' => [], |
72
|
|
|
], |
73
|
|
|
'success' => false, |
74
|
1 |
|
'msg' => new DangerMessage( |
75
|
1 |
|
'Invalid credentials provided. Please double check your user and password.' |
76
|
|
|
), |
77
|
1 |
|
], 401); |
78
|
1 |
|
} catch (PermissionDeniedException $exception) { |
79
|
1 |
|
return new JsonResponse([ |
80
|
1 |
|
'data' => [ |
81
|
|
|
'token' => null, |
82
|
|
|
'validation' => [], |
83
|
|
|
], |
84
|
|
|
'success' => false, |
85
|
1 |
|
'msg' => new DangerMessage( |
86
|
1 |
|
'Provided user does not have permission to access requested resources.' |
87
|
|
|
), |
88
|
1 |
|
], 403); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.