3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Auth\Access\LoginService;
6 use BookStack\Auth\Access\SocialAuthService;
7 use BookStack\Exceptions\LoginAttemptEmailNeededException;
8 use BookStack\Exceptions\LoginAttemptException;
9 use BookStack\Facades\Activity;
10 use BookStack\Http\Controllers\Controller;
11 use Illuminate\Foundation\Auth\AuthenticatesUsers;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
15 class LoginController extends Controller
18 |--------------------------------------------------------------------------
20 |--------------------------------------------------------------------------
22 | This controller handles authenticating users for the application and
23 | redirecting them to your home screen. The controller uses a trait
24 | to conveniently provide its functionality to your applications.
28 use AuthenticatesUsers;
33 protected $redirectTo = '/';
34 protected $redirectPath = '/';
35 protected $redirectAfterLogout = '/login';
37 protected $socialAuthService;
38 protected $loginService;
41 * Create a new controller instance.
43 public function __construct(SocialAuthService $socialAuthService, LoginService $loginService)
45 $this->middleware('guest', ['only' => ['getLogin', 'login']]);
46 $this->middleware('guard:standard,ldap', ['only' => ['login']]);
47 $this->middleware('guard:standard,ldap,oidc', ['only' => ['logout']]);
49 $this->socialAuthService = $socialAuthService;
50 $this->loginService = $loginService;
52 $this->redirectPath = url('/');
53 $this->redirectAfterLogout = url('/login');
56 public function username()
58 return config('auth.method') === 'standard' ? 'email' : 'username';
62 * Get the needed authorization credentials from the request.
64 protected function credentials(Request $request)
66 return $request->only('username', 'email', 'password');
70 * Show the application login form.
72 public function getLogin(Request $request)
74 $socialDrivers = $this->socialAuthService->getActiveDrivers();
75 $authMethod = config('auth.method');
77 if ($request->has('email')) {
78 session()->flashInput([
79 'email' => $request->get('email'),
80 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : '',
84 // Store the previous location for redirect after login
85 $this->updateIntendedFromPrevious();
87 return view('auth.login', [
88 'socialDrivers' => $socialDrivers,
89 'authMethod' => $authMethod,
94 * Handle a login request to the application.
96 * @param \Illuminate\Http\Request $request
98 * @throws \Illuminate\Validation\ValidationException
100 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
102 public function login(Request $request)
104 $this->validateLogin($request);
105 $username = $request->get($this->username());
107 // If the class is using the ThrottlesLogins trait, we can automatically throttle
108 // the login attempts for this application. We'll key this by the username and
109 // the IP address of the client making these requests into this application.
110 if (method_exists($this, 'hasTooManyLoginAttempts') &&
111 $this->hasTooManyLoginAttempts($request)) {
112 $this->fireLockoutEvent($request);
114 Activity::logFailedLogin($username);
116 return $this->sendLockoutResponse($request);
120 if ($this->attemptLogin($request)) {
121 return $this->sendLoginResponse($request);
123 } catch (LoginAttemptException $exception) {
124 Activity::logFailedLogin($username);
126 return $this->sendLoginAttemptExceptionResponse($exception, $request);
129 // If the login attempt was unsuccessful we will increment the number of attempts
130 // to login and redirect the user back to the login form. Of course, when this
131 // user surpasses their maximum number of attempts they will get locked out.
132 $this->incrementLoginAttempts($request);
134 Activity::logFailedLogin($username);
136 return $this->sendFailedLoginResponse($request);
140 * Attempt to log the user into the application.
142 * @param \Illuminate\Http\Request $request
146 protected function attemptLogin(Request $request)
148 return $this->loginService->attempt(
149 $this->credentials($request),
150 auth()->getDefaultDriver(),
151 $request->filled('remember')
156 * The user has been authenticated.
158 * @param \Illuminate\Http\Request $request
163 protected function authenticated(Request $request, $user)
165 return redirect()->intended($this->redirectPath());
169 * Validate the user login request.
171 * @param \Illuminate\Http\Request $request
173 * @throws \Illuminate\Validation\ValidationException
177 protected function validateLogin(Request $request)
179 $rules = ['password' => ['required', 'string']];
180 $authMethod = config('auth.method');
182 if ($authMethod === 'standard') {
183 $rules['email'] = ['required', 'email'];
186 if ($authMethod === 'ldap') {
187 $rules['username'] = ['required', 'string'];
188 $rules['email'] = ['email'];
191 $request->validate($rules);
195 * Send a response when a login attempt exception occurs.
197 protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
199 if ($exception instanceof LoginAttemptEmailNeededException) {
201 session()->flash('request-email', true);
204 if ($message = $exception->getMessage()) {
205 $this->showWarningNotification($message);
208 return redirect('/login');
212 * Get the failed login response instance.
214 * @param \Illuminate\Http\Request $request
216 * @throws \Illuminate\Validation\ValidationException
218 * @return \Symfony\Component\HttpFoundation\Response
220 protected function sendFailedLoginResponse(Request $request)
222 throw ValidationException::withMessages([
223 $this->username() => [trans('auth.failed')],
224 ])->redirectTo('/login');
228 * Update the intended URL location from their previous URL.
229 * Ignores if not from the current app instance or if from certain
230 * login or authentication routes.
232 protected function updateIntendedFromPrevious(): void
234 // Store the previous location for redirect after login
235 $previous = url()->previous('');
236 $isPreviousFromInstance = (strpos($previous, url('/')) === 0);
237 if (!$previous || !setting('app-public') || !$isPreviousFromInstance) {
241 $ignorePrefixList = [
246 foreach ($ignorePrefixList as $ignorePrefix) {
247 if (strpos($previous, url($ignorePrefix)) === 0) {
252 redirect()->setIntendedUrl($previous);