-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide default login/logout actions and move logic into a service class
- Loading branch information
Showing
7 changed files
with
157 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the package t3g/symfony-keycloak-bundle. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace T3G\Bundle\Keycloak\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use T3G\Bundle\Keycloak\Service\RedirectService; | ||
|
||
class LoginController extends AbstractController | ||
{ | ||
private RedirectService $redirectService; | ||
|
||
public function __construct(RedirectService $redirectService) | ||
{ | ||
$this->redirectService = $redirectService; | ||
} | ||
|
||
public function login(): RedirectResponse | ||
{ | ||
if (null !== $this->getUser()) { | ||
return $this->redirectToRoute($this->getParameter('t3g_keycloak.routes.success')); | ||
} | ||
|
||
return $this->redirectService->generateLoginRedirectResponse(['openid', 'profile', 'roles', 'email']); | ||
} | ||
|
||
public function oauthCallback(): RedirectResponse | ||
{ | ||
// fallback in case the authenticator does not redirect | ||
return $this->redirectToRoute($this->getParameter('t3g_keycloak.routes.success')); | ||
} | ||
|
||
public function oauthLogout(): RedirectResponse | ||
{ | ||
return $this->redirectService->generateLogoutRedirectResponse(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<routes xmlns="http://symfony.com/schema/routing" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/routing | ||
http://symfony.com/schema/routing/routing-1.0.xsd"> | ||
<route id="t3g_keycloak_login" controller="keycloak.typo3.com.login_controller::login" path="/login"></route> | ||
<route id="t3g_keycloak_oauthCallback" controller="keycloak.typo3.com.login_controller::oauthCallback" path="/oauth/callback"></route> | ||
<route id="t3g_keycloak_logout" controller="keycloak.typo3.com.login_controller::oauthLogout" path="/oauth/logout"></route> | ||
</routes> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the package t3g/symfony-keycloak-bundle. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace T3G\Bundle\Keycloak\Service; | ||
|
||
use KnpU\OAuth2ClientBundle\Client\ClientRegistry; | ||
use KnpU\OAuth2ClientBundle\Client\OAuth2Client; | ||
use Stevenmaguire\OAuth2\Client\Provider\Keycloak; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Routing\RouterInterface; | ||
|
||
class RedirectService | ||
{ | ||
private ClientRegistry $clientRegistry; | ||
private RouterInterface $router; | ||
private string $clientId; | ||
|
||
public function __construct(ClientRegistry $clientRegistry, RouterInterface $router, string $clientId) | ||
{ | ||
$this->clientRegistry = $clientRegistry; | ||
$this->router = $router; | ||
$this->clientId = $clientId; | ||
} | ||
|
||
/** | ||
* @param string[] $scopes | ||
*/ | ||
public function generateLoginRedirectResponse(array $scopes): RedirectResponse | ||
{ | ||
/** @var OAuth2Client $client */ | ||
$client = $this->clientRegistry->getClient('keycloak'); | ||
|
||
return $client->redirect($scopes); | ||
} | ||
|
||
public function generateLogoutRedirectResponse(): RedirectResponse | ||
{ | ||
$redirectAfterOAuthLogout = rtrim($this->router->generate('home', [], UrlGeneratorInterface::ABSOLUTE_URL), '/'); | ||
/** @var Keycloak $provider */ | ||
$provider = $this->clientRegistry->getClient('keycloak')->getOAuth2Provider(); | ||
$redirectTarget = sprintf( | ||
'%s/realms/%s/protocol/openid-connect/logout?client_id=%s&post_logout_redirect_uri=%s', | ||
$provider->authServerUrl, | ||
$provider->realm, | ||
$this->clientId, | ||
urlencode($redirectAfterOAuthLogout) | ||
); | ||
|
||
return new RedirectResponse($redirectTarget, Response::HTTP_TEMPORARY_REDIRECT); | ||
} | ||
} |