-
-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from gfreeau/master
Added JWT Creator service
- Loading branch information
Showing
6 changed files
with
125 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters