Skip to content

Commit 733c8fb

Browse files
garciasdosdunglas
andauthored
Update JWT Documentation with the OpenApi way (#1240)
* Update JWT Documentation Update with the new OpenApi documentation way. It works with Api Platform 2.6.0 beta 1 * docs: minor tweaks to jwt.md Co-authored-by: Kévin Dunglas <[email protected]>
1 parent 04de0a2 commit 733c8fb

File tree

1 file changed

+53
-64
lines changed

1 file changed

+53
-64
lines changed

core/jwt.md

Lines changed: 53 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ If your API uses a [path prefix](https://symfony.com/doc/current/routing/externa
118118
security:
119119
encoders:
120120
App\Entity\User:
121-
algorithm: argon2i
121+
algorithm: auto
122122
123123
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
124124
providers:
@@ -150,7 +150,7 @@ security:
150150
failure_handler: lexik_jwt_authentication.handler.authentication_failure
151151
152152
access_control:
153-
- { path: ^/api/docs, roles: IS_AUTHENTICATED_ANONYMOUSLY } # Allows accessing the Swagger UI
153+
- { path: ^/docs, roles: IS_AUTHENTICATED_ANONYMOUSLY } # Allows accessing API documentations and Swagger UI
154154
- { path: ^/authentication_token, roles: IS_AUTHENTICATED_ANONYMOUSLY }
155155
- { path: ^/, roles: IS_AUTHENTICATED_FULLY }
156156
```
@@ -181,9 +181,7 @@ All you have to do is configure the API key in the `value` field.
181181
By default, [only the authorization header mode is enabled](https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#2-use-the-token) in LexikJWTAuthenticationBundle.
182182
You must set the [JWT token](https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#1-obtain-the-token) as below and click on the "Authorize" button.
183183

184-
```
185-
Bearer MY_NEW_TOKEN
186-
```
184+
Bearer MY_NEW_TOKEN
187185

188186
![Screenshot of API Platform with the configuration API Key](images/JWTConfigureApiKey.png)
189187

@@ -193,108 +191,99 @@ We can add a `POST /authentication_token` endpoint to SwaggerUI to conveniently
193191

194192
![API Endpoint to retrieve JWT Token from SwaggerUI](images/jwt-token-swagger-ui.png)
195193

196-
To do it, we need to create a `SwaggerDecorator`:
194+
To do it, we need to create a decorator:
197195

198196
```php
199197
<?php
198+
// api/src/OpenApi/JwtDecorator.php
200199
201200
declare(strict_types=1);
202201
203-
namespace App\Swagger;
202+
namespace App\OpenApi;
204203
205-
use Symfony\Component\HttpFoundation\Response;
206-
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
204+
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
205+
use ApiPlatform\Core\OpenApi\OpenApi;
206+
use ApiPlatform\Core\OpenApi\Model;
207207
208-
final class SwaggerDecorator implements NormalizerInterface
208+
final class JwtDecorator implements OpenApiFactoryInterface
209209
{
210-
private NormalizerInterface $decorated;
211-
212-
public function __construct(NormalizerInterface $decorated)
213-
{
214-
$this->decorated = $decorated;
215-
}
216-
217-
public function supportsNormalization($data, string $format = null): bool
218-
{
219-
return $this->decorated->supportsNormalization($data, $format);
220-
}
210+
public function __construct(
211+
private OpenApiFactoryInterface $decorated
212+
) {}
221213
222-
public function normalize($object, string $format = null, array $context = [])
214+
public function __invoke(array $context = []): OpenApi
223215
{
224-
$docs = $this->decorated->normalize($object, $format, $context);
216+
$openApi = ($this->decorated)($context);
217+
$schemas = $openApi->getComponents()->getSchemas();
225218
226-
$docs['components']['schemas']['Token'] = [
219+
$schemas['Token'] = new ArrayObject([
227220
'type' => 'object',
228221
'properties' => [
229222
'token' => [
230223
'type' => 'string',
231224
'readOnly' => true,
232225
],
233226
],
234-
];
235-
236-
$docs['components']['schemas']['Credentials'] = [
227+
]);
228+
$schemas['Credentials'] = new ArrayObject([
237229
'type' => 'object',
238230
'properties' => [
239-
'username' => [
231+
'email' => [
240232
'type' => 'string',
241-
'example' => 'api',
233+
'example' => '[email protected]',
242234
],
243235
'password' => [
244236
'type' => 'string',
245-
'example' => 'api',
237+
'example' => 'apassword',
246238
],
247239
],
248-
];
249-
250-
$tokenDocumentation = [
251-
'paths' => [
252-
'/authentication_token' => [
253-
'post' => [
254-
'tags' => ['Token'],
255-
'operationId' => 'postCredentialsItem',
256-
'summary' => 'Get JWT token to login.',
257-
'requestBody' => [
258-
'description' => 'Create new JWT Token',
259-
'content' => [
260-
'application/json' => [
261-
'schema' => [
262-
'$ref' => '#/components/schemas/Credentials',
263-
],
264-
],
265-
],
266-
],
267-
'responses' => [
268-
Response::HTTP_OK => [
269-
'description' => 'Get JWT token',
270-
'content' => [
271-
'application/json' => [
272-
'schema' => [
273-
'$ref' => '#/components/schemas/Token',
274-
],
275-
],
240+
]);
241+
242+
$pathItem = new Model\PathItem(
243+
ref: 'JWT Token',
244+
post: new Model\Operation(
245+
operationId: 'postCredentialsItem',
246+
responses: [
247+
'200' => [
248+
'description' => 'Get JWT token',
249+
'content' => [
250+
'application/json' => [
251+
'schema' => [
252+
'$ref' => '#/components/schemas/Token',
276253
],
277254
],
278255
],
279256
],
280257
],
281-
],
282-
];
258+
summary: 'Get JWT token to login.',
259+
requestBody: new Model\RequestBody(
260+
description: 'Generate new JWT Token',
261+
content: new ArrayObject([
262+
'application/json' => [
263+
'schema' => [
264+
'$ref' => '#/components/schemas/Credentials',
265+
],
266+
],
267+
]),
268+
),
269+
),
270+
);
271+
$openApi->getPaths()->addPath('/authentication_token', $pathItem);
283272
284-
return array_merge_recursive($docs, $tokenDocumentation);
273+
return $openApi;
285274
}
286275
}
287276
```
288277

289278
And register this service in `config/services.yaml`:
290279

291280
```yaml
281+
# api/config/services.yaml
292282
services:
293283
# ...
294284
295-
App\Swagger\SwaggerDecorator:
296-
decorates: 'api_platform.swagger.normalizer.documentation'
297-
arguments: ['@App\Swagger\SwaggerDecorator.inner']
285+
App\OpenApi\JwtDecorator:
286+
decorates: 'api_platform.openapi.factory'
298287
autoconfigure: false
299288
```
300289

0 commit comments

Comments
 (0)