Skip to content

Commit

Permalink
Merge pull request lexik#73 from 9orky/feature/onJwtEncodedEvent
Browse files Browse the repository at this point in the history
add JWTEncodedEvent so JWT string is available after its creation
  • Loading branch information
slashfan committed Jul 8, 2015
2 parents 5bce68a + f9c8ddc commit fb8c731
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
34 changes: 34 additions & 0 deletions Event/JWTEncodedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Lexik\Bundle\JWTAuthenticationBundle\Event;

use Symfony\Component\EventDispatcher\Event;

/**
* Class JWTEncodedEvent
* @package Lexik\Bundle\JWTAuthenticationBundle\Event
*/
class JWTEncodedEvent extends Event
{

/**
* @var $jwtString
*/
private $jwtString;

/**
* @param $jwtString
*/
public function __construct($jwtString)
{
$this->jwtString = $jwtString;
}

/**
* @return $jwtString
*/
public function getJWTString()
{
return $this->jwtString;
}
}
6 changes: 6 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ final class Events
*/
const JWT_CREATED = 'lexik_jwt_authentication.on_jwt_created';

/**
* Dispatched right after token string is created.
* Hook into this event to get token representation itself.
*/
const JWT_ENCODED = 'lexik_jwt_authentication.on_jwt_encoded';

/**
* Dispatched after the token payload has been decoded by the configured encoder (JWTEncoder by default).
* Hook into this event to perform additional validation on the received payload.
Expand Down
17 changes: 17 additions & 0 deletions Resources/doc/2-data-customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,20 @@ public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $even
$event->setData($data);
}
```
#### Events::JWT_ENCODED - get JWT string

You may need to get JWT after its creation.

Example 6: obtain JWT string

``` php
// Acme\Bundle\ApiBundle\EventListener\OnJwtEncoded.php

/**
* @param JWTEncodedEvent $event
*/
public function onJwtEncoded(JWTEncodedEvent $event)
{
$token = $event->getJWTString();
}
```
13 changes: 10 additions & 3 deletions Services/JWTManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTDecodedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTEncodedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;

/**
* JWTManager
*
Expand Down Expand Up @@ -67,10 +69,15 @@ public function create(UserInterface $user)

$this->addUserIdentityToPayload($user, $payload);

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

$jwtString = $this->jwtEncoder->encode($jwtCreatedEvent->getData());

$jwtEncodedEvent = new JWTEncodedEvent($jwtString);
$this->dispatcher->dispatch(Events::JWT_ENCODED, $jwtEncodedEvent);

return $this->jwtEncoder->encode($event->getData());
return $jwtString;
}

/**
Expand Down
20 changes: 18 additions & 2 deletions Tests/Services/JWTManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ public function testCreate()
{
$dispatcher = $this->getEventDispatcherMock();
$dispatcher
->expects($this->once())
->expects($this->at(0))
->method('dispatch')
->with(
$this->equalTo(Events::JWT_CREATED),
$this->isInstanceOf('Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent')
);

$dispatcher
->expects($this->at(1))
->method('dispatch')
->with(
$this->equalTo(Events::JWT_ENCODED),
$this->isInstanceOf('Lexik\Bundle\JWTAuthenticationBundle\Event\JWTEncodedEvent')
);

$encoder = $this->getJWTEncoderMock();
$encoder
->expects($this->once())
Expand Down Expand Up @@ -70,13 +78,21 @@ public function testIdentityField()

$dispatcher = $this->getEventDispatcherMock();
$dispatcher
->expects($this->once())
->expects($this->at(0))
->method('dispatch')
->with(
$this->equalTo(Events::JWT_CREATED),
$this->isInstanceOf('Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent')
);

$dispatcher
->expects($this->at(1))
->method('dispatch')
->with(
$this->equalTo(Events::JWT_ENCODED),
$this->isInstanceOf('Lexik\Bundle\JWTAuthenticationBundle\Event\JWTEncodedEvent')
);

$encoder = $this->getJWTEncoderMock();
$encoder
->expects($this->once())
Expand Down

0 comments on commit fb8c731

Please sign in to comment.