1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Security\Authenticator\OAuth2; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\User; |
10
|
|
|
use Chamilo\CoreBundle\Repository\Node\UserRepository; |
11
|
|
|
use Chamilo\CoreBundle\ServiceHelper\AuthenticationConfigHelper; |
12
|
|
|
use KnpU\OAuth2ClientBundle\Client\ClientRegistry; |
13
|
|
|
use KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface; |
14
|
|
|
use KnpU\OAuth2ClientBundle\Security\Authenticator\OAuth2Authenticator; |
15
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
16
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\Routing\RouterInterface; |
20
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
21
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
22
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
23
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
24
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; |
25
|
|
|
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; |
26
|
|
|
|
27
|
|
|
abstract class AbstractAuthenticator extends OAuth2Authenticator implements AuthenticationEntryPointInterface |
28
|
|
|
{ |
29
|
|
|
protected string $providerName = ''; |
30
|
|
|
|
31
|
|
|
protected OAuth2ClientInterface $client; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
protected readonly ClientRegistry $clientRegistry, |
35
|
|
|
protected readonly RouterInterface $router, |
36
|
|
|
protected readonly UserRepository $userRepository, |
37
|
|
|
protected readonly AuthenticationConfigHelper $authenticationConfigHelper, |
38
|
|
|
) { |
39
|
|
|
$this->client = $this->clientRegistry->getClient($this->providerName); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function start(Request $request, ?AuthenticationException $authException = null): Response |
43
|
|
|
{ |
44
|
|
|
$targetUrl = $this->router->generate('login'); |
45
|
|
|
|
46
|
|
|
return new RedirectResponse($targetUrl); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
abstract public function supports(Request $request): ?bool; |
50
|
|
|
|
51
|
|
|
public function authenticate(Request $request): Passport |
52
|
|
|
{ |
53
|
|
|
/** @var AccessToken $accessToken */ |
54
|
|
|
$accessToken = $this->fetchAccessToken($this->client); |
55
|
|
|
|
56
|
|
|
$user = $this->userLoader($accessToken); |
57
|
|
|
|
58
|
|
|
return new SelfValidatingPassport( |
59
|
|
|
new UserBadge( |
60
|
|
|
$user->getUserIdentifier() |
61
|
|
|
), |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
66
|
|
|
{ |
67
|
|
|
$targetUrl = $this->router->generate('index'); |
68
|
|
|
|
69
|
|
|
return new RedirectResponse($targetUrl); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response |
73
|
|
|
{ |
74
|
|
|
$message = strtr($exception->getMessage(), $exception->getMessageData()); |
75
|
|
|
|
76
|
|
|
return new Response($message, Response::HTTP_FORBIDDEN); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
abstract protected function userLoader(AccessToken $accessToken): User; |
80
|
|
|
} |
81
|
|
|
|