2016-09-17 13:22:04 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Controllers\Auth;
|
|
|
|
|
2020-05-23 09:37:38 -04:00
|
|
|
use Activity;
|
2020-11-20 14:33:11 -05:00
|
|
|
use BookStack\Actions\ActivityType;
|
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-03-19 17:54:50 -04:00
|
|
|
use BookStack\Facades\Theme;
|
2018-09-25 11:58:03 -04:00
|
|
|
use BookStack\Http\Controllers\Controller;
|
2021-03-19 17:54:50 -04:00
|
|
|
use BookStack\Theming\ThemeEvents;
|
2016-09-17 13:22:04 -04:00
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
use AuthenticatesUsers;
|
|
|
|
|
|
|
|
/**
|
2020-02-01 06:42:22 -05:00
|
|
|
* Redirection paths
|
2016-09-17 13:22:04 -04:00
|
|
|
*/
|
|
|
|
protected $redirectTo = '/';
|
|
|
|
protected $redirectPath = '/';
|
|
|
|
protected $redirectAfterLogout = '/login';
|
|
|
|
|
|
|
|
protected $socialAuthService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*/
|
2020-02-01 06:42:22 -05:00
|
|
|
public function __construct(SocialAuthService $socialAuthService)
|
2016-09-17 13:22:04 -04:00
|
|
|
{
|
2020-02-02 08:10:21 -05:00
|
|
|
$this->middleware('guest', ['only' => ['getLogin', 'login']]);
|
|
|
|
$this->middleware('guard:standard,ldap', ['only' => ['login', 'logout']]);
|
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
$this->socialAuthService = $socialAuthService;
|
2019-08-04 09:26:39 -04:00
|
|
|
$this->redirectPath = url('/');
|
|
|
|
$this->redirectAfterLogout = url('/login');
|
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');
|
2017-12-10 08:56:25 -05:00
|
|
|
|
|
|
|
if ($request->has('email')) {
|
|
|
|
session()->flashInput([
|
|
|
|
'email' => $request->get('email'),
|
|
|
|
'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:27:16 -04:00
|
|
|
// Store the previous location for redirect after login
|
2020-03-14 14:29:31 -04:00
|
|
|
$previous = url()->previous('');
|
2020-07-28 11:27:16 -04:00
|
|
|
if ($previous && $previous !== url('/login') && setting('app-public')) {
|
|
|
|
$isPreviousFromInstance = (strpos($previous, url('/')) === 0);
|
|
|
|
if ($isPreviousFromInstance) {
|
|
|
|
redirect()->setIntendedUrl($previous);
|
|
|
|
}
|
2019-12-14 02:16:48 -05:00
|
|
|
}
|
|
|
|
|
2019-08-05 14:06:39 -04:00
|
|
|
return view('auth.login', [
|
|
|
|
'socialDrivers' => $socialDrivers,
|
|
|
|
'authMethod' => $authMethod,
|
|
|
|
]);
|
2016-09-17 13:22:04 -04:00
|
|
|
}
|
|
|
|
|
2020-02-01 06:42:22 -05:00
|
|
|
/**
|
|
|
|
* Handle a login request to the application.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
|
|
*/
|
|
|
|
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);
|
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);
|
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);
|
2020-02-01 06:42:22 -05:00
|
|
|
return $this->sendFailedLoginResponse($request);
|
|
|
|
}
|
|
|
|
|
2020-04-25 13:19:22 -04:00
|
|
|
/**
|
|
|
|
* The user has been authenticated.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param mixed $user
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function authenticated(Request $request, $user)
|
|
|
|
{
|
|
|
|
// Authenticate on all session guards if a likely admin
|
|
|
|
if ($user->can('users-manage') && $user->can('user-roles-manage')) {
|
|
|
|
$guards = ['standard', 'ldap', 'saml2'];
|
|
|
|
foreach ($guards as $guard) {
|
|
|
|
auth($guard)->login($user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 17:54:50 -04:00
|
|
|
Theme::dispatch(ThemeEvents::AUTH_LOGIN, auth()->getDefaultDriver(), $user);
|
2020-11-20 14:33:11 -05:00
|
|
|
$this->logActivity(ActivityType::AUTH_LOGIN, $user);
|
2020-04-25 13:19:22 -04:00
|
|
|
return redirect()->intended($this->redirectPath());
|
|
|
|
}
|
|
|
|
|
2020-02-01 09:30:23 -05:00
|
|
|
/**
|
|
|
|
* Validate the user login request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
|
|
*/
|
|
|
|
protected function validateLogin(Request $request)
|
|
|
|
{
|
2020-02-02 05:59:03 -05:00
|
|
|
$rules = ['password' => 'required|string'];
|
2020-02-01 09:30:23 -05:00
|
|
|
$authMethod = config('auth.method');
|
|
|
|
|
|
|
|
if ($authMethod === 'standard') {
|
2020-02-02 05:59:03 -05:00
|
|
|
$rules['email'] = 'required|email';
|
2020-02-01 09:30:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($authMethod === 'ldap') {
|
2020-02-02 05:59:03 -05: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');
|
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|