Skip to content

Commit 10c4653

Browse files
committed
Logical Theme: Added OIDC_AUTH_PRE_REDIRECT event
Allows customization of the auth URL before the user is redirected to that URL. Related to #6014
1 parent dd42b9b commit 10c4653

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

app/Access/Oidc/OidcService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function login(): array
4949
$url = $provider->getAuthorizationUrl();
5050
session()->put('oidc_pkce_code', $provider->getPkceCode() ?? '');
5151

52+
$returnUrl = Theme::dispatch(ThemeEvents::OIDC_AUTH_PRE_REDIRECT, $url);
53+
if (is_string($returnUrl)) {
54+
$url = $returnUrl;
55+
}
56+
5257
return [
5358
'url' => $url,
5459
'state' => $provider->getState(),

app/Theming/ThemeEvents.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ class ThemeEvents
8787
*/
8888
const COMMONMARK_ENVIRONMENT_CONFIGURE = 'commonmark_environment_configure';
8989

90+
/**
91+
* OIDC auth pre-redirect event.
92+
* Runs just before BookStack redirects the user to the identity provider for authentication.
93+
* Provides the redirect URL that will be used.
94+
* If the listener returns a string value, that will be used as the redirect URL instead.
95+
*
96+
* @param string $redirectUrl
97+
* @return string|null
98+
*/
99+
const OIDC_AUTH_PRE_REDIRECT = 'oidc_auth_pre_redirect';
100+
90101
/**
91102
* OIDC ID token pre-validate event.
92103
* Runs just before BookStack validates the user ID token data upon login.

tests/Auth/OidcTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,34 @@ public function test_oidc_id_token_pre_validate_theme_event_with_return()
822822
]);
823823
}
824824

825+
public function test_oidc_auth_pre_redirect_theme_event_with_return()
826+
{
827+
$args = [];
828+
$callback = function (...$eventArgs) use (&$args) {
829+
$args = $eventArgs;
830+
return 'https://cats.example.com?beans=true';
831+
};
832+
Theme::listen(ThemeEvents::OIDC_AUTH_PRE_REDIRECT, $callback);
833+
834+
$resp = $this->post('/oidc/login');
835+
$resp->assertRedirect('https://cats.example.com?beans=true');
836+
837+
$this->assertCount(1, $args);
838+
$this->assertStringStartsWith('https://oidc.local/auth', $args[0]);
839+
}
840+
841+
public function test_oidc_auth_pre_redirect_theme_event_with_no_return()
842+
{
843+
$callback = function ($redirectUrl) {
844+
$redirectUrl = 'cat';
845+
};
846+
Theme::listen(ThemeEvents::OIDC_AUTH_PRE_REDIRECT, $callback);
847+
848+
$resp = $this->post('/oidc/login');
849+
$redirect = $resp->headers->get('Location');
850+
$this->assertStringStartsWith('https://oidc.local/auth?', $redirect);
851+
}
852+
825853
public function test_pkce_used_on_authorize_and_access()
826854
{
827855
// Start auth

0 commit comments

Comments
 (0)