File tree Expand file tree Collapse file tree 3 files changed +44
-0
lines changed
Expand file tree Collapse file tree 3 files changed +44
-0
lines changed Original file line number Diff line number Diff 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 (),
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments