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