Passed
Pull Request — master (#5753)
by Angel Fernando Quiroz
07:49
created

AbstractAuthenticator::authenticate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
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\AccessUrlHelper;
12
use Chamilo\CoreBundle\ServiceHelper\AuthenticationConfigHelper;
13
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
14
use KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface;
15
use KnpU\OAuth2ClientBundle\Security\Authenticator\OAuth2Authenticator;
16
use League\OAuth2\Client\Token\AccessToken;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Routing\RouterInterface;
21
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
22
use Symfony\Component\Security\Core\Exception\AuthenticationException;
23
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
24
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
25
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
26
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
27
28
abstract class AbstractAuthenticator extends OAuth2Authenticator implements AuthenticationEntryPointInterface
29
{
30
    protected string $providerName = '';
31
32
    protected OAuth2ClientInterface $client;
33
34
    public function __construct(
35
        protected readonly ClientRegistry $clientRegistry,
36
        protected readonly RouterInterface $router,
37
        protected readonly UserRepository $userRepository,
38
        protected readonly AuthenticationConfigHelper $authenticationConfigHelper,
39
        protected readonly AccessUrlHelper $urlHelper,
40
    ) {
41
        $this->client = $this->clientRegistry->getClient($this->providerName);
42
    }
43
44
    public function start(Request $request, ?AuthenticationException $authException = null): Response
45
    {
46
        $targetUrl = $this->router->generate('login');
47
48
        return new RedirectResponse($targetUrl);
49
    }
50
51
    abstract public function supports(Request $request): ?bool;
52
53
    public function authenticate(Request $request): Passport
54
    {
55
        /** @var AccessToken $accessToken */
56
        $accessToken = $this->fetchAccessToken($this->client);
57
58
        $user = $this->userLoader($accessToken);
59
60
        return new SelfValidatingPassport(
61
            new UserBadge(
62
                $user->getUserIdentifier()
63
            ),
64
        );
65
    }
66
67
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
68
    {
69
        $targetUrl = $this->router->generate('index');
70
71
        return new RedirectResponse($targetUrl);
72
    }
73
74
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
75
    {
76
        $message = strtr($exception->getMessage(), $exception->getMessageData());
77
78
        return new Response($message, Response::HTTP_FORBIDDEN);
79
    }
80
81
    /**
82
     * Find or create and save the new user.
83
     */
84
    abstract protected function userLoader(AccessToken $accessToken): User;
85
}
86