-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
373 additions
and
404 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
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,66 @@ | ||
<?php | ||
/* | ||
* This file is part of the StfalconApiBundle. | ||
* | ||
* (c) Stfalcon LLC <stfalcon.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StfalconStudio\ApiBundle\EventListener\Security; | ||
|
||
use StfalconStudio\ApiBundle\Model\Credentials\CredentialsInterface; | ||
use StfalconStudio\ApiBundle\Security\JwtBlackListService; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Security\Core\Exception\BadCredentialsException; | ||
use Symfony\Component\Security\Http\Event\CheckPassportEvent; | ||
|
||
/** | ||
* CheckVerifiedUserSubscriber. | ||
*/ | ||
final class CheckVerifiedUserSubscriber implements EventSubscriberInterface | ||
{ | ||
private readonly JwtBlackListService $tokenBlackListService; | ||
|
||
/** | ||
* @param JwtBlackListService $tokenBlackListService | ||
*/ | ||
public function __construct(JwtBlackListService $tokenBlackListService) | ||
{ | ||
$this->tokenBlackListService = $tokenBlackListService; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents(): iterable | ||
{ | ||
yield CheckPassportEvent::class => 'onCheckPassport'; | ||
} | ||
|
||
/** | ||
* @param CheckPassportEvent $event | ||
* | ||
* @throws BadCredentialsException | ||
* | ||
* @return void | ||
*/ | ||
public function onCheckPassport(CheckPassportEvent $event): void | ||
{ | ||
$passport = $event->getPassport(); | ||
$user = $passport->getUser(); | ||
|
||
$payload = $passport->getAttribute('payload'); | ||
if ($user instanceof CredentialsInterface && $user->getCredentialsLastChangedAt() instanceof \DateTime && is_array($payload) && (int) $payload['iat'] < $user->getCredentialsLastChangedAt()->getTimestamp()) { | ||
throw new BadCredentialsException('Credentials were changed.'); | ||
} | ||
|
||
$token = $passport->getAttribute('token'); | ||
if (\is_string($token) && $this->tokenBlackListService->tokenIsNotInBlackList($user, $token)) { | ||
throw new BadCredentialsException('Token in the black list.'); | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,42 @@ | ||
<?php | ||
/* | ||
* This file is part of the StfalconApiBundle. | ||
* | ||
* (c) Stfalcon LLC <stfalcon.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StfalconStudio\ApiBundle\Tests\EventListener\JWT; | ||
|
||
use StfalconStudio\ApiBundle\Model\Credentials\CredentialsInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class DummyUser implements UserInterface, CredentialsInterface | ||
{ | ||
public function setCredentialsLastChangedAt(?\DateTime $credentialsLastChangedAt): void | ||
{ | ||
} | ||
|
||
public function getCredentialsLastChangedAt(): ?\DateTime | ||
{ | ||
return null; | ||
} | ||
|
||
public function getRoles(): array | ||
{ | ||
return ['ROLE_USER']; | ||
} | ||
|
||
public function eraseCredentials(): void | ||
{ | ||
} | ||
|
||
public function getUserIdentifier(): string | ||
{ | ||
return 'dummy'; | ||
} | ||
} |
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
Oops, something went wrong.