Skip to content

Commit

Permalink
feat(SDK-4733): Implement support for Back-Channel Logout (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
evansims authored Dec 12, 2023
1 parent 780e02c commit a87a919
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
6 changes: 6 additions & 0 deletions config/definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
->scalarNode('event_listener_provider')
->defaultNull()
->end()
->scalarNode('backchannel_logout_cache')
->defaultNull()
->end()
->integerNode('backchannel_logout_expires')
->defaultValue(2592000)
->end()
->end()
->end() // sdk
->arrayNode('authenticator')
Expand Down
14 changes: 14 additions & 0 deletions docs/BackchannelLogout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Backchannel Logout

The Auth0 Symfony SDK supports [Backchannel Logout](https://auth0.com/docs/authenticate/login/logout/back-channel-logout) from v5.2 onward. To use this feature, some additional configuration is necessary:

1. **Add a new route to your application.** This route must be publicly accessible. Auth0 will use it to send backchannel logout requests to your application. For example, from your `config/routes.yaml` file:

```yaml
backchannel: # Retrieve backchannel logout tokens from Auth0
path: /backckannel
controller: Auth0\Symfony\Controllers\BackchannelController::handle
methods: POST
```
2. **Configure your Auth0 tenant to use Backchannel Logout.** See the [Auth0 documentation](https://auth0.com/docs/authenticate/login/logout/back-channel-logout/configure-back-channel-logout) for more information on how to do this. Please ensure you point the Logout URI to the backchannel route we just added to your application.
1 change: 1 addition & 0 deletions example/config/packages/cache.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ framework:
pools:
auth0_token_cache: { adapter: cache.adapter.redis }
auth0_management_token_cache: { adapter: cache.adapter.redis }
auth0_bachannel_logout_cache: { adapter: cache.adapter.redis }
7 changes: 6 additions & 1 deletion src/Auth0Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
$managementTokenCache = $config['sdk']['management_token_cache'] ?? 'cache.app';
$managementTokenCache = new Reference($managementTokenCache);

$backchannelLogoutCache = $config['sdk']['backchannel_logout_cache'] ?? 'cache.app';
$backchannelLogoutCache = new Reference($backchannelLogoutCache);

$transientStorage = new Reference($config['sdk']['transient_storage'] ?? 'auth0.store_transient');
$sessionStorage = new Reference($config['sdk']['session_storage'] ?? 'auth0.store_session');

Expand Down Expand Up @@ -126,7 +129,9 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
->arg('$queryUserInfo', false)
->arg('$managementToken', $config['sdk']['management_token'])
->arg('$managementTokenCache', $managementTokenCache)
->arg('$eventListenerProvider', $eventListenerProvider);
->arg('$eventListenerProvider', $eventListenerProvider)
->arg('$backchannelLogoutCache', $backchannelLogoutCache)
->arg('$backchannelLogoutExpires', $config['sdk']['backchannel_logout_expires']);

$container->services()
->set('auth0', Service::class)
Expand Down
54 changes: 54 additions & 0 deletions src/Controllers/BackchannelLogoutController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Auth0\Symfony\Controllers;

use Auth0\SDK\Auth0;
use Auth0\Symfony\Contracts\Controllers\AuthenticationControllerInterface;
use Auth0\Symfony\Security\Authenticator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response};
use Throwable;

use function is_string;

final class BackchannelLogoutController extends AbstractController implements AuthenticationControllerInterface
{
public function __construct(
private Authenticator $authenticator,
) {
}

public function handle(Request $request): Response
{
if ('POST' !== $request->getMethod()) {
return new Response('', Response::HTTP_METHOD_NOT_ALLOWED);
}

$logoutToken = $request->get('logout_token');

if (! is_string($logoutToken)) {
return new Response('', Response::HTTP_BAD_REQUEST);
}

$logoutToken = trim($logoutToken);

if ('' === $logoutToken) {
return new Response('', Response::HTTP_BAD_REQUEST);
}

try {
$this->getSdk()->handleBackchannelLogout($logoutToken);
} catch (Throwable $throwable) {
return new Response($throwable->getMessage(), Response::HTTP_BAD_REQUEST);
}

return new Response('', Response::HTTP_OK);
}

private function getSdk(): Auth0
{
return $this->authenticator->service->getSdk();
}
}

0 comments on commit a87a919

Please sign in to comment.