Passed
Push — dependabot/npm_and_yarn/nanoid... ( aaf2c9...c4aa90 )
by
unknown
14:37 queued 06:22
created

AzureAuthenticator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 53
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
A __construct() 0 16 1
A userLoader() 0 22 4
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('The mail field is empty in Azure AD and is needed to set the organisation email for this user.');
63
        }
64
65
        if (empty($me['mailNickname'])) {
66
            throw new UnauthorizedHttpException('The mailNickname field is empty in Azure AD and is needed to set the unique username for this user.');
67
        }
68
69
        if (empty($me['objectId'])) {
70
            throw new UnauthorizedHttpException('The id field is empty in Azure AD and is needed to set the unique Azure ID for this user.');
71
        }
72
73
        $userId = $this->azureHelper->registerUser($me);
74
75
        return $this->userRepository->find($userId);
76
    }
77
}
78