Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow OAuth providers to pass additional options to provider constructor #3062

Merged
merged 3 commits into from
Sep 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions core/classes/Misc/NamelessOAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ public function __construct() {
* @param string $name The name of the provider (Discord, Google, etc).
* @param string $module Name of the module which registered this provider.
* @param array $data Metadata about the provider: class, user_id_name, scope_id_name, icon
* @param array $extra_options Extra options to pass to the provider constructor. Example: keycloak needs some specific options
*/
public function registerProvider(string $name, string $module, array $data): void {
$this->_providers[$name] = array_merge(['module' => $module], $data);
public function registerProvider(string $name, string $module, array $data, array $extra_options = []): void {
$this->_providers[$name] = array_merge(['module' => $module, 'extra_options' => $extra_options], $data);
}

/**
Expand Down Expand Up @@ -90,6 +91,10 @@ public function getProvidersAvailable(): array {
* @return AbstractProvider The provider instance
*/
public function getProviderInstance(string $provider): AbstractProvider {
if (!array_key_exists($provider, $this->_providers)) {
throw new RuntimeException("Unknown provider: $provider");
}

[$clientId, $clientSecret] = $this->getCredentials($provider);
$url = rtrim(URL::getSelfURL(), '/') . URL::build('/oauth', "provider=" . urlencode($provider), 'non-friendly');
$options = [
Expand All @@ -98,11 +103,9 @@ public function getProviderInstance(string $provider): AbstractProvider {
'redirectUri' => $url,
];

if (array_key_exists($provider, $this->_providers)) {
return $this->_provider_instances[$provider] ??= new $this->_providers[$provider]['class']($options);
}
$options = array_merge($options, $this->_providers[$provider]['extra_options']);

throw new RuntimeException("Unknown provider: $provider");
return $this->_provider_instances[$provider] ??= new $this->_providers[$provider]['class']($options);
}

/**
Expand Down