src/OpenApi/JwtDecorator.php line 18

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\OpenApi;
  4. use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
  5. use ApiPlatform\Core\OpenApi\OpenApi;
  6. use ApiPlatform\Core\OpenApi\Model;
  7. final class JwtDecorator implements OpenApiFactoryInterface
  8. {
  9.     public function __construct(
  10.         private OpenApiFactoryInterface $decorated
  11.     ) {}
  12.     public function __invoke(array $context = []): OpenApi
  13.     {
  14.         $openApi = ($this->decorated)($context);
  15.         $schemas $openApi->getComponents()->getSchemas();
  16.         $schemas['Token'] = new \ArrayObject([
  17.             'type' => 'object',
  18.             'properties' => [
  19.                 'token' => [
  20.                     'type' => 'string',
  21.                     'readOnly' => true,
  22.                 ],
  23.             ],
  24.         ]);
  25.         $schemas['Credentials'] = new \ArrayObject([
  26.             'type' => 'object',
  27.             'properties' => [
  28.                 'email' => [
  29.                     'type' => 'string',
  30.                     'example' => 'johndoe@example.com',
  31.                 ],
  32.                 'password' => [
  33.                     'type' => 'string',
  34.                     'example' => 'apassword',
  35.                 ],
  36.             ],
  37.         ]);
  38.         $schemas $openApi->getComponents()->getSecuritySchemes() ?? [];
  39.         $schemas['JWT'] = new \ArrayObject([
  40.             'type' => 'http',
  41.             'scheme' => 'bearer',
  42.             'bearerFormat' => 'JWT',
  43.         ]);
  44.         
  45.         $pathItem = new Model\PathItem(
  46.             ref'JWT Token',
  47.             post: new Model\Operation(
  48.                 operationId'postCredentialsItem',
  49.                 tags: ['Authentication'],
  50.                 responses: [
  51.                     '200' => [
  52.                         'description' => 'Get JWT token',
  53.                         'content' => [
  54.                             'application/json' => [
  55.                                 'schema' => [
  56.                                     '$ref' => '#/components/schemas/Token',
  57.                                 ],
  58.                             ],
  59.                         ],
  60.                     ],
  61.                 ],
  62.                 summary'Get JWT token to login.',
  63.                 requestBody: new Model\RequestBody(
  64.                     description'Generate new JWT Token',
  65.                     content: new \ArrayObject([
  66.                         'application/json' => [
  67.                             'schema' => [
  68.                                 '$ref' => '#/components/schemas/Credentials',
  69.                             ],
  70.                         ],
  71.                     ]),
  72.                 ),
  73.                 security: [],
  74.             ),
  75.         );
  76.         $openApi->getPaths()->addPath('/api/authentication_token'$pathItem);
  77.         return $openApi;
  78.     }
  79. }