Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save soyuka/d8d08ca2121b24357de1fdf38b0a8af8 to your computer and use it in GitHub Desktop.
Save soyuka/d8d08ca2121b24357de1fdf38b0a8af8 to your computer and use it in GitHub Desktop.
JWT api platform
lexik_jwt_authentication:
private_key_path: '%kernel.root_dir%/../var/jwt/private.pem' # ssh private key path
public_key_path: '%kernel.root_dir%/../var/jwt/public.pem' # ssh public key path
pass_phrase: '1234' # ssh key pass phrase
token_ttl: null # token ttl - defaults to 86400 1 day
token_extractors:
authorization_header:
enabled: true
prefix: Bearer
query_parameter:
enabled: true
name: bearer
user_routing:
resource: "@CoreUserBundle/Resources/config/routing.yml"
api:
resource: '.'
type: 'api_platform'
prefix: /api
security:
encoders:
Core\UserBundle\Entity\User:
algorithm: bcrypt
providers:
entity_provider:
entity:
class: CoreUserBundle:User
firewalls:
dev:
pattern: ^/(_(profiler|wdt|error)|css|images|js)/
security: false
main:
anonymous: ~
json_login:
check_path: login_check
username_path: 'username'
password_path: 'password'
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
api:
pattern: ^/api
stateless: true
anonymous: false
provider: entity_provider
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
<?php
namespace Core\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class SecurityController extends Controller
{
public function loginAction(Request $request)
{
}
}
<?php
namespace Core\UserBundle\EventListeners;
use Core\UserBundle\Entity\User;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* JWTResponseListener.
*
* @author Antoine Bluchet <abluchet@ds-restauration.com>
*/
class JWTResponseListener
{
/** @var Symfony\Bridge\Doctrine\RegistryInterface * */
private $doctrine;
public function __construct(RegistryInterface $doctrine)
{
$this->doctrine = $doctrine;
}
/**
* Add public data to the authentication response.
*
* @param AuthenticationSuccessEvent $event
*/
public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
{
$data = $event->getData();
$user = $event->getUser();
if (!$user instanceof UserInterface) {
return;
}
$userRepository = $this->doctrine->getRepository(User::class);
$userRepository->invalidate($user->getUsername());
$event->setData($userRepository->toLoginObject($user, $data));
}
public function onAuthenticationFailure(AuthenticationFailureEvent $event)
{
$exception = $event->getException();
$response = $event->getResponse();
if ($exception) {
$response->setContent($exception->getMessage());
} else {
$response->setContent('Invalid credentials');
}
$response->setStatusCode(401);
}
}
services:
_defaults:
autowire: true
public: false
Core\UserBundle\EventListeners\UserPasswordEncoderListener:
tags:
- { name: doctrine.orm.entity_listener }
Core\UserBundle\EventListeners\JWTResponseListener:
tags:
- { name: kernel.event_listener, event: lexik_jwt_authentication.on_authentication_success, method: onAuthenticationSuccessResponse }
- { name: kernel.event_listener, event: lexik_jwt_authentication.on_authentication_failure, method: onAuthenticationFailure }
Core\UserBundle\Command\ChangeUserPasswordCommand:
public: false
tags: [ { name: 'console.command'} ]
Core\UserBundle\Command\CreateUserCommand:
public: false
tags: [ { name: 'console.command'} ]
Core\UserBundle\Action\ReloadAction:
public: true
login_check:
path: /login_check
@hounded
Copy link

hounded commented Jul 15, 2018

Hey Man do you have all these files incl user password changes etc somewhere that I could look at and learn?

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