3 namespace BookStack\Http\Controllers\Auth;
6 use BookStack\Auth\Access\LoginService;
7 use BookStack\Auth\Access\SocialAuthService;
8 use BookStack\Exceptions\LoginAttemptEmailNeededException;
9 use BookStack\Exceptions\LoginAttemptException;
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', 'logout']]);
48 $this->socialAuthService = $socialAuthService;
49 $this->loginService = $loginService;
51 $this->redirectPath = url('/');
52 $this->redirectAfterLogout = url('/login');
55 public function username()
57 return config('auth.method') === 'standard' ? 'email' : 'username';
61 * Get the needed authorization credentials from the request.
63 protected function credentials(Request $request)
65 return $request->only('username', 'email', 'password');
69 * Show the application login form.
71 public function getLogin(Request $request)
73 $socialDrivers = $this->socialAuthService->getActiveDrivers();
74 $authMethod = config('auth.method');
76 if ($request->has('email')) {
77 session()->flashInput([
78 'email' => $request->get('email'),
79 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : '',
83 // Store the previous location for redirect after login
84 $this->updateIntendedFromPrevious();
86 return view('auth.login', [
87 'socialDrivers' => $socialDrivers,
88 'authMethod' => $authMethod,
93 * Handle a login request to the application.
95 * @param \Illuminate\Http\Request $request
97 * @throws \Illuminate\Validation\ValidationException
99 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
101 public function login(Request $request)
103 $this->validateLogin($request);
104 $username = $request->get($this->username());
106 // If the class is using the ThrottlesLogins trait, we can automatically throttle
107 // the login attempts for this application. We'll key this by the username and
108 // the IP address of the client making these requests into this application.
109 if (method_exists($this, 'hasTooManyLoginAttempts') &&
110 $this->hasTooManyLoginAttempts($request)) {
111 $this->fireLockoutEvent($request);
113 Activity::logFailedLogin($username);
115 return $this->sendLockoutResponse($request);
119 if ($this->attemptLogin($request)) {
120 return $this->sendLoginResponse($request);
122 } catch (LoginAttemptException $exception) {
123 Activity::logFailedLogin($username);
125 return $this->sendLoginAttemptExceptionResponse($exception, $request);
128 // If the login attempt was unsuccessful we will increment the number of attempts
129 // to login and redirect the user back to the login form. Of course, when this
130 // user surpasses their maximum number of attempts they will get locked out.
131 $this->incrementLoginAttempts($request);
133 Activity::logFailedLogin($username);
135 return $this->sendFailedLoginResponse($request);
139 * Attempt to log the user into the application.
141 * @param \Illuminate\Http\Request $request
145 protected function attemptLogin(Request $request)
147 return $this->loginService->attempt(
148 $this->credentials($request),
149 auth()->getDefaultDriver(),
150 $request->filled('remember')
155 * The user has been authenticated.
157 * @param \Illuminate\Http\Request $request
162 protected function authenticated(Request $request, $user)
164 return redirect()->intended($this->redirectPath());
168 * Validate the user login request.
170 * @param \Illuminate\Http\Request $request
172 * @throws \Illuminate\Validation\ValidationException
176 protected function validateLogin(Request $request)
178 $rules = ['password' => 'required|string'];
179 $authMethod = config('auth.method');
181 if ($authMethod === 'standard') {
182 $rules['email'] = 'required|email';
185 if ($authMethod === 'ldap') {
186 $rules['username'] = 'required|string';
187 $rules['email'] = 'email';
190 $request->validate($rules);
194 * Send a response when a login attempt exception occurs.
196 protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
198 if ($exception instanceof LoginAttemptEmailNeededException) {
200 session()->flash('request-email', true);
203 if ($message = $exception->getMessage()) {
204 $this->showWarningNotification($message);
207 return redirect('/login');
211 * Get the failed login response instance.
213 * @param \Illuminate\Http\Request $request
215 * @throws \Illuminate\Validation\ValidationException
217 * @return \Symfony\Component\HttpFoundation\Response
219 protected function sendFailedLoginResponse(Request $request)
221 throw ValidationException::withMessages([
222 $this->username() => [trans('auth.failed')],
223 ])->redirectTo('/login');
227 * Update the intended URL location from their previous URL.
228 * Ignores if not from the current app instance or if from certain
229 * login or authentication routes.
231 protected function updateIntendedFromPrevious(): void
233 // Store the previous location for redirect after login
234 $previous = url()->previous('');
235 $isPreviousFromInstance = (strpos($previous, url('/')) === 0);
236 if (!$previous || !setting('app-public') || !$isPreviousFromInstance) {
240 $ignorePrefixList = [
245 foreach ($ignorePrefixList as $ignorePrefix) {
246 if (strpos($previous, url($ignorePrefix)) === 0) {
251 redirect()->setIntendedUrl($previous);