3 namespace BookStack\Http\Controllers\Auth;
6 use BookStack\Auth\Access\SocialAuthService;
7 use BookStack\Exceptions\LoginAttemptEmailNeededException;
8 use BookStack\Exceptions\LoginAttemptException;
9 use BookStack\Exceptions\UserRegistrationException;
10 use BookStack\Http\Controllers\Controller;
11 use Illuminate\Foundation\Auth\AuthenticatesUsers;
12 use Illuminate\Http\Request;
14 class LoginController extends Controller
17 |--------------------------------------------------------------------------
19 |--------------------------------------------------------------------------
21 | This controller handles authenticating users for the application and
22 | redirecting them to your home screen. The controller uses a trait
23 | to conveniently provide its functionality to your applications.
27 use AuthenticatesUsers;
32 protected $redirectTo = '/';
33 protected $redirectPath = '/';
34 protected $redirectAfterLogout = '/login';
36 protected $socialAuthService;
39 * Create a new controller instance.
41 public function __construct(SocialAuthService $socialAuthService)
43 $this->middleware('guest', ['only' => ['getLogin', 'login']]);
44 $this->middleware('guard:standard,ldap', ['only' => ['login', 'logout']]);
46 $this->socialAuthService = $socialAuthService;
47 $this->redirectPath = url('/');
48 $this->redirectAfterLogout = url('/login');
49 parent::__construct();
52 public function username()
54 return config('auth.method') === 'standard' ? 'email' : 'username';
58 * Get the needed authorization credentials from the request.
60 protected function credentials(Request $request)
62 return $request->only('username', 'email', 'password');
66 * Show the application login form.
68 public function getLogin(Request $request)
70 $socialDrivers = $this->socialAuthService->getActiveDrivers();
71 $authMethod = config('auth.method');
73 if ($request->has('email')) {
74 session()->flashInput([
75 'email' => $request->get('email'),
76 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
80 $previous = url()->previous('');
81 if (setting('app-public') && $previous && $previous !== url('/login')) {
82 redirect()->setIntendedUrl($previous);
85 return view('auth.login', [
86 'socialDrivers' => $socialDrivers,
87 'authMethod' => $authMethod,
92 * Handle a login request to the application.
94 * @param \Illuminate\Http\Request $request
95 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
97 * @throws \Illuminate\Validation\ValidationException
99 public function login(Request $request)
101 $this->validateLogin($request);
102 $username = $request->get($this->username());
104 // If the class is using the ThrottlesLogins trait, we can automatically throttle
105 // the login attempts for this application. We'll key this by the username and
106 // the IP address of the client making these requests into this application.
107 if (method_exists($this, 'hasTooManyLoginAttempts') &&
108 $this->hasTooManyLoginAttempts($request)) {
109 $this->fireLockoutEvent($request);
111 Activity::logFailedLogin($username);
112 return $this->sendLockoutResponse($request);
116 if ($this->attemptLogin($request)) {
117 return $this->sendLoginResponse($request);
119 } catch (LoginAttemptException $exception) {
120 Activity::logFailedLogin($username);
121 return $this->sendLoginAttemptExceptionResponse($exception, $request);
124 // If the login attempt was unsuccessful we will increment the number of attempts
125 // to login and redirect the user back to the login form. Of course, when this
126 // user surpasses their maximum number of attempts they will get locked out.
127 $this->incrementLoginAttempts($request);
129 Activity::logFailedLogin($username);
130 return $this->sendFailedLoginResponse($request);
134 * The user has been authenticated.
136 * @param \Illuminate\Http\Request $request
140 protected function authenticated(Request $request, $user)
142 // Authenticate on all session guards if a likely admin
143 if ($user->can('users-manage') && $user->can('user-roles-manage')) {
144 $guards = ['standard', 'ldap', 'saml2'];
145 foreach ($guards as $guard) {
146 auth($guard)->login($user);
150 return redirect()->intended($this->redirectPath());
154 * Validate the user login request.
156 * @param \Illuminate\Http\Request $request
159 * @throws \Illuminate\Validation\ValidationException
161 protected function validateLogin(Request $request)
163 $rules = ['password' => 'required|string'];
164 $authMethod = config('auth.method');
166 if ($authMethod === 'standard') {
167 $rules['email'] = 'required|email';
170 if ($authMethod === 'ldap') {
171 $rules['username'] = 'required|string';
172 $rules['email'] = 'email';
175 $request->validate($rules);
179 * Send a response when a login attempt exception occurs.
181 protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
183 if ($exception instanceof LoginAttemptEmailNeededException) {
185 session()->flash('request-email', true);
188 if ($message = $exception->getMessage()) {
189 $this->showWarningNotification($message);
192 return redirect('/login');