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->getOAuthProvidersForUrl($url); |
27
|
|
|
|
28
|
|
|
if ([] === $providers) { |
29
|
|
|
return []; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (!isset($providers[$providerName])) { |
33
|
|
|
throw new InvalidArgumentException('Invalid authentication provider for access URL'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $providers[$providerName]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function isOAuth2ProviderEnabled(string $methodName, ?AccessUrl $url = null): bool |
40
|
|
|
{ |
41
|
|
|
$configParams = $this->getProviderConfig($methodName, $url); |
42
|
|
|
|
43
|
|
|
return $configParams['enabled'] ?? false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getEnabledOAuthProviders(?AccessUrl $url = null): array |
47
|
|
|
{ |
48
|
|
|
$urlProviders = $this->getOAuthProvidersForUrl($url); |
49
|
|
|
|
50
|
|
|
$enabledProviders = []; |
51
|
|
|
|
52
|
|
|
foreach ($urlProviders as $providerName => $providerParams) { |
53
|
|
|
if ($providerParams['enabled'] ?? false) { |
54
|
|
|
$enabledProviders[] = [ |
55
|
|
|
'name' => $providerName, |
56
|
|
|
'title' => $providerParams['title'] ?? u($providerName)->title(), |
|
|
|
|
57
|
|
|
'url' => $this->urlGenerator->generate(\sprintf('chamilo.oauth2_%s_start', $providerName)), |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $enabledProviders; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function getOAuthProvidersForUrl(?AccessUrl $url): array |
66
|
|
|
{ |
67
|
|
|
$urlId = $url ? $url->getId() : $this->urlHelper->getCurrent()->getId(); |
68
|
|
|
|
69
|
|
|
$authentication = $this->parameterBag->has('authentication') |
70
|
|
|
? $this->parameterBag->get('authentication') |
71
|
|
|
: []; |
72
|
|
|
|
73
|
|
|
if (isset($authentication[$urlId]['oauth2'])) { |
74
|
|
|
return $authentication[$urlId]['oauth2']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (isset($authentication['default']['oauth2'])) { |
78
|
|
|
return $authentication['default']['oauth2']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return []; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getProviderOptions(string $providerType, array $config): array |
85
|
|
|
{ |
86
|
|
|
$defaults = match ($providerType) { |
87
|
|
|
'generic' => [ |
88
|
|
|
'clientId' => $config['client_id'], |
89
|
|
|
'clientSecret' => $config['client_secret'], |
90
|
|
|
'urlAuthorize' => $config['urlAuthorize'], |
91
|
|
|
'urlAccessToken' => $config['urlAccessToken'], |
92
|
|
|
'urlResourceOwnerDetails' => $config['urlResourceOwnerDetails'], |
93
|
|
|
'accessTokenMethod' => $config['accessTokenMethod'] ?? null, |
94
|
|
|
'accessTokenResourceOwnerId' => $config['accessTokenResourceOwnerId'] ?? null, |
95
|
|
|
'scopeSeparator' => $config['scopeSeparator'] ?? null, |
96
|
|
|
'responseError' => $config['responseError'] ?? null, |
97
|
|
|
'responseCode' => $config['responseCode'] ?? null, |
98
|
|
|
'responseResourceOwnerId' => $config['responseResourceOwnerId'] ?? null, |
99
|
|
|
'scopes' => $config['scopes'] ?? null, |
100
|
|
|
'pkceMethod' => $config['pkceMethod'] ?? null, |
101
|
|
|
], |
102
|
|
|
'facebook' => [ |
103
|
|
|
'clientId' => $config['client_id'], |
104
|
|
|
'clientSecret' => $config['client_secret'], |
105
|
|
|
'graphApiVersion' => $config['graph_api_version'] ?? null, |
106
|
|
|
], |
107
|
|
|
'keycloak' => [ |
108
|
|
|
'clientId' => $config['client_id'], |
109
|
|
|
'clientSecret' => $config['client_secret'], |
110
|
|
|
'authServerUrl' => $config['auth_server_url'], |
111
|
|
|
'realm' => $config['realm'], |
112
|
|
|
'version' => $config['version'] ?? null, |
113
|
|
|
'encryptionAlgorithm' => $config['encryption_algorithm'] ?? null, |
114
|
|
|
'encryptionKeyPath' => $config['encryption_key_path'] ?? null, |
115
|
|
|
'encryptionKey' => $config['encryption_key'] ?? null, |
116
|
|
|
], |
117
|
|
|
'azure' => [ |
118
|
|
|
'clientId' => $config['client_id'], |
119
|
|
|
'clientSecret' => $config['client_secret'], |
120
|
|
|
'clientCertificatePrivateKey' => $config['client_certificate_private_key'] ?? null, |
121
|
|
|
'clientCertificateThumbprint' => $config['client_certificate_thumbprint'] ?? null, |
122
|
|
|
'urlLogin' => $config['url_login'] ?? null, |
123
|
|
|
'pathAuthorize' => $config['path_authorize'] ?? null, |
124
|
|
|
'pathToken' => $config['path_token'] ?? null, |
125
|
|
|
'scope' => $config['scope'] ?? null, |
126
|
|
|
'tenant' => $config['tenant'] ?? null, |
127
|
|
|
'urlAPI' => $config['url_api'] ?? null, |
128
|
|
|
'resource' => $config['resource'] ?? null, |
129
|
|
|
'API_VERSION' => $config['api_version'] ?? null, |
130
|
|
|
'authWithResource' => $config['auth_with_resource'] ?? null, |
131
|
|
|
'defaultEndPointVersion' => $config['default_end_point_version'] ?? null, |
132
|
|
|
], |
133
|
|
|
}; |
134
|
|
|
|
135
|
|
|
return array_filter($defaults, fn ($value) => null !== $value); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|