Skip to content

Commit

Permalink
Auth: Group OAuth2 providers in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Feb 21, 2025
1 parent 315043f commit 296e58b
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 65 deletions.
4 changes: 2 additions & 2 deletions assets/vue/components/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</div>
</form>

<ExternalLoginButtons />
<LoginOAuth2Buttons />
</div>
</template>

Expand All @@ -80,7 +80,7 @@ import Password from "primevue/password"
import InputSwitch from "primevue/inputswitch"
import { useI18n } from "vue-i18n"
import { useLogin } from "../composables/auth/login"
import ExternalLoginButtons from "./login/LoginExternalButtons.vue"
import LoginOAuth2Buttons from "./login/LoginOAuth2Buttons.vue"
import { usePlatformConfig } from "../store/platformConfig"
const { t } = useI18n()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const platformConfig = usePlatformConfig()

<template>
<div
v-if="platformConfig.externalAuthentication.length > 0"
v-if="platformConfig.oauth2Providers.length > 0"
class="external-logins"
>
<BaseDivider
Expand All @@ -21,7 +21,7 @@ const platformConfig = usePlatformConfig()

<ul class="external-logins__button-list">
<li
v-for="(extAuth, idx) in platformConfig.externalAuthentication"
v-for="(extAuth, idx) in platformConfig.oauth2Providers"
:key="idx"
>
<BaseAppLink
Expand Down
6 changes: 3 additions & 3 deletions assets/vue/store/platformConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const usePlatformConfig = defineStore("platformConfig", () => {
const studentView = ref("teacherview")
const plugins = ref([])
const visualTheme = ref("chamilo")
const externalAuthentication = ref([])
const oauth2Providers = ref([])

async function findSettingsRequest() {
isLoading.value = true
Expand All @@ -24,7 +24,7 @@ export const usePlatformConfig = defineStore("platformConfig", () => {

plugins.value = data.plugins

externalAuthentication.value = data.external_authentication
oauth2Providers.value = data.oauth2_providers
} catch (e) {
console.log(e)
} finally {
Expand All @@ -51,6 +51,6 @@ export const usePlatformConfig = defineStore("platformConfig", () => {
getSetting,
isStudentViewActive,
visualTheme,
externalAuthentication,
oauth2Providers,
}
})
85 changes: 43 additions & 42 deletions config/authentication.dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,49 @@
parameters:
authentication:
default:
generic:
enabled: false
title: 'External'
client_id: ''
client_secret: ''
provider_options:
urlAuthorize: ''
urlAccessToken: ''
urlResourceOwnerDetails: ''
responseResourceOwnerId: 'sub'
allow_create_new_users: true
allow_update_user_info: false
resource_owner_username_field: null
resource_owner_firstname_field: null
resource_owner_lastname_field: null
resource_owner_email_field: null
resource_owner_status_field: null
resource_owner_teacher_status_field: null
resource_owner_sessadmin_status_field: null
resource_owner_hr_status_field: null
resource_owner_status_status_field: null
resource_owner_anon_status_field: null
resource_owner_urls_field: null
oauth2:
generic:
enabled: false
title: 'External'
client_id: ''
client_secret: ''
provider_options:
urlAuthorize: ''
urlAccessToken: ''
urlResourceOwnerDetails: ''
responseResourceOwnerId: 'sub'
allow_create_new_users: true
allow_update_user_info: false
resource_owner_username_field: null
resource_owner_firstname_field: null
resource_owner_lastname_field: null
resource_owner_email_field: null
resource_owner_status_field: null
resource_owner_teacher_status_field: null
resource_owner_sessadmin_status_field: null
resource_owner_hr_status_field: null
resource_owner_status_status_field: null
resource_owner_anon_status_field: null
resource_owner_urls_field: null

facebook:
enabled: false
title: 'Facebook'
client_id: ''
client_secret: ''
#graph_api_version: 'v20.0'
facebook:
enabled: false
title: 'Facebook'
client_id: ''
client_secret: ''
#graph_api_version: 'v20.0'

keycloak:
enabled: false
title: 'Keycloak'
client_id: ''
client_secret: ''
auth_server_url: ''
realm: ''
#version: ''
keycloak:
enabled: false
title: 'Keycloak'
client_id: ''
client_secret: ''
auth_server_url: ''
realm: ''
#version: ''

azure:
enabled: false
title: 'Azure'
client_id: ''
client_secret: ''
azure:
enabled: false
title: 'Azure'
client_id: ''
client_secret: ''
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

abstract class AbstractProviderController extends AbstractController
abstract class AbstractOAuth2ProviderController extends AbstractController
{
protected function getStartResponse(
string $providerName,
ClientRegistry $clientRegistry,
AuthenticationConfigHelper $authenticationConfigHelper,
): Response {
if (!$authenticationConfigHelper->isEnabled($providerName)) {
if (!$authenticationConfigHelper->isOAuth2ProviderEnabled($providerName)) {
throw $this->createAccessDeniedException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class AzureProviderController extends AbstractProviderController
class AzureProviderController extends AbstractOAuth2ProviderController
{
#[Route('/connect/azure', name: 'chamilo.oauth2_azure_start')]
public function connect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class FacebookProviderController extends AbstractProviderController
class FacebookProviderController extends AbstractOAuth2ProviderController
{
#[Route('/connect/facebook', name: 'chamilo.oauth2_facebook_start')]
public function connect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class GenericProviderController extends AbstractProviderController
class GenericProviderController extends AbstractOAuth2ProviderController
{
#[Route('/connect/generic', name: 'chamilo.oauth2_generic_start')]
public function connect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class KeycloakProviderController extends AbstractProviderController
class KeycloakProviderController extends AbstractOAuth2ProviderController
{
#[Route('/connect/keycloak', name: 'chamilo.oauth2_keycloak_start')]
public function connect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function list(SettingsManager $settingsManager): Response
'studentview' => $requestSession->get('studentview'),
'plugins' => [],
'visual_theme' => $this->themeHelper->getVisualTheme(),
'external_authentication' => $this->authenticationConfigHelper->getEnabledProviders(),
'oauth2_providers' => $this->authenticationConfigHelper->getEnabledOAuthProviders(),
];

$configuration['settings']['registration.allow_registration'] = $settingsManager->getSetting('registration.allow_registration', true);
Expand Down
18 changes: 9 additions & 9 deletions src/CoreBundle/ServiceHelper/AuthenticationConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(

public function getProviderConfig(string $providerName, ?AccessUrl $url = null): array
{
$providers = $this->getProvidersForUrl($url);
$providers = $this->getOAuthProvidersForUrl($url);

if ([] === $providers) {
return [];
Expand All @@ -36,16 +36,16 @@ public function getProviderConfig(string $providerName, ?AccessUrl $url = null):
return $providers[$providerName];
}

public function isEnabled(string $methodName, ?AccessUrl $url = null): bool
public function isOAuth2ProviderEnabled(string $methodName, ?AccessUrl $url = null): bool
{
$configParams = $this->getProviderConfig($methodName, $url);

return $configParams['enabled'] ?? false;
}

public function getEnabledProviders(?AccessUrl $url = null): array
public function getEnabledOAuthProviders(?AccessUrl $url = null): array
{
$urlProviders = $this->getProvidersForUrl($url);
$urlProviders = $this->getOAuthProvidersForUrl($url);

$enabledProviders = [];

Expand All @@ -62,20 +62,20 @@ public function getEnabledProviders(?AccessUrl $url = null): array
return $enabledProviders;
}

private function getProvidersForUrl(?AccessUrl $url): array
private function getOAuthProvidersForUrl(?AccessUrl $url): array
{
$urlId = $url ? $url->getId() : $this->urlHelper->getCurrent()->getId();

$authentication = $this->parameterBag->has('authentication')
? $this->parameterBag->get('authentication')
: [];

if (isset($authentication[$urlId])) {
return $authentication[$urlId];
if (isset($authentication[$urlId]['oauth2'])) {
return $authentication[$urlId]['oauth2'];
}

if (isset($authentication['default'])) {
return $authentication['default'];
if (isset($authentication['default']['oauth2'])) {
return $authentication['default']['oauth2'];
}

return [];
Expand Down

0 comments on commit 296e58b

Please sign in to comment.