1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\ServiceHelper; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\AccessUrl; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
12
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
13
|
|
|
|
14
|
|
|
use function Symfony\Component\String\u; |
|
|
|
|
15
|
|
|
|
16
|
|
|
readonly class AuthenticationConfigHelper |
17
|
|
|
{ |
18
|
|
|
public function __construct( |
19
|
|
|
private ParameterBagInterface $parameterBag, |
20
|
|
|
private AccessUrlHelper $urlHelper, |
21
|
|
|
private UrlGeneratorInterface $urlGenerator, |
22
|
|
|
) {} |
23
|
|
|
|
24
|
|
|
public function getProviderConfig(string $providerName, ?AccessUrl $url = null): array |
25
|
|
|
{ |
26
|
|
|
$providers = $this->getProvidersForUrl($url); |
27
|
|
|
|
28
|
|
|
if (!isset($providers[$providerName])) { |
29
|
|
|
throw new InvalidArgumentException('Invalid authentication provider for access URL'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $providers[$providerName]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function isEnabled(string $methodName, ?AccessUrl $url = null): bool |
36
|
|
|
{ |
37
|
|
|
$configParams = $this->getProviderConfig($methodName, $url); |
38
|
|
|
|
39
|
|
|
return $configParams['enabled'] ?? false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getEnabledProviders(?AccessUrl $url = null): array |
43
|
|
|
{ |
44
|
|
|
$urlProviders = $this->getProvidersForUrl($url); |
45
|
|
|
|
46
|
|
|
$enabledProviders = []; |
47
|
|
|
|
48
|
|
|
foreach ($urlProviders as $providerName => $providerParams) { |
49
|
|
|
if ($providerParams['enabled'] ?? false) { |
50
|
|
|
$enabledProviders[] = [ |
51
|
|
|
'name' => $providerName, |
52
|
|
|
'title' => $providerParams['title'] ?? u($providerName)->title(), |
|
|
|
|
53
|
|
|
'url' => $this->urlGenerator->generate(\sprintf('chamilo.oauth2_%s_start', $providerName)), |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $enabledProviders; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function getProvidersForUrl(?AccessUrl $url): array |
62
|
|
|
{ |
63
|
|
|
$urlId = $url ? $url->getId() : $this->urlHelper->getCurrent()->getId(); |
64
|
|
|
|
65
|
|
|
$authentication = $this->parameterBag->get('authentication'); |
66
|
|
|
|
67
|
|
|
if (isset($authentication[$urlId])) { |
68
|
|
|
return $authentication[$urlId]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (isset($authentication['default'])) { |
72
|
|
|
return $authentication['default']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
throw new InvalidArgumentException('Invalid access URL configuration'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getProviderOptions(string $providerType, array $config): array |
79
|
|
|
{ |
80
|
|
|
$defaults = match ($providerType) { |
81
|
|
|
'generic' => [ |
82
|
|
|
'clientId' => $config['client_id'], |
83
|
|
|
'clientSecret' => $config['client_secret'], |
84
|
|
|
'urlAuthorize' => $config['urlAuthorize'], |
85
|
|
|
'urlAccessToken' => $config['urlAccessToken'], |
86
|
|
|
'urlResourceOwnerDetails' => $config['urlResourceOwnerDetails'], |
87
|
|
|
'accessTokenMethod' => $config['accessTokenMethod'] ?? null, |
88
|
|
|
'accessTokenResourceOwnerId' => $config['accessTokenResourceOwnerId'] ?? null, |
89
|
|
|
'scopeSeparator' => $config['scopeSeparator'] ?? null, |
90
|
|
|
'responseError' => $config['responseError'] ?? null, |
91
|
|
|
'responseCode' => $config['responseCode'] ?? null, |
92
|
|
|
'responseResourceOwnerId' => $config['responseResourceOwnerId'] ?? null, |
93
|
|
|
'scopes' => $config['scopes'] ?? null, |
94
|
|
|
'pkceMethod' => $config['pkceMethod'] ?? null, |
95
|
|
|
], |
96
|
|
|
'facebook' => [ |
97
|
|
|
'clientId' => $config['client_id'], |
98
|
|
|
'clientSecret' => $config['client_secret'], |
99
|
|
|
'graphApiVersion' => $config['graph_api_version'] ?? null, |
100
|
|
|
], |
101
|
|
|
'keycloak' => [ |
102
|
|
|
'clientId' => $config['client_id'], |
103
|
|
|
'clientSecret' => $config['client_secret'], |
104
|
|
|
'authServerUrl' => $config['auth_server_url'], |
105
|
|
|
'realm' => $config['realm'], |
106
|
|
|
'version' => $config['version'] ?? null, |
107
|
|
|
'encryptionAlgorithm' => $config['encryption_algorithm'] ?? null, |
108
|
|
|
'encryptionKeyPath' => $config['encryption_key_path'] ?? null, |
109
|
|
|
'encryptionKey' => $config['encryption_key'] ?? null, |
110
|
|
|
], |
111
|
|
|
'azure' => [ |
112
|
|
|
'clientId' => $config['client_id'], |
113
|
|
|
'clientSecret' => $config['client_secret'], |
114
|
|
|
'clientCertificatePrivateKey' => $config['client_certificate_private_key'] ?? null, |
115
|
|
|
'clientCertificateThumbprint' => $config['client_certificate_thumbprint'] ?? null, |
116
|
|
|
'urlLogin' => $config['url_login'] ?? null, |
117
|
|
|
'pathAuthorize' => $config['path_authorize'] ?? null, |
118
|
|
|
'pathToken' => $config['path_token'] ?? null, |
119
|
|
|
'scope' => $config['scope'] ?? null, |
120
|
|
|
'tenant' => $config['tenant'] ?? null, |
121
|
|
|
'urlAPI' => $config['url_api'] ?? null, |
122
|
|
|
'resource' => $config['resource'] ?? null, |
123
|
|
|
'API_VERSION' => $config['api_version'] ?? null, |
124
|
|
|
'authWithResource' => $config['auth_with_resource'] ?? null, |
125
|
|
|
'defaultEndPointVersion' => $config['default_end_point_version'] ?? null, |
126
|
|
|
], |
127
|
|
|
}; |
128
|
|
|
|
129
|
|
|
return array_filter($defaults, fn ($value) => null !== $value); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|