2016-09-17 13:22:04 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Controllers\Auth;
|
|
|
|
|
2021-07-17 12:45:00 -04:00
|
|
|
use BookStack\Auth\Access\LoginService;
|
2018-09-25 07:30:50 -04:00
|
|
|
use BookStack\Auth\Access\SocialAuthService;
|
2020-02-01 06:42:22 -05:00
|
|
|
use BookStack\Exceptions\LoginAttemptEmailNeededException;
|
|
|
|
use BookStack\Exceptions\LoginAttemptException;
|
2021-12-11 12:29:33 -05:00
|
|
|
use BookStack\Facades\Activity;
|
2018-09-25 11:58:03 -04:00
|
|
|
use BookStack\Http\Controllers\Controller;
|
2022-09-22 11:54:27 -04:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2016-09-17 13:22:04 -04:00
|
|
|
use Illuminate\Http\Request;
|
2022-09-22 11:54:27 -04:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2021-05-29 18:42:21 -04:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2016-09-17 13:22:04 -04:00
|
|
|
|
|
|
|
class LoginController extends Controller
|
|
|
|
{
|
2022-09-22 11:54:27 -04:00
|
|
|
use ThrottlesLogins;
|
2016-09-17 13:22:04 -04:00
|
|
|
|
2022-06-21 10:32:18 -04:00
|
|
|
protected SocialAuthService $socialAuthService;
|
|
|
|
protected LoginService $loginService;
|
2016-09-17 13:22:04 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*/
|
2021-07-17 12:45:00 -04:00
|
|
|
public function __construct(SocialAuthService $socialAuthService, LoginService $loginService)
|
2016-09-17 13:22:04 -04:00
|
|
|
{
|
2020-02-02 08:10:21 -05:00
|
|
|
$this->middleware('guest', ['only' => ['getLogin', 'login']]);
|
2021-10-11 18:00:45 -04:00
|
|
|
$this->middleware('guard:standard,ldap', ['only' => ['login']]);
|
|
|
|
$this->middleware('guard:standard,ldap,oidc', ['only' => ['logout']]);
|
2020-02-02 08:10:21 -05:00
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
$this->socialAuthService = $socialAuthService;
|
2021-07-17 12:45:00 -04:00
|
|
|
$this->loginService = $loginService;
|
2016-09-17 13:22:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the application login form.
|
|
|
|
*/
|
2017-12-10 08:56:25 -05:00
|
|
|
public function getLogin(Request $request)
|
2016-09-17 13:22:04 -04:00
|
|
|
{
|
|
|
|
$socialDrivers = $this->socialAuthService->getActiveDrivers();
|
|
|
|
$authMethod = config('auth.method');
|
2022-06-21 10:32:18 -04:00
|
|
|
$preventInitiation = $request->get('prevent_auto_init') === 'true';
|
2017-12-10 08:56:25 -05:00
|
|
|
|
|
|
|
if ($request->has('email')) {
|
|
|
|
session()->flashInput([
|
2021-06-26 11:23:15 -04:00
|
|
|
'email' => $request->get('email'),
|
|
|
|
'password' => (config('app.env') === 'demo') ? $request->get('password', '') : '',
|
2017-12-10 08:56:25 -05:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:27:16 -04:00
|
|
|
// Store the previous location for redirect after login
|
2021-08-31 15:54:43 -04:00
|
|
|
$this->updateIntendedFromPrevious();
|
2019-12-14 02:16:48 -05:00
|
|
|
|
2022-06-21 10:32:18 -04:00
|
|
|
if (!$preventInitiation && $this->shouldAutoInitiate()) {
|
|
|
|
return view('auth.login-initiate', [
|
2022-05-02 06:35:11 -04:00
|
|
|
'authMethod' => $authMethod,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-08-05 14:06:39 -04:00
|
|
|
return view('auth.login', [
|
2021-06-26 11:23:15 -04:00
|
|
|
'socialDrivers' => $socialDrivers,
|
|
|
|
'authMethod' => $authMethod,
|
2019-08-05 14:06:39 -04:00
|
|
|
]);
|
2016-09-17 13:22:04 -04:00
|
|
|
}
|
|
|
|
|
2020-02-01 06:42:22 -05:00
|
|
|
/**
|
|
|
|
* Handle a login request to the application.
|
|
|
|
*/
|
|
|
|
public function login(Request $request)
|
|
|
|
{
|
|
|
|
$this->validateLogin($request);
|
2020-07-28 07:59:43 -04:00
|
|
|
$username = $request->get($this->username());
|
2020-02-01 06:42:22 -05:00
|
|
|
|
2022-09-22 11:54:27 -04:00
|
|
|
// Check login throttling attempts to see if they've gone over the limit
|
|
|
|
if ($this->hasTooManyLoginAttempts($request)) {
|
2020-07-28 07:59:43 -04:00
|
|
|
Activity::logFailedLogin($username);
|
2020-02-01 06:42:22 -05:00
|
|
|
return $this->sendLockoutResponse($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($this->attemptLogin($request)) {
|
|
|
|
return $this->sendLoginResponse($request);
|
|
|
|
}
|
|
|
|
} catch (LoginAttemptException $exception) {
|
2020-07-28 07:59:43 -04:00
|
|
|
Activity::logFailedLogin($username);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2020-02-01 06:42:22 -05:00
|
|
|
return $this->sendLoginAttemptExceptionResponse($exception, $request);
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:54:27 -04:00
|
|
|
// On unsuccessful login attempt, Increment login attempts for throttling and log failed login.
|
2020-02-01 06:42:22 -05:00
|
|
|
$this->incrementLoginAttempts($request);
|
2020-07-28 07:59:43 -04:00
|
|
|
Activity::logFailedLogin($username);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2022-09-22 11:54:27 -04:00
|
|
|
// Throw validation failure for failed login
|
|
|
|
throw ValidationException::withMessages([
|
|
|
|
$this->username() => [trans('auth.failed')],
|
|
|
|
])->redirectTo('/login');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logout user and perform subsequent redirect.
|
|
|
|
*/
|
|
|
|
public function logout(Request $request)
|
|
|
|
{
|
|
|
|
Auth::guard()->logout();
|
|
|
|
$request->session()->invalidate();
|
|
|
|
$request->session()->regenerateToken();
|
|
|
|
|
|
|
|
$redirectUri = $this->shouldAutoInitiate() ? '/login?prevent_auto_init=true' : '/';
|
|
|
|
|
|
|
|
return redirect($redirectUri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the expected username input based upon the current auth method.
|
|
|
|
*/
|
|
|
|
protected function username(): string
|
|
|
|
{
|
|
|
|
return config('auth.method') === 'standard' ? 'email' : 'username';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the needed authorization credentials from the request.
|
|
|
|
*/
|
|
|
|
protected function credentials(Request $request): array
|
|
|
|
{
|
|
|
|
return $request->only('username', 'email', 'password');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the response after the user was authenticated.
|
|
|
|
* @return RedirectResponse
|
|
|
|
*/
|
|
|
|
protected function sendLoginResponse(Request $request)
|
|
|
|
{
|
|
|
|
$request->session()->regenerate();
|
|
|
|
$this->clearLoginAttempts($request);
|
|
|
|
|
|
|
|
return redirect()->intended('/');
|
2020-02-01 06:42:22 -05:00
|
|
|
}
|
|
|
|
|
2021-07-17 12:45:00 -04:00
|
|
|
/**
|
|
|
|
* Attempt to log the user into the application.
|
|
|
|
*/
|
2022-09-22 11:54:27 -04:00
|
|
|
protected function attemptLogin(Request $request): bool
|
2021-07-17 12:45:00 -04:00
|
|
|
{
|
|
|
|
return $this->loginService->attempt(
|
2021-08-21 10:49:40 -04:00
|
|
|
$this->credentials($request),
|
|
|
|
auth()->getDefaultDriver(),
|
|
|
|
$request->filled('remember')
|
2021-07-17 12:45:00 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-25 13:19:22 -04:00
|
|
|
|
2020-02-01 09:30:23 -05:00
|
|
|
/**
|
|
|
|
* Validate the user login request.
|
2022-09-22 11:54:27 -04:00
|
|
|
* @throws ValidationException
|
2020-02-01 09:30:23 -05:00
|
|
|
*/
|
2022-09-22 11:54:27 -04:00
|
|
|
protected function validateLogin(Request $request): void
|
2020-02-01 09:30:23 -05:00
|
|
|
{
|
2021-11-04 20:26:55 -04:00
|
|
|
$rules = ['password' => ['required', 'string']];
|
2020-02-01 09:30:23 -05:00
|
|
|
$authMethod = config('auth.method');
|
|
|
|
|
|
|
|
if ($authMethod === 'standard') {
|
2021-11-04 20:26:55 -04:00
|
|
|
$rules['email'] = ['required', 'email'];
|
2020-02-01 09:30:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($authMethod === 'ldap') {
|
2021-11-04 20:26:55 -04:00
|
|
|
$rules['username'] = ['required', 'string'];
|
|
|
|
$rules['email'] = ['email'];
|
2020-02-01 09:30:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$request->validate($rules);
|
|
|
|
}
|
|
|
|
|
2020-02-01 06:42:22 -05:00
|
|
|
/**
|
|
|
|
* Send a response when a login attempt exception occurs.
|
|
|
|
*/
|
|
|
|
protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
|
|
|
|
{
|
|
|
|
if ($exception instanceof LoginAttemptEmailNeededException) {
|
|
|
|
$request->flash();
|
|
|
|
session()->flash('request-email', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($message = $exception->getMessage()) {
|
|
|
|
$this->showWarningNotification($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect('/login');
|
|
|
|
}
|
2021-05-29 18:42:21 -04:00
|
|
|
|
2021-08-31 15:54:43 -04:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2022-05-02 06:35:11 -04:00
|
|
|
|
2022-06-21 10:32:18 -04:00
|
|
|
/**
|
|
|
|
* 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']);
|
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|