Skip to content

Add event in firewall / guard #251

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

Draft
wants to merge 49 commits into
base: v3.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
ac7706c
ADD: Create the AUTHENTICATION_FAILURE event
froozeify Dec 6, 2020
cd5219b
RENAME: Wrong filename
froozeify Dec 6, 2020
bb05bd6
ADD: Custom response formatter
froozeify Dec 6, 2020
c0b984f
QUALITY: PHP Coding Standards Fix
froozeify Dec 6, 2020
aec0a1a
QUALITY: File not capitalize
froozeify Dec 6, 2020
a7c5d1a
QUALITY: File not capitalize
froozeify Dec 6, 2020
34a5dac
ADD: Authentication scope failure event
froozeify Dec 7, 2020
f20ae68
REMOVE: exception_event_listener_priority not anymore used
froozeify Dec 7, 2020
ad87d01
FIX: Error in condition blocking the scope verification in firewall
froozeify Dec 7, 2020
16651d1
UPDATE: Remove irrelevant condition
froozeify Dec 7, 2020
b5433c0
UPDATE: Remove unused code
froozeify Dec 7, 2020
6c8a9d4
ADD: MISSING_AUTHORIZATION_HEADER event
froozeify Dec 7, 2020
cf97ed3
ADD: MISSING_AUTHORIZATION_HEADER event for Guard
froozeify Dec 7, 2020
64fc4f3
UPDATE: Switch to InvalidAuthorizationHeader event
froozeify Dec 11, 2020
9d8794d
UPDATE: Guard now support all exception
froozeify Dec 11, 2020
b4836d2
QUALITY: php-cs-fixer
froozeify Dec 11, 2020
388bd3a
FIX: Wrong response status code
froozeify Dec 11, 2020
498d477
FIX: Missing WWW-Authenticate response
froozeify Dec 11, 2020
334f958
QUALITY: php-cs-fixer
froozeify Dec 11, 2020
90ade72
DOC: Add doc for the new events
froozeify Dec 11, 2020
ce304f6
Revert "ADD: Custom response formatter"
froozeify Dec 30, 2020
76180ab
Revert "Custom response formatter"
froozeify Dec 30, 2020
69c1385
UPDATE: Replace the ResponseFormatter to be always an ErrorJsonResponse
froozeify Dec 30, 2020
8498f85
DOC: Update doc, update the UPGRADE section (in case it will be added…
froozeify Dec 30, 2020
7486965
Merge branch 'v3.x' of https://github.com/trikoder/oauth2-bundle into…
froozeify Dec 30, 2020
11ab5e8
QUALITY: php-cs-fixer
froozeify Dec 30, 2020
0c969fc
UPDATE: Rename file and default error message for exception (missing …
froozeify Jan 22, 2021
95a690d
UPDATE: Rename file and default error message for exception (missing …
froozeify Jan 22, 2021
af19bf8
ADD: InvalidCredentials event
froozeify Jan 24, 2021
9cf845c
Merge upstream v3.x into current branch
froozeify Apr 13, 2021
573f08f
update: revert to original version
froozeify Apr 20, 2021
025af85
update: start switching to an ExceptionEventFactory
froozeify Apr 20, 2021
8cb11b2
add: invalidCredential can now use the event system
froozeify Apr 20, 2021
397ec2d
update: switch to AbstractOauthEvent
froozeify Apr 22, 2021
c77a071
update: Move OautEvent to a dedicated folder
froozeify Apr 22, 2021
de9ece0
update: new events names
froozeify Apr 22, 2021
c6d814d
_wip_: update: handling league exception and calling event
froozeify Apr 22, 2021
221951c
update: OauthEvent folder change
froozeify Apr 23, 2021
6134a25
update: import cleanup
froozeify May 15, 2021
a922ce8
add: Conversion from OauthServerException to nice error Response (som…
froozeify May 16, 2021
12c8e5e
add: Conversion from OauthServerException to nice error Response (som…
froozeify May 16, 2021
c4856fa
update: guard now works 100% with events
froozeify May 16, 2021
83742b7
update: import cleanup
froozeify May 16, 2021
b30da91
update: firewall response wasn't converted to sf response
froozeify May 16, 2021
e0fb8b2
update: remove catching to be like in the guard
froozeify May 16, 2021
99229f3
remove: unused Exception
froozeify May 16, 2021
525d3ef
quality: code quality
froozeify May 16, 2021
c7f65ca
test: invalid_credentials error is now returned
froozeify May 16, 2021
d2a6857
fix: forgot to convert Request to PsrRequest
froozeify May 16, 2021
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
16 changes: 12 additions & 4 deletions Controller/AuthorizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Exception\OAuthServerException;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Trikoder\Bundle\OAuth2Bundle\Converter\UserConverterInterface;
use Trikoder\Bundle\OAuth2Bundle\Event\AuthorizationRequestResolveEvent;
use Trikoder\Bundle\OAuth2Bundle\Event\AuthorizationRequestResolveEventFactory;
use Trikoder\Bundle\OAuth2Bundle\Manager\ClientManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\OAuth2Events;
use Trikoder\Bundle\OAuth2Bundle\Security\Exception\ExceptionEventFactory;

final class AuthorizationController
{
Expand Down Expand Up @@ -43,21 +43,28 @@ final class AuthorizationController
*/
private $clientManager;

/**
* @var ExceptionEventFactory
*/
private $exceptionEventFactory;

public function __construct(
AuthorizationServer $server,
EventDispatcherInterface $eventDispatcher,
AuthorizationRequestResolveEventFactory $eventFactory,
UserConverterInterface $userConverter,
ClientManagerInterface $clientManager
ClientManagerInterface $clientManager,
ExceptionEventFactory $exceptionEventFactory
) {
$this->server = $server;
$this->eventDispatcher = $eventDispatcher;
$this->eventFactory = $eventFactory;
$this->userConverter = $userConverter;
$this->clientManager = $clientManager;
$this->exceptionEventFactory = $exceptionEventFactory;
}

public function indexAction(ServerRequestInterface $serverRequest, ResponseFactoryInterface $responseFactory): ResponseInterface
public function indexAction(ServerRequestInterface $serverRequest, ResponseFactoryInterface $responseFactory)
{
$serverResponse = $responseFactory->createResponse();

Expand Down Expand Up @@ -90,7 +97,8 @@ public function indexAction(ServerRequestInterface $serverRequest, ResponseFacto

return $this->server->completeAuthorizationRequest($authRequest, $serverResponse);
} catch (OAuthServerException $e) {
return $e->generateHttpResponse($serverResponse);
$event = $this->exceptionEventFactory->handleLeagueException($e);
return $event->getResponse();
}
}
}
15 changes: 11 additions & 4 deletions Controller/TokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Exception\OAuthServerException;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Trikoder\Bundle\OAuth2Bundle\Security\Exception\ExceptionEventFactory;

final class TokenController
{
Expand All @@ -17,21 +17,28 @@ final class TokenController
*/
private $server;

public function __construct(AuthorizationServer $server)
/**
* @var ExceptionEventFactory
*/
private $exceptionEventFactory;

public function __construct(AuthorizationServer $server, ExceptionEventFactory $exceptionEventFactory)
{
$this->server = $server;
$this->exceptionEventFactory = $exceptionEventFactory;
}

public function indexAction(
ServerRequestInterface $serverRequest,
ResponseFactoryInterface $responseFactory
): ResponseInterface {
) {
$serverResponse = $responseFactory->createResponse();

try {
return $this->server->respondToAccessTokenRequest($serverRequest, $serverResponse);
} catch (OAuthServerException $e) {
return $e->generateHttpResponse($serverResponse);
$event = $this->exceptionEventFactory->handleLeagueException($e);
return $event->getResponse();
}
}
}
4 changes: 0 additions & 4 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ public function getConfigTreeBuilder(): TreeBuilder

$rootNode
->children()
->scalarNode('exception_event_listener_priority')
->info('The priority of the event listener that converts an Exception to a Response.')
->defaultValue(10)
->end()
->scalarNode('role_prefix')
->info('Set a custom prefix that replaces the default "ROLE_OAUTH2_" role prefix.')
->defaultValue('ROLE_OAUTH2_')
Expand Down
12 changes: 6 additions & 6 deletions DependencyInjection/TrikoderOAuth2Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
use Trikoder\Bundle\OAuth2Bundle\DBAL\Type\Grant as GrantType;
use Trikoder\Bundle\OAuth2Bundle\DBAL\Type\RedirectUri as RedirectUriType;
use Trikoder\Bundle\OAuth2Bundle\DBAL\Type\Scope as ScopeType;
use Trikoder\Bundle\OAuth2Bundle\EventListener\ConvertExceptionToResponseListener;
use Trikoder\Bundle\OAuth2Bundle\EventListener\ExceptionToOauthResponseListener;
use Trikoder\Bundle\OAuth2Bundle\League\AuthorizationServer\GrantTypeInterface;
use Trikoder\Bundle\OAuth2Bundle\Manager\Doctrine\AccessTokenManager;
use Trikoder\Bundle\OAuth2Bundle\Manager\Doctrine\AuthorizationCodeManager;
Expand Down Expand Up @@ -66,15 +66,15 @@ public function load(array $configs, ContainerBuilder $container)
$container->getDefinition(OAuth2TokenFactory::class)
->setArgument(0, $config['role_prefix']);

$container->getDefinition(ConvertExceptionToResponseListener::class)
$container->registerForAutoconfiguration(GrantTypeInterface::class)
->addTag('trikoder.oauth2.authorization_server.grant');

$container->getDefinition(ExceptionToOauthResponseListener::class)
->addTag('kernel.event_listener', [
'event' => KernelEvents::EXCEPTION,
'method' => 'onKernelException',
'priority' => $config['exception_event_listener_priority'],
'priority' => 10
]);

$container->registerForAutoconfiguration(GrantTypeInterface::class)
->addTag('trikoder.oauth2.authorization_server.grant');
}

/**
Expand Down
57 changes: 57 additions & 0 deletions Event/OauthEvent/AbstractOauthEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Trikoder\Bundle\OAuth2Bundle\Event\OauthEvent;

use League\OAuth2\Server\Exception\OAuthServerException;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @author Benoit VIGNAL <github@benoit-vignal.fr>
*/
abstract class AbstractOauthEvent extends Event
{
/**
* @var OAuthServerException
*/
protected $exception;

/**
* @var ResponseInterface
*/
protected $response;

public function __construct(OAuthServerException $exception, ResponseInterface $response)
{
$this->exception = $exception;
$this->response = $response;
}

/**
* @return string The event name that will be use with the eventDispatcher
*/
abstract function getEventName(): string;

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

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

/**
* @param ResponseInterface $response
* @return $this
*/
public function setResponse(ResponseInterface $response): AbstractOauthEvent
{
$this->response = $response;
return $this;
}
}
18 changes: 18 additions & 0 deletions Event/OauthEvent/AuthenticationFailureEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Trikoder\Bundle\OAuth2Bundle\Event\OauthEvent;

use Trikoder\Bundle\OAuth2Bundle\OAuth2Events;

/**
* @author Benoit VIGNAL <github@benoit-vignal.fr>
*/
class AuthenticationFailureEvent extends AbstractOauthEvent
{
function getEventName(): string
{
return OAuth2Events::AUTHENTICATION_FAILURE;
}
}
37 changes: 37 additions & 0 deletions Event/OauthEvent/AuthenticationScopeFailureEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Trikoder\Bundle\OAuth2Bundle\Event\OauthEvent;

use League\OAuth2\Server\Exception\OAuthServerException;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Trikoder\Bundle\OAuth2Bundle\OAuth2Events;

/**
* @author Benoit VIGNAL <github@benoit-vignal.fr>
*/
class AuthenticationScopeFailureEvent extends AbstractOauthEvent
{
/**
* @var TokenInterface|null
*/
private $token;

public function __construct(OAuthServerException $exception, ResponseInterface $response, ?TokenInterface $token = null)
{
parent::__construct($exception, $response);
$this->token = $token;
}

function getEventName(): string
{
return OAuth2Events::AUTHENTICATION_SCOPE_FAILURE;
}

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

declare(strict_types=1);

namespace Trikoder\Bundle\OAuth2Bundle\Event\OauthEvent;

use Trikoder\Bundle\OAuth2Bundle\OAuth2Events;

/**
* @author Benoit VIGNAL <github@benoit-vignal.fr>
*/
class AuthorizationServerErrorEvent extends AbstractOauthEvent
{
function getEventName(): string
{
return OAuth2Events::AUTHORIZATION_SERVER_ERROR;
}
}
18 changes: 18 additions & 0 deletions Event/OauthEvent/InvalidCredentialsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Trikoder\Bundle\OAuth2Bundle\Event\OauthEvent;

use Trikoder\Bundle\OAuth2Bundle\OAuth2Events;

/**
* @author Benoit VIGNAL <github@benoit-vignal.fr>
*/
class InvalidCredentialsEvent extends AbstractOauthEvent
{
function getEventName(): string
{
return OAuth2Events::INVALID_CREDENTIALS;
}
}
18 changes: 18 additions & 0 deletions Event/OauthEvent/MissingAuthorizationHeaderEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Trikoder\Bundle\OAuth2Bundle\Event\OauthEvent;

use Trikoder\Bundle\OAuth2Bundle\OAuth2Events;

/**
* @author Benoit VIGNAL <github@benoit-vignal.fr>
*/
class MissingAuthorizationHeaderEvent extends AbstractOauthEvent
{
function getEventName(): string
{
return OAuth2Events::AUTHORIZATION_HEADER_FAILURE;
}
}
24 changes: 0 additions & 24 deletions EventListener/ConvertExceptionToResponseListener.php

This file was deleted.

41 changes: 41 additions & 0 deletions EventListener/ExceptionToOauthResponseListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Trikoder\Bundle\OAuth2Bundle\EventListener;


use League\OAuth2\Server\Exception\OAuthServerException;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Trikoder\Bundle\OAuth2Bundle\Security\Exception\ExceptionEventFactory;

class ExceptionToOauthResponseListener
{
/**
* @var ExceptionEventFactory
*/
private $exceptionEventFactory;

public function __construct(ExceptionEventFactory $exceptionEventFactory)
{
$this->exceptionEventFactory = $exceptionEventFactory;
}

/**
* This method will catch and convert all OAuthServerException to a nice ErrorResponse
* This will also trigger the event system
*
* @param ExceptionEvent $event
*/
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if ($exception instanceof OAuthServerException) {
$updatedEvent = $this->exceptionEventFactory->handleLeagueException($exception);

$httpFoundationFactory = new HttpFoundationFactory();
$event->setResponse($httpFoundationFactory->createResponse($updatedEvent->getResponse()));
}
}
}
3 changes: 2 additions & 1 deletion League/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Trikoder\Bundle\OAuth2Bundle\League\Repository;

use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Trikoder\Bundle\OAuth2Bundle\Converter\UserConverterInterface;
Expand Down Expand Up @@ -64,7 +65,7 @@ public function getUserEntityByUserCredentials(
$user = $event->getUser();

if (null === $user) {
return null;
throw OAuthServerException::invalidCredentials();
}

return $this->userConverter->toLeague($user);
Expand Down
Loading