Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SF6.2 Access Token Support #1062

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DependencyInjection/LexikJWTAuthenticationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface;

/**
* This is the class that loads and manages your bundle configuration.
Expand Down Expand Up @@ -44,6 +45,9 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('token_authenticator.xml');
$loader->load('token_extractor.xml');
$loader->load('guard_authenticator.xml');
if (interface_exists(AccessTokenHandlerInterface::class)) {
$loader->load('access_token_handler.xml');
}

if (isset($config['private_key_path'])) {
$config['secret_key'] = $config['private_key_path'];
Expand Down
12 changes: 12 additions & 0 deletions Resources/config/access_token_handler.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="lexik_jwt_authentication.security.access_token_handler" class="Lexik\Bundle\JWTAuthenticationBundle\Security\AccessToken\JWTHandler">
<argument type="service" id="lexik_jwt_authentication.jwt_manager"/>
</service>
</services>
</container>
47 changes: 47 additions & 0 deletions Security/AccessToken/JWTHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Lexik\Bundle\JWTAuthenticationBundle\Security\AccessToken;

use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface;

/**
* @final
*/
class JWTHandler implements AccessTokenHandlerInterface
{
/**
* @var JWTTokenManagerInterface
*/
private $jwtManager;

public function __construct(
JWTTokenManagerInterface $jwtManager
)
{
$this->jwtManager = $jwtManager;
}

public function getUserIdentifierFrom(string $accessToken): string
{

try {
if (!$payload = $this->jwtManager->parse($accessToken)) {
throw new AuthenticationException('Invalid JWT Token');
}
} catch (\Throwable $e) {
throw new AuthenticationException('Invalid JWT Token', 0, $e);
}

$idClaim = $this->jwtManager->getUserIdClaim();
if (!isset($payload[$idClaim])) {
throw new AuthenticationException(sprintf('Unable to find key "%s" in the token payload.', $idClaim));
}
if (!is_string($payload[$idClaim]) || $payload[$idClaim] === '') {
throw new AuthenticationException(sprintf('Invalid key "%s" in the token payload.', $idClaim));
}

return $payload[$idClaim];
}
}