33namespace BookStack \Access \Controllers ;
44
55use BookStack \Access \LoginService ;
6- use BookStack \Access \SocialAuthService ;
6+ use BookStack \Access \SocialDriverManager ;
77use BookStack \Exceptions \LoginAttemptEmailNeededException ;
88use BookStack \Exceptions \LoginAttemptException ;
99use BookStack \Facades \Activity ;
1010use BookStack \Http \Controller ;
1111use Illuminate \Http \RedirectResponse ;
1212use Illuminate \Http \Request ;
13- use Illuminate \Support \Facades \Auth ;
1413use Illuminate \Validation \ValidationException ;
1514
1615class LoginController extends Controller
1716{
1817 use ThrottlesLogins;
1918
20- protected SocialAuthService $ socialAuthService ;
21- protected LoginService $ loginService ;
22-
23- /**
24- * Create a new controller instance.
25- */
26- public function __construct (SocialAuthService $ socialAuthService , LoginService $ loginService )
27- {
19+ public function __construct (
20+ protected SocialDriverManager $ socialDriverManager ,
21+ protected LoginService $ loginService ,
22+ ) {
2823 $ this ->middleware ('guest ' , ['only ' => ['getLogin ' , 'login ' ]]);
2924 $ this ->middleware ('guard:standard,ldap ' , ['only ' => ['login ' ]]);
3025 $ this ->middleware ('guard:standard,ldap,oidc ' , ['only ' => ['logout ' ]]);
31-
32- $ this ->socialAuthService = $ socialAuthService ;
33- $ this ->loginService = $ loginService ;
3426 }
3527
3628 /**
3729 * Show the application login form.
3830 */
3931 public function getLogin (Request $ request )
4032 {
41- $ socialDrivers = $ this ->socialAuthService -> getActiveDrivers ();
33+ $ socialDrivers = $ this ->socialDriverManager -> getActive ();
4234 $ authMethod = config ('auth.method ' );
4335 $ preventInitiation = $ request ->get ('prevent_auto_init ' ) === 'true ' ;
4436
@@ -52,7 +44,7 @@ public function getLogin(Request $request)
5244 // Store the previous location for redirect after login
5345 $ this ->updateIntendedFromPrevious ();
5446
55- if (!$ preventInitiation && $ this ->shouldAutoInitiate ()) {
47+ if (!$ preventInitiation && $ this ->loginService -> shouldAutoInitiate ()) {
5648 return view ('auth.login-initiate ' , [
5749 'authMethod ' => $ authMethod ,
5850 ]);
@@ -101,15 +93,9 @@ public function login(Request $request)
10193 /**
10294 * Logout user and perform subsequent redirect.
10395 */
104- public function logout (Request $ request )
96+ public function logout ()
10597 {
106- Auth::guard ()->logout ();
107- $ request ->session ()->invalidate ();
108- $ request ->session ()->regenerateToken ();
109-
110- $ redirectUri = $ this ->shouldAutoInitiate () ? '/login?prevent_auto_init=true ' : '/ ' ;
111-
112- return redirect ($ redirectUri );
98+ return redirect ($ this ->loginService ->logout ());
11399 }
114100
115101 /**
@@ -200,7 +186,7 @@ protected function updateIntendedFromPrevious(): void
200186 {
201187 // Store the previous location for redirect after login
202188 $ previous = url ()->previous ('' );
203- $ isPreviousFromInstance = ( strpos ( $ previous , url ('/ ' )) === 0 );
189+ $ isPreviousFromInstance = str_starts_with ( $ previous , url ('/ ' ));
204190 if (!$ previous || !setting ('app-public ' ) || !$ isPreviousFromInstance ) {
205191 return ;
206192 }
@@ -211,23 +197,11 @@ protected function updateIntendedFromPrevious(): void
211197 ];
212198
213199 foreach ($ ignorePrefixList as $ ignorePrefix ) {
214- if (strpos ($ previous , url ($ ignorePrefix )) === 0 ) {
200+ if (str_starts_with ($ previous , url ($ ignorePrefix ))) {
215201 return ;
216202 }
217203 }
218204
219205 redirect ()->setIntendedUrl ($ previous );
220206 }
221-
222- /**
223- * Check if login auto-initiate should be valid based upon authentication config.
224- */
225- protected function shouldAutoInitiate (): bool
226- {
227- $ socialDrivers = $ this ->socialAuthService ->getActiveDrivers ();
228- $ authMethod = config ('auth.method ' );
229- $ autoRedirect = config ('auth.auto_initiate ' );
230-
231- return $ autoRedirect && count ($ socialDrivers ) === 0 && in_array ($ authMethod , ['oidc ' , 'saml2 ' ]);
232- }
233207}
0 commit comments