|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Damax\Bundle\ApiAuthBundle\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\TokenBuilder; |
|
8
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\ResponseFactory; |
|
9
|
|
|
use Swagger\Annotations as OpenApi; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; |
|
12
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
13
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
15
|
|
|
|
|
16
|
|
|
class TokenController |
|
17
|
|
|
{ |
|
18
|
|
|
private $securityTokenStorage; |
|
19
|
|
|
private $responseFactory; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(TokenStorageInterface $securityTokenStorage, ResponseFactory $responseFactory) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->securityTokenStorage = $securityTokenStorage; |
|
24
|
|
|
$this->responseFactory = $responseFactory; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @OpenApi\Post( |
|
29
|
|
|
* tags={"security"}, |
|
30
|
|
|
* summary="Refresh token.", |
|
31
|
|
|
* security={ |
|
32
|
|
|
* {"Bearer"=""} |
|
33
|
|
|
* }, |
|
34
|
|
|
* @OpenApi\Response( |
|
35
|
|
|
* response=200, |
|
36
|
|
|
* description="Authentication result.", |
|
37
|
|
|
* @OpenApi\Schema(ref="#/definitions/SecurityLoginResult") |
|
38
|
|
|
* ), |
|
39
|
|
|
* @OpenApi\Response( |
|
40
|
|
|
* response=401, |
|
41
|
|
|
* description="Bad credentials." |
|
42
|
|
|
* ) |
|
43
|
|
|
* ) |
|
44
|
|
|
* |
|
45
|
|
|
* @Route("/refresh-token", methods={"POST"}) |
|
46
|
|
|
* |
|
47
|
|
|
* @throws UnauthorizedHttpException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function refreshAction(TokenBuilder $tokenBuilder): Response |
|
50
|
|
|
{ |
|
51
|
|
|
$user = $this->securityTokenStorage->getToken()->getUser(); |
|
52
|
|
|
|
|
53
|
|
|
if (!$user instanceof UserInterface) { |
|
54
|
|
|
throw new UnauthorizedHttpException('Bearer'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$jwtString = $tokenBuilder->fromUser($user); |
|
58
|
|
|
|
|
59
|
|
|
return $this->responseFactory->fromToken($jwtString); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|