Skip to content

Commit 09d9582

Browse files
lancepiochDaneEveritt
authored andcommitted
Add togglable 2FA user requirements (pterodactyl#635)
1 parent 55ae913 commit 09d9582

File tree

6 files changed

+140
-2
lines changed

6 files changed

+140
-2
lines changed

app/Http/Controllers/Admin/BaseController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function getSettings()
9393
public function postSettings(BaseFormRequest $request)
9494
{
9595
$this->settings->set('company', $request->input('company'));
96+
$this->settings->set('2fa', $request->input('2fa'));
9697

9798
$this->alert->success('Settings have been successfully updated.')->flash();
9899

app/Http/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Kernel extends HttpKernel
3737
\Pterodactyl\Http\Middleware\VerifyCsrfToken::class,
3838
\Illuminate\Routing\Middleware\SubstituteBindings::class,
3939
\Pterodactyl\Http\Middleware\LanguageMiddleware::class,
40+
\Pterodactyl\Http\Middleware\RequireTwoFactorAuthentication::class,
4041
],
4142
'api' => [
4243
\Pterodactyl\Http\Middleware\HMACAuthorization::class,
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Http\Middleware;
26+
27+
use Closure;
28+
use Krucas\Settings\Settings;
29+
use Prologue\Alerts\AlertsMessageBag;
30+
31+
class RequireTwoFactorAuthentication
32+
{
33+
const LEVEL_NONE = 0;
34+
const LEVEL_ADMIN = 1;
35+
const LEVEL_ALL = 2;
36+
37+
/**
38+
* @var \Prologue\Alerts\AlertsMessageBag
39+
*/
40+
protected $alert;
41+
42+
/**
43+
* @var \Krucas\Settings\Settings
44+
*/
45+
protected $settings;
46+
47+
/**
48+
* All TOTP related routes.
49+
*
50+
* @var array
51+
*/
52+
protected $ignoreRoutes = [
53+
'account.security',
54+
'account.security.revoke',
55+
'account.security.totp',
56+
'account.security.totp.set',
57+
'account.security.totp.disable',
58+
'auth.totp',
59+
'auth.logout',
60+
];
61+
62+
/**
63+
* RequireTwoFactorAuthentication constructor.
64+
*
65+
* @param \Prologue\Alerts\AlertsMessageBag $alert
66+
* @param \Krucas\Settings\Settings $settings
67+
*/
68+
public function __construct(AlertsMessageBag $alert, Settings $settings)
69+
{
70+
$this->alert = $alert;
71+
$this->settings = $settings;
72+
}
73+
74+
/**
75+
* Handle an incoming request.
76+
*
77+
* @param \Illuminate\Http\Request $request
78+
* @param \Closure $next
79+
* @return mixed
80+
*/
81+
public function handle($request, Closure $next)
82+
{
83+
// Ignore non-users
84+
if (! $request->user()) {
85+
return $next($request);
86+
}
87+
88+
// Skip the 2FA pages
89+
if (in_array($request->route()->getName(), $this->ignoreRoutes)) {
90+
return $next($request);
91+
}
92+
93+
// Get the setting
94+
switch ((int) $this->settings->get('2fa', 0)) {
95+
case self::LEVEL_NONE:
96+
return $next($request);
97+
98+
case self::LEVEL_ADMIN:
99+
if (! $request->user()->root_admin) {
100+
return $next($request);
101+
}
102+
break;
103+
104+
case self::LEVEL_ALL:
105+
if ($request->user()->use_totp) {
106+
return $next($request);
107+
}
108+
break;
109+
}
110+
111+
$this->alert->danger('The administrator has required 2FA to be enabled. You must enable it before you can do any other action.')->flash();
112+
113+
return redirect()->route('account.security');
114+
}
115+
}

public/themes/pterodactyl/css/pterodactyl.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ span[aria-labelledby="select2-pUserId-container"] {
346346
line-height: 1.5;
347347
}
348348

349+
.btn.active, .btn.active.focus {
350+
background-color: #408fec;
351+
}
352+
349353
.strong {
350354
font-weight: bold !important;
351355
}

resources/themes/pterodactyl/admin/settings.blade.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@
6666
<p class="text-muted"><small>This is the default language that all clients will use unless they manually change it.</small></p>
6767
</div>
6868
</div> --}}
69+
<div class="form-group col-md-6">
70+
<label class="control-label">2FA Required</label>
71+
<div>
72+
<div class="btn-group" data-toggle="buttons">
73+
<label class="btn btn-primary @if (old('2fa', Settings::get('2fa', 0)) == 0) active @endif">
74+
<input type="radio" name="2fa" autocomplete="off" value="0" @if (old('2fa', Settings::get('2fa', 0)) == 0) checked @endif> Nobody
75+
</label>
76+
<label class="btn btn-primary @if (old('2fa', Settings::get('2fa', 0)) == 1) active @endif">
77+
<input type="radio" name="2fa" autocomplete="off" value="1" @if (old('2fa', Settings::get('2fa', 0)) == 1) checked @endif> Admins
78+
</label>
79+
<label class="btn btn-primary @if (old('2fa', Settings::get('2fa', 0)) == 2) active @endif">
80+
<input type="radio" name="2fa" autocomplete="off" value="2" @if (old('2fa', Settings::get('2fa', 0)) == 2) checked @endif> Everybody
81+
</label>
82+
</div>
83+
<p class="text-muted"><small>Require your administrators or users to have 2FA enabled. Users include Admins. Everybody includes Sub Users.</small></p>
84+
</div>
85+
</div>
6986
</div>
7087
<div class="row">
7188
<div class="col-md-12">

routes/base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
Route::put('/totp', 'SecurityController@generateTotp')->name('account.security.totp');
7171

72-
Route::post('/totp', 'SecurityController@setTotp');
72+
Route::post('/totp', 'SecurityController@setTotp')->name('account.security.totp.set');
7373

74-
Route::delete('/totp', 'SecurityController@disableTotp');
74+
Route::delete('/totp', 'SecurityController@disableTotp')->name('account.security.totp.disable');
7575
});

0 commit comments

Comments
 (0)