Skip to content

Instantly share code, notes, and snippets.

@alborq
Last active July 21, 2020 11:14
Show Gist options
  • Save alborq/9ec969cdbb1f697d0b11a7a0eb3734bb to your computer and use it in GitHub Desktop.
Save alborq/9ec969cdbb1f697d0b11a7a0eb3734bb to your computer and use it in GitHub Desktop.
API-Platform Use JWT Token to auth, Add JWT in documentation | Api-platofrm, JWT, PHP, Documentation, Security, Swagger
<?php
namespace AppBundle\Documentation;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Class DocumentationNormalizer
* @package AppBundle\Documentation
*/
final class DocumentationNormalizer implements NormalizerInterface
{
/**
* @var NormalizerInterface
*/
private $normalizerDeferred;
public function __construct(NormalizerInterface $normalizerDeferred)
{
$this->normalizerDeferred = $normalizerDeferred;
}
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = [])
{
$allowedFormat = ['application/ld+json', 'application/json', 'text/html'];
$TokenDocumentation =[
'paths' => [
'/token' => [
'post' => [
'tags' => ['Token'],
'operationId' => 'postTokenItem',
'consumes' => $allowedFormat,
'produces' => $allowedFormat,
'summary' => 'Get JWT token to login.',
'parameters' => [
[
'in' => 'formData',
'name' => 'email',
'description' => 'Your Login Username Or Email',
'required' => true,
'type' => 'string'
],
[
'in' => 'formData',
'name' => 'password',
'description' => 'Your password',
'required' => true,
'type' => 'string'
]
],
'responses' => [
200 => [
'description' => 'give JWT token',
'schema' => [
'$ref' => '#/definitions/Token'
]
],
401 => [
'description' => 'Bad credentials'
]
],
]
]
],
'definitions' => [
'Token' => [
'type' => 'object',
'description' => "",
'properties' => [
'token' => [
'type' => 'string',
'readOnly' => true,
]
]
]
]
];
$officialDocumentation = $this->normalizerDeferred->normalize($object, $format, $context);
return array_merge_recursive($officialDocumentation, $TokenDocumentation);
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $this->normalizerDeferred->supportsNormalization($data, $format);
}
}
# app/config/security.yml
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_READER: ROLE_USER
ROLE_ADMIN: ROLE_READER
providers:
fos_userbundle:
id: fos_user.user_provider.username_email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
utils:
pattern: ^/(utils|login)
form_login:
provider: fos_userbundle
default_target_path: api_doc
logout: true
anonymous: true
api_login:
pattern: ^/token
stateless: true
anonymous: true
provider: fos_userbundle
form_login:
check_path: /token
username_parameter: email
password_parameter: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
require_previous_session: false
api:
pattern: ^/
provider: fos_userbundle
stateless: true
anonymous: true
lexik_jwt:
query_parameter:
enabled: true
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/token, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: IS_AUTHENTICATED_FULLY }
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
lexik_jwt_authentication:
private_key_path: '%jwt_private_key_path%'
public_key_path: '%jwt_public_key_path%'
pass_phrase: '%jwt_key_pass_phrase%'
token_ttl: '%jwt_token_ttl%'
@baudev
Copy link

baudev commented Jan 2, 2019

By using exactly your code, Swagger's IU generates a bad JSON. Indeed, the { } braces are missing at the beginning and end of the sent parameters.
I had to modify the definition of the parameters as follows:

'summary'=> 'Get JWT token to login.',
'parameters' => [
    [
        'in' => 'body',
        'name' => 'user',
        'schema' => [
            'type' => 'object',
            'description' => "Username and password of the user",
            'properties' => [
                'username' => [
                    'type' => 'string',
                    'example' => 'username'
                ],
                'password' => [
                    'type' => 'string',
                    'example' => 'password'
                ]
            ]
        ]
    ]
],

Thank you for your gist by the way 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment