Skip to content

Commit 4a4b54b

Browse files
committed
docs: minor tweaks to jwt.md
1 parent ee21499 commit 4a4b54b

File tree

1 file changed

+43
-51
lines changed

1 file changed

+43
-51
lines changed

core/jwt.md

Lines changed: 43 additions & 51 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,35 +191,32 @@ 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
205204
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
206205
use ApiPlatform\Core\OpenApi\OpenApi;
207206
use ApiPlatform\Core\OpenApi\Model;
208-
use ArrayObject;
209-
use Symfony\Component\HttpFoundation\Response;
210207
211-
final class JWTSwaggerDecorator implements OpenApiFactoryInterface
208+
final class JwtDecorator implements OpenApiFactoryInterface
212209
{
213-
private OpenApiFactoryInterface $decorated;
214-
215-
public function __construct(OpenApiFactoryInterface $decorated)
216-
{
217-
$this->decorated = $decorated;
218-
}
210+
public function __construct(
211+
private OpenApiFactoryInterface $decorated
212+
) {}
219213
220214
public function __invoke(array $context = []): OpenApi
221215
{
222-
$openApi = $this->decorated->__invoke($context);
216+
$openApi = ($this->decorated)($context);
217+
$schemas = $openApi->getComponents()->getSchemas();
223218
224-
$openApi->getComponents()->getSchemas()['Token'] = new ArrayObject([
219+
$schemas['Token'] = new ArrayObject([
225220
'type' => 'object',
226221
'properties' => [
227222
'token' => [
@@ -230,51 +225,48 @@ final class JWTSwaggerDecorator implements OpenApiFactoryInterface
230225
],
231226
],
232227
]);
233-
234-
$openApi->getComponents()->getSchemas()['Credentials'] = new ArrayObject([
235-
'type' => 'object',
236-
'properties' => [
237-
'email' => [
238-
'type' => 'string',
239-
'example' => '[email protected]',
240-
],
241-
'password' => [
242-
'type' => 'string',
243-
'example' => 'apassword',
244-
],
228+
$schemas['Credentials'] = new ArrayObject([
229+
'type' => 'object',
230+
'properties' => [
231+
'email' => [
232+
'type' => 'string',
233+
'example' => '[email protected]',
245234
],
246-
]
247-
);
235+
'password' => [
236+
'type' => 'string',
237+
'example' => 'apassword',
238+
],
239+
],
240+
]);
248241
249242
$pathItem = new Model\PathItem(
250243
ref: 'JWT Token',
251244
post: new Model\Operation(
252245
operationId: 'postCredentialsItem',
253246
responses: [
254-
Response::HTTP_OK => [
255-
'description' => 'Get JWT token',
256-
'content' => [
257-
'application/json' => [
258-
'schema' => [
259-
'$ref' => '#/components/schemas/Token',
247+
'200' => [
248+
'description' => 'Get JWT token',
249+
'content' => [
250+
'application/json' => [
251+
'schema' => [
252+
'$ref' => '#/components/schemas/Token',
253+
],
260254
],
261255
],
262256
],
263-
]],
257+
],
264258
summary: 'Get JWT token to login.',
265259
requestBody: new Model\RequestBody(
266-
description: 'Generate new JWT Token',
267-
content: new ArrayObject(
268-
[
269-
'application/json' => [
270-
'schema' => [
271-
'$ref' => '#/components/schemas/Credentials',
260+
description: 'Generate new JWT Token',
261+
content: new ArrayObject([
262+
'application/json' => [
263+
'schema' => [
264+
'$ref' => '#/components/schemas/Credentials',
265+
],
272266
],
273-
],
274-
]),
267+
]),
268+
),
275269
),
276-
277-
)
278270
);
279271
$openApi->getPaths()->addPath('/authentication_token', $pathItem);
280272
@@ -286,12 +278,12 @@ final class JWTSwaggerDecorator implements OpenApiFactoryInterface
286278
And register this service in `config/services.yaml`:
287279

288280
```yaml
281+
# api/config/services.yaml
289282
services:
290283
# ...
291284
292-
App\Swagger\JWTSwaggerDecorator:
285+
App\OpenApi\JwtDecorator:
293286
decorates: 'api_platform.openapi.factory'
294-
arguments: ['@App\Swagger\JWTSwaggerDecorator.inner']
295287
autoconfigure: false
296288
```
297289

0 commit comments

Comments
 (0)