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\Entities\Token; |
12
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\Middleware\ResourceMiddlewareInterface; |
13
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry; |
14
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralErrorResponseFactory; |
15
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory; |
16
|
|
|
use Zend\Hydrator\HydratorInterface; |
17
|
|
|
|
18
|
|
|
final class InvalidateTokenAction implements MiddlewareInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var LoggerInterface |
22
|
|
|
*/ |
23
|
|
|
private $logger; |
24
|
|
|
/** |
25
|
|
|
* @var HydratorInterface |
26
|
|
|
*/ |
27
|
|
|
private $hydrator; |
28
|
|
|
/** |
29
|
|
|
* @var EntityManagerRegistry |
30
|
|
|
*/ |
31
|
|
|
private $managerRegistry; |
32
|
|
|
|
33
|
2 |
|
public function __construct( |
34
|
|
|
EntityManagerRegistry $managerRegistry, |
35
|
|
|
LoggerInterface $logger, |
36
|
|
|
HydratorInterface $hydrator |
37
|
|
|
) { |
38
|
2 |
|
$this->managerRegistry = $managerRegistry; |
39
|
2 |
|
$this->logger = $logger; |
40
|
2 |
|
$this->hydrator = $hydrator; |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
2 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
47
|
|
|
{ |
48
|
|
|
/** @var Token $token */ |
49
|
2 |
|
$token = $request->getAttribute(ResourceMiddlewareInterface::DATA_RESOURCE); |
50
|
2 |
|
$token->setActive(false); |
51
|
|
|
|
52
|
|
|
try { |
53
|
2 |
|
$em = $this->managerRegistry->getManagerForClass(get_class($token)); |
54
|
2 |
|
$em->persist($token); |
55
|
2 |
|
$em->flush(); |
56
|
2 |
|
$msg = 'Token invalidated.'; |
57
|
2 |
|
return (new GeneralSuccessResponseFactory())($msg, 'token', $this->hydrator->extract($token)); |
58
|
|
|
} catch (\Exception $exception) { |
59
|
|
|
$this->logger->error((string)$exception); |
60
|
|
|
$msg = 'There was an error during token invalidation'; |
61
|
|
|
return (new GeneralErrorResponseFactory())($msg, 'token'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|