Skip to content

Commit a87a919

Browse files
authored
feat(SDK-4733): Implement support for Back-Channel Logout (#167)
1 parent 780e02c commit a87a919

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

config/definition.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@
120120
->scalarNode('event_listener_provider')
121121
->defaultNull()
122122
->end()
123+
->scalarNode('backchannel_logout_cache')
124+
->defaultNull()
125+
->end()
126+
->integerNode('backchannel_logout_expires')
127+
->defaultValue(2592000)
128+
->end()
123129
->end()
124130
->end() // sdk
125131
->arrayNode('authenticator')

docs/BackchannelLogout.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Backchannel Logout
2+
3+
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:
4+
5+
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:
6+
7+
```yaml
8+
backchannel: # Retrieve backchannel logout tokens from Auth0
9+
path: /backckannel
10+
controller: Auth0\Symfony\Controllers\BackchannelController::handle
11+
methods: POST
12+
```
13+
14+
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.

example/config/packages/cache.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ framework:
2020
pools:
2121
auth0_token_cache: { adapter: cache.adapter.redis }
2222
auth0_management_token_cache: { adapter: cache.adapter.redis }
23+
auth0_bachannel_logout_cache: { adapter: cache.adapter.redis }

src/Auth0Bundle.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
3030
$managementTokenCache = $config['sdk']['management_token_cache'] ?? 'cache.app';
3131
$managementTokenCache = new Reference($managementTokenCache);
3232

33+
$backchannelLogoutCache = $config['sdk']['backchannel_logout_cache'] ?? 'cache.app';
34+
$backchannelLogoutCache = new Reference($backchannelLogoutCache);
35+
3336
$transientStorage = new Reference($config['sdk']['transient_storage'] ?? 'auth0.store_transient');
3437
$sessionStorage = new Reference($config['sdk']['session_storage'] ?? 'auth0.store_session');
3538

@@ -126,7 +129,9 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
126129
->arg('$queryUserInfo', false)
127130
->arg('$managementToken', $config['sdk']['management_token'])
128131
->arg('$managementTokenCache', $managementTokenCache)
129-
->arg('$eventListenerProvider', $eventListenerProvider);
132+
->arg('$eventListenerProvider', $eventListenerProvider)
133+
->arg('$backchannelLogoutCache', $backchannelLogoutCache)
134+
->arg('$backchannelLogoutExpires', $config['sdk']['backchannel_logout_expires']);
130135

131136
$container->services()
132137
->set('auth0', Service::class)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Auth0\Symfony\Controllers;
6+
7+
use Auth0\SDK\Auth0;
8+
use Auth0\Symfony\Contracts\Controllers\AuthenticationControllerInterface;
9+
use Auth0\Symfony\Security\Authenticator;
10+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11+
use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response};
12+
use Throwable;
13+
14+
use function is_string;
15+
16+
final class BackchannelLogoutController extends AbstractController implements AuthenticationControllerInterface
17+
{
18+
public function __construct(
19+
private Authenticator $authenticator,
20+
) {
21+
}
22+
23+
public function handle(Request $request): Response
24+
{
25+
if ('POST' !== $request->getMethod()) {
26+
return new Response('', Response::HTTP_METHOD_NOT_ALLOWED);
27+
}
28+
29+
$logoutToken = $request->get('logout_token');
30+
31+
if (! is_string($logoutToken)) {
32+
return new Response('', Response::HTTP_BAD_REQUEST);
33+
}
34+
35+
$logoutToken = trim($logoutToken);
36+
37+
if ('' === $logoutToken) {
38+
return new Response('', Response::HTTP_BAD_REQUEST);
39+
}
40+
41+
try {
42+
$this->getSdk()->handleBackchannelLogout($logoutToken);
43+
} catch (Throwable $throwable) {
44+
return new Response($throwable->getMessage(), Response::HTTP_BAD_REQUEST);
45+
}
46+
47+
return new Response('', Response::HTTP_OK);
48+
}
49+
50+
private function getSdk(): Auth0
51+
{
52+
return $this->authenticator->service->getSdk();
53+
}
54+
}

0 commit comments

Comments
 (0)