Skip to content

Commit

Permalink
New authenticator for the newer Symfony authenticator API
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Jun 29, 2021
1 parent aafe5fb commit 23f4fa6
Show file tree
Hide file tree
Showing 11 changed files with 497 additions and 7 deletions.
49 changes: 49 additions & 0 deletions Event/RefreshAuthenticationFailureEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the GesdinetJWTRefreshTokenBundle package.
*
* (c) Gesdinet <http://www.gesdinet.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gesdinet\JWTRefreshTokenBundle\Event;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class RefreshAuthenticationFailureEvent extends Event
{
/**
* @var AuthenticationException
*/
private $exception;

/**
* @var Response|null
*/
private $response;

public function __construct(AuthenticationException $exception, ?Response $response = null)
{
$this->exception = $exception;
$this->response = $response;
}

public function getException(): AuthenticationException
{
return $this->exception;
}

public function getResponse(): ?Response
{
return $this->response;
}

public function setResponse(?Response $response = null): void
{
$this->response = $response;
}
}
24 changes: 19 additions & 5 deletions Event/RefreshEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,41 @@
namespace Gesdinet\JWTRefreshTokenBundle\Event;

use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenInterface;
use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class RefreshEvent extends Event
{
/**
* @var RefreshTokenInterface
*/
private $refreshToken;

private $preAuthenticatedToken;
/**
* @var TokenInterface
*/
private $token;

public function __construct(RefreshTokenInterface $refreshToken, PostAuthenticationGuardToken $preAuthenticatedToken)
public function __construct(RefreshTokenInterface $refreshToken, TokenInterface $token)
{
$this->refreshToken = $refreshToken;
$this->preAuthenticatedToken = $preAuthenticatedToken;
$this->token = $token;
}

public function getRefreshToken()
{
return $this->refreshToken;
}

/**
* @deprecated use getToken() instead
*/
public function getPreAuthenticatedToken()
{
return $this->preAuthenticatedToken;
return $this->getToken();
}

public function getToken(): TokenInterface
{
return $this->token;
}
}
49 changes: 49 additions & 0 deletions Event/RefreshTokenNotFoundEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the GesdinetJWTRefreshTokenBundle package.
*
* (c) Gesdinet <http://www.gesdinet.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gesdinet\JWTRefreshTokenBundle\Event;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class RefreshTokenNotFoundEvent extends Event
{
/**
* @var AuthenticationException
*/
private $exception;

/**
* @var Response|null
*/
private $response;

public function __construct(AuthenticationException $exception, ?Response $response = null)
{
$this->exception = $exception;
$this->response = $response;
}

public function getException(): AuthenticationException
{
return $this->exception;
}

public function getResponse(): ?Response
{
return $this->response;
}

public function setResponse(?Response $response = null): void
{
$this->response = $response;
}
}
47 changes: 47 additions & 0 deletions Http/RefreshAuthenticationFailureResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the GesdinetJWTRefreshTokenBundle package.
*
* (c) Gesdinet <http://www.gesdinet.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gesdinet\JWTRefreshTokenBundle\Http;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class RefreshAuthenticationFailureResponse extends JsonResponse
{
/**
* @var string
*/
private $message;

public function __construct(string $message = 'Bad credentials', int $statusCode = Response::HTTP_UNAUTHORIZED)
{
$this->message = $message;

parent::__construct(null, $statusCode);
}

public function setMessage(string $message): self
{
$this->message = $message;

return $this->setData();
}

public function getMessage(): string
{
return $this->message;
}

public function setData($data = []): self
{
return parent::setData((array) $data + ['code' => $this->statusCode, 'message' => $this->message]);
}
}
16 changes: 14 additions & 2 deletions Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Gesdinet\JWTRefreshTokenBundle\Doctrine\RefreshTokenManager;
use Gesdinet\JWTRefreshTokenBundle\EventListener\AttachRefreshTokenOnSuccessListener;
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
use Gesdinet\JWTRefreshTokenBundle\Security\Authenticator\RefreshTokenAuthenticator;
use Gesdinet\JWTRefreshTokenBundle\Security\Authenticator\RefreshTokenAuthenticator as LegacyRefreshTokenAuthenticator;
use Gesdinet\JWTRefreshTokenBundle\Security\Http\Authenticator\RefreshTokenAuthenticator;
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider;
use Gesdinet\JWTRefreshTokenBundle\Service\RefreshToken;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
Expand Down Expand Up @@ -62,12 +63,23 @@
]);

$services->set('gesdinet.jwtrefreshtoken.authenticator')
->class(RefreshTokenAuthenticator::class)
->class(LegacyRefreshTokenAuthenticator::class)
->args([
new Reference('gesdinet.jwtrefreshtoken.user_checker'),
'%gesdinet_jwt_refresh_token.token_parameter_name%',
]);

$services->set('gesdinet.jwtrefreshtoken.http.authenticator')
->class(RefreshTokenAuthenticator::class)
->args([
new Reference('lexik_jwt_authentication.handler.authentication_success'),
new Reference('gesdinet.jwtrefreshtoken.refresh_token_manager'),
new Reference('event_dispatcher'),
'%gesdinet_jwt_refresh_token.token_parameter_name%',
'%gesdinet_jwt_refresh_token.ttl%',
'%gesdinet_jwt_refresh_token.ttl_update%',
]);

$services->set(ClearInvalidRefreshTokensCommand::class)
->args([
new Reference('gesdinet.jwtrefreshtoken.refresh_token_manager'),
Expand Down
22 changes: 22 additions & 0 deletions Security/Exception/InvalidTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the GesdinetJWTRefreshTokenBundle package.
*
* (c) Gesdinet <http://www.gesdinet.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gesdinet\JWTRefreshTokenBundle\Security\Exception;

use Symfony\Component\Security\Core\Exception\AuthenticationException;

class InvalidTokenException extends AuthenticationException
{
public function getMessageKey()
{
return 'Invalid JWT Refresh Token';
}
}
22 changes: 22 additions & 0 deletions Security/Exception/MissingTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the GesdinetJWTRefreshTokenBundle package.
*
* (c) Gesdinet <http://www.gesdinet.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gesdinet\JWTRefreshTokenBundle\Security\Exception;

use Symfony\Component\Security\Core\Exception\AuthenticationException;

class MissingTokenException extends AuthenticationException
{
public function getMessageKey()
{
return 'Missing JWT Refresh Token';
}
}
22 changes: 22 additions & 0 deletions Security/Exception/TokenNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the GesdinetJWTRefreshTokenBundle package.
*
* (c) Gesdinet <http://www.gesdinet.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gesdinet\JWTRefreshTokenBundle\Security\Exception;

use Symfony\Component\Security\Core\Exception\AuthenticationException;

class TokenNotFoundException extends AuthenticationException
{
public function getMessageKey()
{
return 'JWT Refresh Token Not Found';
}
}
Loading

0 comments on commit 23f4fa6

Please sign in to comment.