44
55use Pterodactyl \Models \User ;
66
7- use Validator ;
87use Auth ;
8+ use Alert ;
9+ use Validator ;
910
1011use Pterodactyl \Http \Controllers \Controller ;
1112use PragmaRX \Google2FA \Google2FA ;
@@ -28,73 +29,6 @@ class AuthController extends Controller
2829
2930 use AuthenticatesAndRegistersUsers, ThrottlesLogins;
3031
31- /**
32- * Handle a login request to the application.
33- *
34- * @param \Illuminate\Http\Request $request
35- * @return \Illuminate\Http\Response
36- */
37- public function postLogin (Request $ request )
38- {
39- $ this ->validate ($ request , [
40- $ this ->loginUsername () => 'required ' , 'password ' => 'required ' ,
41- ]);
42-
43- $ throttles = $ this ->isUsingThrottlesLoginsTrait ();
44-
45- if ($ throttles && $ this ->hasTooManyLoginAttempts ($ request )) {
46- return $ this ->sendLockoutResponse ($ request );
47- }
48-
49- $ credentials = $ this ->getCredentials ($ request );
50-
51- if (Auth::attempt ($ credentials , $ request ->has ('remember ' ))) {
52- if (User::select ('id ' )->where ('email ' , $ request ->input ('email ' ))->where ('use_totp ' , 1 )->exists ()) {
53- $ validator = Validator::make ($ request ->all (), [
54- 'totp_token ' => 'required|numeric '
55- ]);
56-
57- if ($ validator ->fails ()) {
58- Auth::logout ();
59- return redirect ('auth/login ' )->withErrors ($ validator )->withInput ();
60- }
61-
62- $ google2fa = new Google2FA ();
63-
64- if ($ google2fa ->verifyKey (User::where ('email ' , $ request ->input ('email ' ))->first ()->totp_secret , $ request ->input ('totp_token ' ))) {
65- return $ this ->handleUserWasAuthenticated ($ request , $ throttles );
66- } else {
67- Auth::logout ();
68- $ validator ->errors ()->add ('field ' , trans ('validation.welcome ' ));
69- return redirect ('auth/login ' )->withErrors ($ validator )->withInput ();
70- }
71- } else {
72- return $ this ->handleUserWasAuthenticated ($ request , $ throttles );
73- }
74- }
75-
76- if ($ throttles ) {
77- $ this ->incrementLoginAttempts ($ request );
78- }
79-
80- return redirect ($ this ->loginPath ())
81- ->withInput ($ request ->only ($ this ->loginUsername (), 'remember ' ))
82- ->withErrors ([
83- $ this ->loginUsername () => $ this ->getFailedLoginMessage (),
84- ]);
85- }
86-
87- /**
88- * Check if the provided user has TOTP enabled.
89- *
90- * @param \Illuminate\Http\Request $request
91- * @return \Illuminate\Http\Response
92- */
93- public function checkTotp (Request $ request )
94- {
95- return response ()->json (User::select ('id ' )->where ('email ' , $ request ->input ('email ' ))->where ('use_totp ' , 1 )->first ());
96- }
97-
9832 /**
9933 * Post-Authentication redirect location.
10034 *
@@ -121,7 +55,7 @@ public function checkTotp(Request $request)
12155 *
12256 * @var integer
12357 */
124- protected $ maxLoginAttempts = 5 ;
58+ protected $ maxLoginAttempts = 3 ;
12559
12660 /**
12761 * Create a new authentication controller instance.
@@ -162,4 +96,70 @@ protected function create(array $data)
16296 ]);
16397 }
16498
99+ /**
100+ * Handle a login request to the application.
101+ *
102+ * @param \Illuminate\Http\Request $request
103+ * @return \Illuminate\Http\Response
104+ */
105+ public function postLogin (Request $ request )
106+ {
107+
108+ $ this ->validate ($ request , [
109+ 'email ' => 'required|email ' ,
110+ 'password ' => 'required ' ,
111+ ]);
112+
113+ $ throttled = $ this ->isUsingThrottlesLoginsTrait ();
114+ if ($ throttled && $ this ->hasTooManyLoginAttempts ($ request )) {
115+ return $ this ->sendLockoutResponse ($ request );
116+ }
117+
118+ $ G2FA = new Google2FA ();
119+ $ user = User::select ('use_totp ' , 'totp_secret ' )->where ('email ' , $ request ->input ($ this ->loginUsername ()))->first ();
120+
121+ // Verify TOTP Token was Valid
122+ if ($ user ->use_totp === 1 ) {
123+ if (!$ G2FA ->verifyKey ($ user ->totp_secret , $ request ->input ('totp_token ' ))) {
124+
125+ if ($ throttled ) {
126+ $ this ->incrementLoginAttempts ($ request );
127+ }
128+
129+ Alert::danger (trans ('auth.totp_failed ' ))->flash ();
130+ return redirect ()->route ('auth.login ' )->withInput ($ request ->only ('email ' , 'remember ' ));
131+
132+ }
133+ }
134+
135+ // Attempt to Login
136+ if (Auth::attempt ([
137+ 'email ' => $ request ->input ('email ' ),
138+ 'password ' => $ request ->input ('password ' )
139+ ], $ request ->has ('remember ' ))) {
140+ return $ this ->handleUserWasAuthenticated ($ request , $ throttled );
141+ }
142+
143+ if ($ throttled ) {
144+ $ this ->incrementLoginAttempts ($ request );
145+ }
146+
147+ return redirect ()->route ('auth.login ' )
148+ ->withInput ($ request ->only ('email ' , 'remember ' ))
149+ ->withErrors ([
150+ 'email ' => $ this ->getFailedLoginMessage (),
151+ ]);
152+ }
153+
154+ /**
155+ * Check if the provided user has TOTP enabled.
156+ *
157+ * @param \Illuminate\Http\Request $request
158+ * @return \Illuminate\Http\Response
159+ */
160+ public function checkTotp (Request $ request )
161+ {
162+ return response ()->json (User::select ('id ' )->where ('email ' , $ request ->input ('email ' ))->where ('use_totp ' , 1 )->first ());
163+ }
164+
165165}
0 commit comments