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;
|
2016-09-17 13:22:04 -04:00
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
|
|
|
use Illuminate\Http\Request;
|
2021-05-29 18:42:21 -04:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2016-09-17 13:22:04 -04:00
|
|
|
|
|
|
|
class LoginController extends Controller
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Login Controller
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| This controller handles authenticating users for the application and
|
|
|
|
| redirecting them to your home screen. The controller uses a trait
|
|
|
|
| to conveniently provide its functionality to your applications.
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2022-05-02 06:35:11 -04:00
|
|
|
use AuthenticatesUsers { logout as traitLogout; }
|
2016-09-17 13:22:04 -04:00
|
|
|
|
|
|
|
/**
|
2021-06-26 11:23:15 -04:00
|
|
|
* Redirection paths.
|
2016-09-17 13:22:04 -04:00
|
|
|
*/
|
|
|
|
protected $redirectTo = '/';
|
|
|
|
protected $redirectPath = '/';
|
2022-05-02 06:35:11 -04:00
|
|
|
protected $redirectAfterLogout = '/';
|
2016-09-17 13:22:04 -04:00
|
|
|
|
|
|
|
protected $socialAuthService;
|
2021-07-17 12:45:00 -04:00
|
|
|
protected $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;
|
|
|
|
|
2019-08-04 09:26:39 -04:00
|
|
|
$this->redirectPath = url('/');
|
2022-05-02 06:35:11 -04:00
|
|
|
$this->redirectAfterLogout = url(config('auth.auto_redirect') ? '/login?logout=1' : '/');
|
2016-09-17 13:22:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function username()
|
|
|
|
{
|
|
|
|
return config('auth.method') === 'standard' ? 'email' : 'username';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-01 06:42:22 -05:00
|
|
|
* Get the needed authorization credentials from the request.
|
2016-09-17 13:22:04 -04:00
|
|
|
*/
|
2020-02-01 06:42:22 -05:00
|
|
|
protected function credentials(Request $request)
|
2016-09-17 13:22:04 -04:00
|
|
|
{
|
2020-02-01 06:42:22 -05:00
|
|
|
return $request->only('username', 'email', 'password');
|
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-05-02 06:35:11 -04:00
|
|
|
$autoRedirect = config('auth.auto_redirect');
|
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-05-02 06:35:11 -04:00
|
|
|
if ($autoRedirect && !($request->has('logout') && $request->get('logout') == '1') && count($socialDrivers) == 0 && in_array($authMethod, ['oidc', 'saml2'])) {
|
|
|
|
return view('auth.login-redirect', [
|
|
|
|
'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.
|
|
|
|
*
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2020-02-01 06:42:22 -05:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
|
2020-02-01 06:42:22 -05:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
// 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)) {
|
|
|
|
$this->fireLockoutEvent($request);
|
|
|
|
|
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->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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the login attempt was unsuccessful we will increment the number of attempts
|
|
|
|
// to login and redirect the user back to the login form. Of course, when this
|
|
|
|
// user surpasses their maximum number of attempts they will get locked out.
|
|
|
|
$this->incrementLoginAttempts($request);
|
|
|
|
|
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->sendFailedLoginResponse($request);
|
|
|
|
}
|
|
|
|
|
2021-07-17 12:45:00 -04:00
|
|
|
/**
|
|
|
|
* Attempt to log the user into the application.
|
|
|
|
*
|
2021-08-21 10:49:40 -04:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*
|
2021-07-17 12:45:00 -04:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function attemptLogin(Request $request)
|
|
|
|
{
|
|
|
|
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
|
|
|
/**
|
|
|
|
* The user has been authenticated.
|
|
|
|
*
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param mixed $user
|
|
|
|
*
|
2020-04-25 13:19:22 -04:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function authenticated(Request $request, $user)
|
|
|
|
{
|
|
|
|
return redirect()->intended($this->redirectPath());
|
|
|
|
}
|
|
|
|
|
2020-02-01 09:30:23 -05:00
|
|
|
/**
|
|
|
|
* Validate the user login request.
|
|
|
|
*
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2020-02-01 09:30:23 -05:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
|
|
|
* @return void
|
2020-02-01 09:30:23 -05:00
|
|
|
*/
|
|
|
|
protected function validateLogin(Request $request)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the failed login response instance.
|
|
|
|
*
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2021-05-29 18:42:21 -04:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
2021-05-29 18:42:21 -04:00
|
|
|
*/
|
|
|
|
protected function sendFailedLoginResponse(Request $request)
|
|
|
|
{
|
|
|
|
throw ValidationException::withMessages([
|
|
|
|
$this->username() => [trans('auth.failed')],
|
|
|
|
])->redirectTo('/login');
|
|
|
|
}
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Logout user and perform subsequent redirect.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function logout(Request $request)
|
|
|
|
{
|
|
|
|
$this->traitLogout($request);
|
|
|
|
|
|
|
|
return redirect($this->redirectAfterLogout);
|
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|