src/OpenApi/JwtDecorator.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\OpenApi;
  4. use ApiPlatform\Core\OpenApi\Model;
  5. use ApiPlatform\Core\OpenApi\OpenApi;
  6. use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. final class JwtDecorator implements OpenApiFactoryInterface
  9. {
  10.     private string $apiRoutePrefix;
  11.     public function __construct(
  12.         private OpenApiFactoryInterface $decorated,
  13.         private ParameterBagInterface $parameterBag,
  14.     ) {
  15.         $this->apiRoutePrefix $this->parameterBag->get('api.route.prefix');
  16.     }
  17.     public function __invoke(array $context = []): OpenApi
  18.     {
  19.         $openApi = ($this->decorated)($context);
  20.         $schemas $openApi->getComponents()->getSchemas();
  21.         $schemas['Token'] = new \ArrayObject([
  22.             'type' => 'object',
  23.             'properties' => [
  24.                 'token' => [
  25.                     'type' => 'string',
  26.                     'readOnly' => true,
  27.                 ],
  28.                 'refresh_token' => [
  29.                     'type' => 'string',
  30.                     'readOnly' => true,
  31.                 ],
  32.             ],
  33.         ]);
  34.         $schemas['Credentials'] = new \ArrayObject([
  35.             'type' => 'object',
  36.             'properties' => [
  37.                 'username' => [
  38.                     'type' => 'string',
  39.                     'example' => 'johndoe@example.com',
  40.                 ],
  41.                 'password' => [
  42.                     'type' => 'string',
  43.                     'example' => 'password',
  44.                 ],
  45.                 'franchiseId' => [
  46.                     'type' => 'integer',
  47.                     'example' => 0,
  48.                 ],
  49.             ],
  50.         ]);
  51.         $pathItem = new Model\PathItem(
  52.             ref'JWT Token',
  53.             post: new Model\Operation(
  54.                 operationId'postCredentialsItem',
  55.                 tags: ['Token'],
  56.                 responses: [
  57.                     '200' => [
  58.                         'description' => 'Get JWT token',
  59.                         'content' => [
  60.                             'application/json' => [
  61.                                 'schema' => [
  62.                                     '$ref' => '#/components/schemas/Token',
  63.                                 ],
  64.                             ],
  65.                         ],
  66.                     ],
  67.                 ],
  68.                 summary'Get JWT token to login.',
  69.                 requestBody: new Model\RequestBody(
  70.                     description'Generate new JWT Token',
  71.                     content: new \ArrayObject([
  72.                         'application/json' => [
  73.                             'schema' => [
  74.                                 '$ref' => '#/components/schemas/Credentials',
  75.                             ],
  76.                         ],
  77.                     ]),
  78.                 ),
  79.             ),
  80.         );
  81.         $openApi->getPaths()->addPath($this->apiRoutePrefix '/login'$pathItem);
  82.         $schemas['RefreshCredentials'] = new \ArrayObject([
  83.             'type' => 'object',
  84.             'properties' => [
  85.                 'refresh_token' => [
  86.                     'type' => 'string',
  87.                     'example' => 'refresh_token',
  88.                 ],
  89.                 'franchiseId' => [
  90.                     'type' => 'integer',
  91.                     'example' => 0,
  92.                 ],
  93.             ],
  94.         ]);
  95.         $refreshPathItem = new Model\PathItem(
  96.             ref'JWT Token Refresh',
  97.             post: new Model\Operation(
  98.                 operationId'postRefreshTokenItem',
  99.                 tags: ['Token'],
  100.                 responses: [
  101.                     '200' => [
  102.                         'description' => 'Get refreshed JWT token',
  103.                         'content' => [
  104.                             'application/json' => [
  105.                                 'schema' => [
  106.                                     '$ref' => '#/components/schemas/Token',
  107.                                 ],
  108.                             ],
  109.                         ],
  110.                     ],
  111.                 ],
  112.                 summary'Get refreshed JWT token using a refresh token.',
  113.                 requestBody: new Model\RequestBody(
  114.                     description'Refresh JWT Token',
  115.                     content: new \ArrayObject([
  116.                         'application/json' => [
  117.                             'schema' => [
  118.                                 '$ref' => '#/components/schemas/RefreshCredentials',
  119.                             ],
  120.                         ],
  121.                     ]),
  122.                 ),
  123.             ),
  124.         );
  125.         $openApi->getPaths()->addPath($this->apiRoutePrefix '/token/refresh'$refreshPathItem);
  126.         return $openApi;
  127.     }
  128. }