namespace BookStack\Http\Controllers\Auth;
-use Activity;
use BookStack\Auth\Access\LoginService;
use BookStack\Auth\Access\SocialAuthService;
use BookStack\Exceptions\LoginAttemptEmailNeededException;
use BookStack\Exceptions\LoginAttemptException;
+use BookStack\Facades\Activity;
use BookStack\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
| to conveniently provide its functionality to your applications.
|
*/
-
- use AuthenticatesUsers;
+ use AuthenticatesUsers {
+ logout as traitLogout;
+ }
/**
* Redirection paths.
*/
protected $redirectTo = '/';
protected $redirectPath = '/';
- protected $redirectAfterLogout = '/login';
- protected $socialAuthService;
- protected $loginService;
+ protected SocialAuthService $socialAuthService;
+ protected LoginService $loginService;
/**
* Create a new controller instance.
public function __construct(SocialAuthService $socialAuthService, LoginService $loginService)
{
$this->middleware('guest', ['only' => ['getLogin', 'login']]);
- $this->middleware('guard:standard,ldap', ['only' => ['login', 'logout']]);
+ $this->middleware('guard:standard,ldap', ['only' => ['login']]);
+ $this->middleware('guard:standard,ldap,oidc', ['only' => ['logout']]);
$this->socialAuthService = $socialAuthService;
$this->loginService = $loginService;
$this->redirectPath = url('/');
- $this->redirectAfterLogout = url('/login');
}
public function username()
{
$socialDrivers = $this->socialAuthService->getActiveDrivers();
$authMethod = config('auth.method');
+ $preventInitiation = $request->get('prevent_auto_init') === 'true';
if ($request->has('email')) {
session()->flashInput([
}
// Store the previous location for redirect after login
- $previous = url()->previous('');
- if ($previous && $previous !== url('/login') && setting('app-public')) {
- $isPreviousFromInstance = (strpos($previous, url('/')) === 0);
- if ($isPreviousFromInstance) {
- redirect()->setIntendedUrl($previous);
- }
+ $this->updateIntendedFromPrevious();
+
+ if (!$preventInitiation && $this->shouldAutoInitiate()) {
+ return view('auth.login-initiate', [
+ 'authMethod' => $authMethod,
+ ]);
}
return view('auth.login', [
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
- if (method_exists($this, 'hasTooManyLoginAttempts') &&
- $this->hasTooManyLoginAttempts($request)) {
+ if (
+ method_exists($this, 'hasTooManyLoginAttempts') &&
+ $this->hasTooManyLoginAttempts($request)
+ ) {
$this->fireLockoutEvent($request);
Activity::logFailedLogin($username);
*/
protected function validateLogin(Request $request)
{
- $rules = ['password' => 'required|string'];
+ $rules = ['password' => ['required', 'string']];
$authMethod = config('auth.method');
if ($authMethod === 'standard') {
- $rules['email'] = 'required|email';
+ $rules['email'] = ['required', 'email'];
}
if ($authMethod === 'ldap') {
- $rules['username'] = 'required|string';
- $rules['email'] = 'email';
+ $rules['username'] = ['required', 'string'];
+ $rules['email'] = ['email'];
}
$request->validate($rules);
$this->username() => [trans('auth.failed')],
])->redirectTo('/login');
}
+
+ /**
+ * Update the intended URL location from their previous URL.
+ * Ignores if not from the current app instance or if from certain
+ * login or authentication routes.
+ */
+ protected function updateIntendedFromPrevious(): void
+ {
+ // Store the previous location for redirect after login
+ $previous = url()->previous('');
+ $isPreviousFromInstance = (strpos($previous, url('/')) === 0);
+ if (!$previous || !setting('app-public') || !$isPreviousFromInstance) {
+ return;
+ }
+
+ $ignorePrefixList = [
+ '/login',
+ '/mfa',
+ ];
+
+ foreach ($ignorePrefixList as $ignorePrefix) {
+ if (strpos($previous, url($ignorePrefix)) === 0) {
+ return;
+ }
+ }
+
+ redirect()->setIntendedUrl($previous);
+ }
+
+ /**
+ * Check if login auto-initiate should be valid based upon authentication config.
+ */
+ protected function shouldAutoInitiate(): bool
+ {
+ $socialDrivers = $this->socialAuthService->getActiveDrivers();
+ $authMethod = config('auth.method');
+ $autoRedirect = config('auth.auto_initiate');
+
+ return $autoRedirect && count($socialDrivers) === 0 && in_array($authMethod, ['oidc', 'saml2']);
+ }
+
+ /**
+ * Logout user and perform subsequent redirect.
+ *
+ * @param \Illuminate\Http\Request $request
+ *
+ * @return mixed
+ */
+ public function logout(Request $request)
+ {
+ $this->traitLogout($request);
+
+ $redirectUri = $this->shouldAutoInitiate() ? '/login?prevent_auto_init=true' : '/';
+
+ return redirect($redirectUri);
+ }
}