]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/LoginController.php
Default OpenID display name set to standard value
[bookstack] / app / Http / Controllers / Auth / LoginController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
5 use BookStack\Auth\Access\SocialAuthService;
6 use BookStack\Exceptions\LoginAttemptEmailNeededException;
7 use BookStack\Exceptions\LoginAttemptException;
8 use BookStack\Exceptions\UserRegistrationException;
9 use BookStack\Http\Controllers\Controller;
10 use Illuminate\Foundation\Auth\AuthenticatesUsers;
11 use Illuminate\Http\Request;
12
13 class LoginController extends Controller
14 {
15     /*
16     |--------------------------------------------------------------------------
17     | Login Controller
18     |--------------------------------------------------------------------------
19     |
20     | This controller handles authenticating users for the application and
21     | redirecting them to your home screen. The controller uses a trait
22     | to conveniently provide its functionality to your applications.
23     |
24     */
25
26     use AuthenticatesUsers;
27
28     /**
29      * Redirection paths
30      */
31     protected $redirectTo = '/';
32     protected $redirectPath = '/';
33     protected $redirectAfterLogout = '/login';
34
35     protected $socialAuthService;
36
37     /**
38      * Create a new controller instance.
39      */
40     public function __construct(SocialAuthService $socialAuthService)
41     {
42         $this->middleware('guest', ['only' => ['getLogin', 'login']]);
43         $this->middleware('guard:standard,ldap', ['only' => ['login', 'logout']]);
44
45         $this->socialAuthService = $socialAuthService;
46         $this->redirectPath = url('/');
47         $this->redirectAfterLogout = url('/login');
48         parent::__construct();
49     }
50
51     public function username()
52     {
53         return config('auth.method') === 'standard' ? 'email' : 'username';
54     }
55
56     /**
57      * Get the needed authorization credentials from the request.
58      */
59     protected function credentials(Request $request)
60     {
61         return $request->only('username', 'email', 'password');
62     }
63
64     /**
65      * Show the application login form.
66      */
67     public function getLogin(Request $request)
68     {
69         $socialDrivers = $this->socialAuthService->getActiveDrivers();
70         $authMethod = config('auth.method');
71
72         if ($request->has('email')) {
73             session()->flashInput([
74                 'email' => $request->get('email'),
75                 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
76             ]);
77         }
78
79         $previous = url()->previous('');
80         if (setting('app-public') && $previous && $previous !== url('/login')) {
81             redirect()->setIntendedUrl($previous);
82         }
83
84         return view('auth.login', [
85           'socialDrivers' => $socialDrivers,
86           'authMethod' => $authMethod,
87         ]);
88     }
89
90     /**
91      * Handle a login request to the application.
92      *
93      * @param  \Illuminate\Http\Request  $request
94      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
95      *
96      * @throws \Illuminate\Validation\ValidationException
97      */
98     public function login(Request $request)
99     {
100         $this->validateLogin($request);
101
102         // If the class is using the ThrottlesLogins trait, we can automatically throttle
103         // the login attempts for this application. We'll key this by the username and
104         // the IP address of the client making these requests into this application.
105         if (method_exists($this, 'hasTooManyLoginAttempts') &&
106             $this->hasTooManyLoginAttempts($request)) {
107             $this->fireLockoutEvent($request);
108
109             return $this->sendLockoutResponse($request);
110         }
111
112         try {
113             if ($this->attemptLogin($request)) {
114                 return $this->sendLoginResponse($request);
115             }
116         } catch (LoginAttemptException $exception) {
117             return $this->sendLoginAttemptExceptionResponse($exception, $request);
118         }
119
120         // If the login attempt was unsuccessful we will increment the number of attempts
121         // to login and redirect the user back to the login form. Of course, when this
122         // user surpasses their maximum number of attempts they will get locked out.
123         $this->incrementLoginAttempts($request);
124
125         return $this->sendFailedLoginResponse($request);
126     }
127
128     /**
129      * The user has been authenticated.
130      *
131      * @param  \Illuminate\Http\Request  $request
132      * @param  mixed  $user
133      * @return mixed
134      */
135     protected function authenticated(Request $request, $user)
136     {
137         // Authenticate on all session guards if a likely admin
138         if ($user->can('users-manage') && $user->can('user-roles-manage')) {
139             $guards = ['standard', 'ldap', 'saml2', 'openid'];
140             foreach ($guards as $guard) {
141                 auth($guard)->login($user);
142             }
143         }
144
145         return redirect()->intended($this->redirectPath());
146     }
147
148     /**
149      * Validate the user login request.
150      *
151      * @param  \Illuminate\Http\Request  $request
152      * @return void
153      *
154      * @throws \Illuminate\Validation\ValidationException
155      */
156     protected function validateLogin(Request $request)
157     {
158         $rules = ['password' => 'required|string'];
159         $authMethod = config('auth.method');
160
161         if ($authMethod === 'standard') {
162             $rules['email'] = 'required|email';
163         }
164
165         if ($authMethod === 'ldap') {
166             $rules['username'] = 'required|string';
167             $rules['email'] = 'email';
168         }
169
170         $request->validate($rules);
171     }
172
173     /**
174      * Send a response when a login attempt exception occurs.
175      */
176     protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
177     {
178         if ($exception instanceof LoginAttemptEmailNeededException) {
179             $request->flash();
180             session()->flash('request-email', true);
181         }
182
183         if ($message = $exception->getMessage()) {
184             $this->showWarningNotification($message);
185         }
186
187         return redirect('/login');
188     }
189 }