diff --git a/Resources/config/packages/univ_lorraine_symfony_cas.yaml b/Resources/config/packages/univ_lorraine_symfony_cas.yaml index 82d300a..e690d18 100644 --- a/Resources/config/packages/univ_lorraine_symfony_cas.yaml +++ b/Resources/config/packages/univ_lorraine_symfony_cas.yaml @@ -7,4 +7,4 @@ univ_lorraine_symfony_cas: cas_login_redirect: / # optional (default: /) cas_logout_redirect: ~ # optional (must be a public area) cas_version: "3.0" # optional (default: 2.0) - + public_access_regex : ~ # a regex that match publicly accessible URLs but give user if authenticated diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 0d98328..61a83c7 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -66,6 +66,11 @@ public function getConfigTreeBuilder(): TreeBuilder ->example('2.0') ->info('Version of the CAS Server.') ->end() + ->scalarNode('public_access_regex') + ->defaultValue('') + ->example('#^/(public|other/(sub1|sub2))$#') + ->info('a regex that match publicly accessible URLs but give user if authenticated') + ->end() ->end() ; diff --git a/src/Security/CasAuthenticator.php b/src/Security/CasAuthenticator.php index 9097b4f..0571747 100644 --- a/src/Security/CasAuthenticator.php +++ b/src/Security/CasAuthenticator.php @@ -51,6 +51,11 @@ public function start(Request $request, AuthenticationException $authException = */ public function supports(Request $request): ?bool { + if ($this->casService->public_access_regex + && preg_match($this->casService->public_access_regex, $request->getRequestUri())) { + return false; + } + // If user already connected, skip the CAS auth return !$this->security->getUser(); } diff --git a/src/Services/CasAuthenticationService.php b/src/Services/CasAuthenticationService.php index 607c038..f1e8f56 100644 --- a/src/Services/CasAuthenticationService.php +++ b/src/Services/CasAuthenticationService.php @@ -16,7 +16,7 @@ class CasAuthenticationService private string $cas_login_redirect; private string $cas_logout_redirect; private string $cas_version; - + public string $public_access_regex; public function __construct(array $config, string $env) { @@ -28,6 +28,7 @@ public function __construct(array $config, string $env) $this->cas_login_redirect = ltrim($config['cas_login_redirect'], '/\\'); $this->cas_logout_redirect = $config['cas_logout_redirect'] ?: ''; $this->cas_version = $config['cas_version']; + $this->public_access_regex = $config['public_access_regex'] ?: ''; $this->env = $env; }