Passed
Push — master ( a0d4b0...c0930e )
by Angel Fernando Quiroz
15:43 queued 07:51
created

AzureAuthenticator::userLoader()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
nc 4
nop 1
dl 0
loc 28
rs 9.8333
c 1
b 0
f 0
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 Chamilo\CoreBundle\ServiceHelper\AzureAuthenticatorHelper;
14
use Doctrine\ORM\EntityManagerInterface;
15
use Doctrine\ORM\NonUniqueResultException;
16
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
17
use League\OAuth2\Client\Token\AccessToken;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
20
use Symfony\Component\Routing\RouterInterface;
21
use TheNetworg\OAuth2\Client\Provider\Azure;
22
23
class AzureAuthenticator extends AbstractAuthenticator
24
{
25
    protected string $providerName = 'azure';
26
27
    public function __construct(
28
        ClientRegistry $clientRegistry,
29
        RouterInterface $router,
30
        UserRepository $userRepository,
31
        AuthenticationConfigHelper $authenticationConfigHelper,
32
        AccessUrlHelper $urlHelper,
33
        EntityManagerInterface $entityManager,
34
        private readonly AzureAuthenticatorHelper $azureHelper,
35
    ) {
36
        parent::__construct(
37
            $clientRegistry,
38
            $router,
39
            $userRepository,
40
            $authenticationConfigHelper,
41
            $urlHelper,
42
            $entityManager
43
        );
44
    }
45
46
    public function supports(Request $request): ?bool
47
    {
48
        return 'chamilo.oauth2_azure_check' === $request->attributes->get('_route');
49
    }
50
51
    /**
52
     * @throws NonUniqueResultException
53
     */
54
    protected function userLoader(AccessToken $accessToken): User
55
    {
56
        /** @var Azure $provider */
57
        $provider = $this->client->getOAuth2Provider();
58
59
        $me = $provider->get('/me', $accessToken);
60
61
        if (empty($me['mail'])) {
62
            throw new UnauthorizedHttpException(
63
                'The mail field is empty in Azure AD and is needed to set the organisation email for this user.'
64
            );
65
        }
66
67
        if (empty($me['mailNickname'])) {
68
            throw new UnauthorizedHttpException(
69
                'The mailNickname field is empty in Azure AD and is needed to set the unique username for this user.'
70
            );
71
        }
72
73
        if (empty($me['objectId'])) {
74
            throw new UnauthorizedHttpException(
75
                'The id field is empty in Azure AD and is needed to set the unique Azure ID for this user.'
76
            );
77
        }
78
79
        $userId = $this->azureHelper->registerUser($me);
80
81
        return $this->userRepository->find($userId);
82
    }
83
}