Skip to content

Commit fb8c731

Browse files
committed
Merge pull request #73 from 9orky/feature/onJwtEncodedEvent
add JWTEncodedEvent so JWT string is available after its creation
2 parents 5bce68a + f9c8ddc commit fb8c731

File tree

5 files changed

+85
-5
lines changed

5 files changed

+85
-5
lines changed

Event/JWTEncodedEvent.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Lexik\Bundle\JWTAuthenticationBundle\Event;
4+
5+
use Symfony\Component\EventDispatcher\Event;
6+
7+
/**
8+
* Class JWTEncodedEvent
9+
* @package Lexik\Bundle\JWTAuthenticationBundle\Event
10+
*/
11+
class JWTEncodedEvent extends Event
12+
{
13+
14+
/**
15+
* @var $jwtString
16+
*/
17+
private $jwtString;
18+
19+
/**
20+
* @param $jwtString
21+
*/
22+
public function __construct($jwtString)
23+
{
24+
$this->jwtString = $jwtString;
25+
}
26+
27+
/**
28+
* @return $jwtString
29+
*/
30+
public function getJWTString()
31+
{
32+
return $this->jwtString;
33+
}
34+
}

Events.php

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ final class Events
2626
*/
2727
const JWT_CREATED = 'lexik_jwt_authentication.on_jwt_created';
2828

29+
/**
30+
* Dispatched right after token string is created.
31+
* Hook into this event to get token representation itself.
32+
*/
33+
const JWT_ENCODED = 'lexik_jwt_authentication.on_jwt_encoded';
34+
2935
/**
3036
* Dispatched after the token payload has been decoded by the configured encoder (JWTEncoder by default).
3137
* Hook into this event to perform additional validation on the received payload.

Resources/doc/2-data-customization.md

+17
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,20 @@ public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $even
178178
$event->setData($data);
179179
}
180180
```
181+
#### Events::JWT_ENCODED - get JWT string
182+
183+
You may need to get JWT after its creation.
184+
185+
Example 6: obtain JWT string
186+
187+
``` php
188+
// Acme\Bundle\ApiBundle\EventListener\OnJwtEncoded.php
189+
190+
/**
191+
* @param JWTEncodedEvent $event
192+
*/
193+
public function onJwtEncoded(JWTEncodedEvent $event)
194+
{
195+
$token = $event->getJWTString();
196+
}
197+
```

Services/JWTManager.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
66
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
77
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTDecodedEvent;
8+
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTEncodedEvent;
89
use Lexik\Bundle\JWTAuthenticationBundle\Events;
910
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1011
use Symfony\Component\HttpFoundation\Request;
1112
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1213
use Symfony\Component\Security\Core\User\UserInterface;
1314
use Symfony\Component\PropertyAccess\PropertyAccess;
15+
1416
/**
1517
* JWTManager
1618
*
@@ -67,10 +69,15 @@ public function create(UserInterface $user)
6769

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

70-
$event = new JWTCreatedEvent($payload, $user, $this->request);
71-
$this->dispatcher->dispatch(Events::JWT_CREATED, $event);
72+
$jwtCreatedEvent = new JWTCreatedEvent($payload, $user, $this->request);
73+
$this->dispatcher->dispatch(Events::JWT_CREATED, $jwtCreatedEvent);
74+
75+
$jwtString = $this->jwtEncoder->encode($jwtCreatedEvent->getData());
76+
77+
$jwtEncodedEvent = new JWTEncodedEvent($jwtString);
78+
$this->dispatcher->dispatch(Events::JWT_ENCODED, $jwtEncodedEvent);
7279

73-
return $this->jwtEncoder->encode($event->getData());
80+
return $jwtString;
7481
}
7582

7683
/**

Tests/Services/JWTManagerTest.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@ public function testCreate()
2121
{
2222
$dispatcher = $this->getEventDispatcherMock();
2323
$dispatcher
24-
->expects($this->once())
24+
->expects($this->at(0))
2525
->method('dispatch')
2626
->with(
2727
$this->equalTo(Events::JWT_CREATED),
2828
$this->isInstanceOf('Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent')
2929
);
3030

31+
$dispatcher
32+
->expects($this->at(1))
33+
->method('dispatch')
34+
->with(
35+
$this->equalTo(Events::JWT_ENCODED),
36+
$this->isInstanceOf('Lexik\Bundle\JWTAuthenticationBundle\Event\JWTEncodedEvent')
37+
);
38+
3139
$encoder = $this->getJWTEncoderMock();
3240
$encoder
3341
->expects($this->once())
@@ -70,13 +78,21 @@ public function testIdentityField()
7078

7179
$dispatcher = $this->getEventDispatcherMock();
7280
$dispatcher
73-
->expects($this->once())
81+
->expects($this->at(0))
7482
->method('dispatch')
7583
->with(
7684
$this->equalTo(Events::JWT_CREATED),
7785
$this->isInstanceOf('Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent')
7886
);
7987

88+
$dispatcher
89+
->expects($this->at(1))
90+
->method('dispatch')
91+
->with(
92+
$this->equalTo(Events::JWT_ENCODED),
93+
$this->isInstanceOf('Lexik\Bundle\JWTAuthenticationBundle\Event\JWTEncodedEvent')
94+
);
95+
8096
$encoder = $this->getJWTEncoderMock();
8197
$encoder
8298
->expects($this->once())

0 commit comments

Comments
 (0)