Skip to content

Commit

Permalink
Merge pull request #15 from gfreeau/master
Browse files Browse the repository at this point in the history
Added JWT Creator service
  • Loading branch information
slashfan committed Jul 11, 2014
2 parents af5f913 + a065625 commit 2e50b09
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 25 deletions.
56 changes: 56 additions & 0 deletions Event/JWTCreatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php


namespace Lexik\Bundle\JWTAuthenticationBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Security\Core\User\UserInterface;

class JWTCreatedEvent extends Event
{
/**
* @var array
*/
protected $data;

/**
* @var UserInterface
*/
protected $user;

/**
* @param array $data
* @param UserInterface $user
*/
public function __construct(array $data, UserInterface $user)
{
$this->data = $data;
$this->user = $user;
}

/**
* @return array
*/
public function getData()
{
return $this->data;
}

/**
* @param array $data
*/
public function setData(array $data)
{
$this->data = $data;
}

/**
* Get user
*
* @return UserInterface
*/
public function getUser()
{
return $this->user;
}
}
5 changes: 5 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ final class Events
* on the authentication success response
*/
const AUTHENTICATION_SUCCESS = 'lexik_jwt_authentication.on_authentication_success';

/**
* Dispatched token payload is created to allow for extra fields
*/
const JWT_CREATED = 'lexik_jwt_authentication.on_jwt_created';
}
10 changes: 8 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<parameters>
<parameter key="lexik_jwt_authentication.jwt_encoder.class">Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoder</parameter>
<parameter key="lexik_jwt_authentication.jwt_creator.class">Lexik\Bundle\JWTAuthenticationBundle\Services\JWTCreator</parameter>
<parameter key="lexik_jwt_authentication.handler.authentication_success.class">Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationSuccessHandler</parameter>
<parameter key="lexik_jwt_authentication.handler.authentication_failure.class">Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationFailureHandler</parameter>
<parameter key="lexik_jwt_authentication.security.authentication.provider.class">Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Provider\JWTProvider</parameter>
Expand All @@ -22,11 +23,16 @@
<argument>%lexik_jwt_authentication.public_key_path%</argument>
<argument>%lexik_jwt_authentication.pass_phrase%</argument>
</service>
<!-- JWT Authentication response interceptor -->
<service id="lexik_jwt_authentication.handler.authentication_success" class="%lexik_jwt_authentication.handler.authentication_success.class%">
<!-- JWT Creator -->
<service id="lexik_jwt_authentication.jwt_creator" class="%lexik_jwt_authentication.jwt_creator.class%">
<argument type="service" id="lexik_jwt_authentication.jwt_encoder"/>
<argument type="service" id="event_dispatcher"/>
<argument>%lexik_jwt_authentication.token_ttl%</argument>
</service>
<!-- JWT Authentication response interceptor -->
<service id="lexik_jwt_authentication.handler.authentication_success" class="%lexik_jwt_authentication.handler.authentication_success.class%">
<argument type="service" id="lexik_jwt_authentication.jwt_creator"/>
<argument type="service" id="event_dispatcher"/>
<tag name="monolog.logger" channel="security"></tag>
</service>
<service id="lexik_jwt_authentication.handler.authentication_failure" class="%lexik_jwt_authentication.handler.authentication_failure.class%">
Expand Down
25 changes: 7 additions & 18 deletions Security/Http/Authentication/AuthenticationSuccessHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoder;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTCreator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -19,30 +19,23 @@
class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
{
/**
* @var JWTEncoder
* @var JWTCreator
*/
protected $encoder;
protected $jwtCreator;

/**
* @var EventDispatcherInterface
*/
protected $dispatcher;

/**
* @var int
*/
protected $ttl;

/**
* @param JWTEncoder $encoder
* @param JWTCreator $jwtCreator
* @param EventDispatcherInterface $dispatcher
* @param int $ttl
*/
public function __construct(JWTEncoder $encoder, EventDispatcherInterface $dispatcher, $ttl)
public function __construct(JWTCreator $jwtCreator, EventDispatcherInterface $dispatcher)
{
$this->encoder = $encoder;
$this->jwtCreator = $jwtCreator;
$this->dispatcher = $dispatcher;
$this->ttl = $ttl;
}

/**
Expand All @@ -52,11 +45,7 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$user = $token->getUser();

$payload = array();
$payload['exp'] = time() + $this->ttl;
$payload['username'] = $user->getUsername();

$jwt = $this->encoder->encode($payload)->getTokenString();
$jwt = $this->jwtCreator->create($user);

$event = new AuthenticationSuccessEvent(array('token' => $jwt), $user);
$this->dispatcher->dispatch(Events::AUTHENTICATION_SUCCESS, $event);
Expand Down
44 changes: 44 additions & 0 deletions Services/JWTCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Lexik\Bundle\JWTAuthenticationBundle\Services;

use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoder;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use RuntimeException;

class JWTCreator
{
/**
* @param JWTEncoder $encoder
* @param EventDispatcherInterface $dispatcher
* @param int $ttl
*/
public function __construct(JWTEncoder $encoder, EventDispatcherInterface $dispatcher, $ttl)
{
$this->encoder = $encoder;
$this->dispatcher = $dispatcher;
$this->ttl = $ttl;
}

/**
* @param UserInterface $user
* @return string
*/
public function create(UserInterface $user)
{
$payload = array();
$payload['exp'] = time() + $this->ttl;
$payload['username'] = $user->getUsername();

$event = new JWTCreatedEvent($payload, $user);
$this->dispatcher->dispatch(Events::JWT_CREATED, $event);

$payload = $event->getData();
$jwt = $this->encoder->encode($payload)->getTokenString();

return $jwt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ protected function getHandler()
->method('getTokenString')
->will($this->returnValue('tokenstring'));

$encoder = $this->getMockBuilder('Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoder')
$creator = $this->getMockBuilder('Lexik\Bundle\JWTAuthenticationBundle\Services\JWTCreator')
->disableOriginalConstructor()
->getMock();

$encoder
$creator
->expects($this->any())
->method('encode')
->will($this->returnValue($jws));
->method('create')
->will($this->returnValue($jws->getTokenString()));

$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
->disableOriginalConstructor()
->getMock();

return new AuthenticationSuccessHandler($encoder, $dispatcher, 3600);
return new AuthenticationSuccessHandler($creator, $dispatcher);
}

/**
Expand Down

0 comments on commit 2e50b09

Please sign in to comment.