forked from chamilo/chamilo-lms
-
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.
SSO: Implement keycloak login/registration - refs BT#21881
- Loading branch information
Showing
6 changed files
with
106 additions
and
0 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
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
26 changes: 26 additions & 0 deletions
26
src/CoreBundle/Controller/OAuth2/KeycloakProviderController.php
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,26 @@ | ||
<?php | ||
|
||
/* For licensing terms, see /license.txt */ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chamilo\CoreBundle\Controller\OAuth2; | ||
|
||
use Chamilo\CoreBundle\ServiceHelper\AuthenticationConfigHelper; | ||
use KnpU\OAuth2ClientBundle\Client\ClientRegistry; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
|
||
class KeycloakProviderController extends AbstractProviderController | ||
{ | ||
#[Route('/connect/keycloak', name: 'chamilo.oauth2_keycloak_start')] | ||
public function connect( | ||
ClientRegistry $clientRegistry, | ||
AuthenticationConfigHelper $authenticationConfigHelper, | ||
): Response { | ||
return $this->getStartResponse('keycloak', $clientRegistry, $authenticationConfigHelper); | ||
} | ||
|
||
#[Route('/connect/keycloak/check', name: 'chamilo.oauth2_keycloak_check')] | ||
public function connectCheck(): void {} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/CoreBundle/Security/Authenticator/OAuth2/KeycloakAuthenticator.php
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,57 @@ | ||
<?php | ||
|
||
/* For licensing terms, see /license.txt */ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chamilo\CoreBundle\Security\Authenticator\OAuth2; | ||
|
||
use Chamilo\CoreBundle\Entity\User; | ||
use KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface; | ||
use League\OAuth2\Client\Token\AccessToken; | ||
use Stevenmaguire\OAuth2\Client\Provider\KeycloakResourceOwner; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class KeycloakAuthenticator extends AbstractAuthenticator | ||
{ | ||
protected string $providerName = 'keycloak'; | ||
|
||
public function supports(Request $request): ?bool | ||
{ | ||
return 'chamilo.oauth2_keycloak_check' === $request->attributes->get('_route'); | ||
} | ||
|
||
protected function userLoader(AccessToken $accessToken): User | ||
{ | ||
/** @var KeycloakResourceOwner $resourceOwner */ | ||
$resourceOwner = $this->client->fetchUserFromToken($accessToken); | ||
|
||
$user = $this->userRepository->findOneBy(['username' => $resourceOwner->getUsername()]) | ||
?: | ||
$this->userRepository->findOneBy(['email' => $resourceOwner->getEmail()]); | ||
|
||
if (!$user) { | ||
$user = (new User()) | ||
->setCreatorId($this->userRepository->getRootUser()->getId()) | ||
; | ||
} | ||
|
||
$username = $user->getUsername() ?: $user->getEmail(); | ||
|
||
$user | ||
->setFirstname($resourceOwner->getFirstName()) | ||
->setLastname($resourceOwner->getLastName()) | ||
->setEmail($resourceOwner->getEmail()) | ||
->setUsername($username) | ||
->setPlainPassword('keycloak') | ||
->setStatus(STUDENT) | ||
->setAuthSource('keycloak') | ||
->setRoleFromStatus(STUDENT) | ||
; | ||
|
||
$this->userRepository->updateUser($user); | ||
// updateAccessUrls ? | ||
return $user; | ||
} | ||
} |