-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathUser2FAController.php
263 lines (220 loc) · 7.1 KB
/
User2FAController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
namespace IXP\Http\Controllers\User;
/*
* Copyright (C) 2009 - 2019 Internet Neutral Exchange Association Company Limited By Guarantee.
* All Rights Reserved.
*
* This file is part of IXP Manager.
*
* IXP Manager is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version v2.0 of the License.
*
* IXP Manager is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License v2.0
* along with IXP Manager. If not, see:
*
* http://www.gnu.org/licenses/gpl-2.0.html
*/
use Auth, D2EM, Hash, Redirect;
use Illuminate\Auth\Recaller;
use Illuminate\Contracts\View\Factory;
use Entities\{
User2FA as User2FAEntity,
User as UserEntity
};
use Illuminate\Http\{
RedirectResponse,
Request
};
use Illuminate\View\View;
use PragmaRX\Google2FALaravel\Support\Authenticator as GoogleAuthenticator;
use IXP\Utils\View\Alert\{
Alert,
Container as AlertContainer
};
use IXP\Http\Controllers\Controller;
use IXP\Exceptions\User2FAException;
/**
* Security Password Controller
*
* @author Yann Robin <yann@islandbridgenetworks.ie>
* @author Barry O'Donovan <barry@islandbridgenetworks.ie>
*
* @category Admin
* @copyright Copyright (C) 2009 - 2019 Internet Neutral Exchange Association Company Limited By Guarantee
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL V2.0
*/
class User2FAController extends Controller
{
/**
* Configure 2FA
*
* @param Request $request
* @return Factory|View
*/
public function configure( Request $request )
{
if( !$request->user()->getUser2FA() ) {
$this->generateUser2FA( $request->user() );
}
return view( 'user/2fa/configure' )->with([
'user' => $request->user(),
'qrcode' => $this->generateQRCode( $request->user() ),
]);
}
/**
* Enable 2FA for a user
*
* @param Request $request
* @return RedirectResponse|View
* @throws
*/
public function enable( Request $request )
{
if( !$this->checkUserPassword( $request ) || !$this->testOneTimeCode( $request ) ) {
return redirect(route('2fa@configure'));
}
$request->user()->getUser2FA()->setEnabled( true );
// We also need to mark the current session as 2fa complete:
if( $r = $request->cookies->get(Auth::getRecallerName()) ) {
$recaller = new Recaller($r);
$urt = d2r( 'UserRememberToken' )->findOneBy( [ 'token' => $recaller->token() ] );
if( $urt ) {
$urt->setIs2faComplete(true);
}
}
D2EM::flush();
$this->google2faLogin($request);
AlertContainer::push( "2FA successfully enabled.", Alert::SUCCESS );
return Redirect::to('');
}
/**
* Delete a user's 2fa configuration
*
* @param Request $request
* @return RedirectResponse|View
* @throws
*/
public function delete( Request $request )
{
if( !( $user = d2r('User')->find( $request->input('id') ) ) ) {
abort( 404, 'Unknown user' );
}
if( !$request->user()->isSuperUser() ) {
abort( 403, 'You are not authorised to perform this action' );
}
D2EM::remove( $user->getUser2FA() );
$user->setUser2FA(null);
D2EM::flush();
AlertContainer::push( "2FA deleted for " . $user->getUsername() . ".", Alert::SUCCESS );
return Redirect::back();
}
/**
* Enable 2FA for a user
*
* @param Request $request
* @return RedirectResponse|View
* @throws
*/
public function disable( Request $request )
{
if( !$this->checkUserPassword( $request ) ) {
return redirect(route('2fa@configure'));
}
D2EM::remove( $request->user()->getUser2FA() );
$request->user()->setUser2FA(null);
D2EM::flush();
$this->google2faLogin($request, false);
AlertContainer::push( "2FA successfully disable.", Alert::SUCCESS );
return Redirect::to(route('profile@edit'));
}
/**
* Create Password Security if needed and generate a QR code
*
* @param UserEntity $user
*
* @return void
*
* @throws
*/
private function generateUser2FA( UserEntity $user )
{
$google2fa = app( 'pragmarx.google2fa' );
if( !$user->getUser2FA() ) {
$u2fa = new User2FAEntity;
$u2fa->setUser( $user );
$u2fa->setSecret( $google2fa->generateSecretKey( 32 ) );
$u2fa->setEnabled( false );
D2EM::persist( $u2fa );
D2EM::flush();
// Refresh the user object in order to get the 2FA secret code to generate the qr code
D2EM::refresh( $user );
}
}
/**
* Get a QR Code object for the user's 2fa settings
*
* @param UserEntity $user
* @return mixed
*/
private function generateQRCode( UserEntity $user )
{
$google2fa = app( 'pragmarx.google2fa' );
return $google2fa->getQRCodeInline(
config( 'identity.sitename' ),
$user->getEmail(),
$user->getUser2FA()->getSecret()
);
}
/**
* Check if the user password is valid
*
* @param Request $request
* @return bool
*/
private function checkUserPassword( $request ): bool
{
if( !Hash::check( $request->input('password'), $request->user()->getPassword() ) ) {
AlertContainer::push( 'Incorrect user password - please check your password and try again.', Alert::DANGER );
return false;
}
return true;
}
/**
* Test if the one time code is valid
*
* @param Request $request
* @return bool
* @throws User2FAException
*/
private function testOneTimeCode( Request $request )
{
if( !$request->user()->getUser2FA() ) {
throw new User2FAException('Attempt to test OTC but no 2FA record exists');
}
$google2fa = app( 'pragmarx.google2fa' );
if( !$google2fa->verifyKey( $request->user()->getUser2FA()->getSecret(), $request->input('one_time_password') ) ) {
AlertContainer::push( "Incorrect one time code - please check your code and try again.", Alert::DANGER );
return false;
}
return true;
}
/**
* Login or logout via the 2FA authenticator.
*
* This essentially decides whether the 2fa middleware will look for a 2fa code on the next request.
*
* @param $request
* @param bool $login
*/
private function google2faLogin( $request, bool $login = true )
{
$authenticator = new GoogleAuthenticator($request);
$login ? $authenticator->login() : $authenticator->logout();
}
}