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

Add a regex to check if auth must be done #7

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Resources/config/packages/univ_lorraine_symfony_cas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;

Expand Down
5 changes: 5 additions & 0 deletions src/Security/CasAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Services/CasAuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}

Expand Down