Skip to content

Commit 79decaf

Browse files
committed
Update all the middlewares
1 parent e0d0351 commit 79decaf

16 files changed

+161
-100
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
2828
* Server creation page now only asks for a node to deploy to, rather than requiring a location and then a node.
2929
* Database passwords are now hidden by default and will only show if clicked on. In addition, database view in ACP now indicates that passwords must be viewed on the front-end.
3030
* Localhost cannot be used as a connection address in the environment configuration script. `127.0.0.1` is allowed.
31+
* Application locale can now be quickly set using an environment variable `APP_LOCALE` rather than having to edit core files.
3132

3233
### Fixed
3334
* Unable to change the daemon secret for a server via the Admin CP.

app/Http/Kernel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Foundation\Http\Kernel as HttpKernel;
77
use Illuminate\Routing\Middleware\SubstituteBindings;
88
use Pterodactyl\Http\Middleware\AccessingValidServer;
9+
use Pterodactyl\Http\Middleware\Server\AuthenticateAsSubuser;
910
use Pterodactyl\Http\Middleware\Server\SubuserBelongsToServer;
1011
use Pterodactyl\Http\Middleware\Server\DatabaseBelongsToServer;
1112
use Pterodactyl\Http\Middleware\Server\ScheduleBelongsToServer;
@@ -66,7 +67,7 @@ class Kernel extends HttpKernel
6667
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
6768
'guest' => \Pterodactyl\Http\Middleware\RedirectIfAuthenticated::class,
6869
'server' => AccessingValidServer::class,
69-
'subuser.auth' => \Pterodactyl\Http\Middleware\SubuserAccessAuthenticate::class,
70+
'subuser.auth' => AuthenticateAsSubuser::class,
7071
'admin' => \Pterodactyl\Http\Middleware\AdminAuthenticate::class,
7172
'daemon-old' => DaemonAuthenticate::class,
7273
'csrf' => \Pterodactyl\Http\Middleware\VerifyCsrfToken::class,

app/Http/Middleware/AdminAuthenticate.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace Pterodactyl\Http\Middleware;
1111

1212
use Closure;
13+
use Illuminate\Http\Request;
14+
use Symfony\Component\HttpKernel\Exception\HttpException;
1315

1416
class AdminAuthenticate
1517
{
@@ -20,18 +22,10 @@ class AdminAuthenticate
2022
* @param \Closure $next
2123
* @return mixed
2224
*/
23-
public function handle($request, Closure $next)
25+
public function handle(Request $request, Closure $next)
2426
{
25-
if (! $request->user()) {
26-
if ($request->expectsJson() || $request->json()) {
27-
return response('Unauthorized.', 401);
28-
} else {
29-
return redirect()->guest('auth/login');
30-
}
31-
}
32-
33-
if (! $request->user()->root_admin) {
34-
return abort(403);
27+
if (! $request->user() || ! $request->user()->root_admin) {
28+
throw new HttpException(403, 'Access Denied');
3529
}
3630

3731
return $next($request);

app/Http/Middleware/Authenticate.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pterodactyl\Http\Middleware;
44

55
use Closure;
6+
use Illuminate\Http\Request;
67
use Illuminate\Contracts\Auth\Guard;
78

89
class Authenticate
@@ -31,7 +32,7 @@ public function __construct(Guard $auth)
3132
* @param \Closure $next
3233
* @return mixed
3334
*/
34-
public function handle($request, Closure $next)
35+
public function handle(Request $request, Closure $next)
3536
{
3637
if ($this->auth->guest()) {
3738
if ($request->ajax()) {

app/Http/Middleware/Daemon/DaemonAuthenticate.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@
3333
class DaemonAuthenticate
3434
{
3535
/**
36+
* Daemon routes that this middleware should be skipped on.
3637
* @var array
3738
*/
38-
protected $except = ['daemon.configuration'];
39+
protected $except = [
40+
'daemon.configuration',
41+
];
3942

4043
/**
4144
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
@@ -63,6 +66,10 @@ public function __construct(NodeRepositoryInterface $repository)
6366
*/
6467
public function handle(Request $request, Closure $next)
6568
{
69+
if (in_array($request->route()->getName(), $this->except)) {
70+
return $next($request);
71+
}
72+
6673
$token = $request->bearerToken();
6774

6875
if (is_null($token)) {

app/Http/Middleware/DaemonAuthenticate.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,36 @@
1010
namespace Pterodactyl\Http\Middleware;
1111

1212
use Closure;
13+
use Illuminate\Http\Request;
1314
use Pterodactyl\Models\Node;
14-
use Illuminate\Contracts\Auth\Guard;
15+
use Symfony\Component\HttpKernel\Exception\HttpException;
16+
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
17+
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
1518

1619
class DaemonAuthenticate
1720
{
18-
/**
19-
* The Guard implementation.
20-
*
21-
* @var \Illuminate\Contracts\Auth\Guard
22-
*/
23-
protected $auth;
24-
2521
/**
2622
* An array of route names to not apply this middleware to.
2723
*
2824
* @var array
2925
*/
30-
protected $except = [
26+
private $except = [
3127
'daemon.configuration',
3228
];
3329

30+
/**
31+
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
32+
*/
33+
private $repository;
34+
3435
/**
3536
* Create a new filter instance.
3637
*
37-
* @param \Illuminate\Contracts\Auth\Guard $auth
38+
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository
3839
*/
39-
public function __construct(Guard $auth)
40+
public function __construct(NodeRepositoryInterface $repository)
4041
{
41-
$this->auth = $auth;
42+
$this->repository = $repository;
4243
}
4344

4445
/**
@@ -48,21 +49,24 @@ public function __construct(Guard $auth)
4849
* @param \Closure $next
4950
* @return mixed
5051
*/
51-
public function handle($request, Closure $next)
52+
public function handle(Request $request, Closure $next)
5253
{
5354
if (in_array($request->route()->getName(), $this->except)) {
5455
return $next($request);
5556
}
5657

5758
if (! $request->header('X-Access-Node')) {
58-
return abort(403);
59+
throw new HttpException(403);
5960
}
6061

61-
$node = Node::where('daemonSecret', $request->header('X-Access-Node'))->first();
62-
if (! $node) {
63-
return abort(401);
62+
try {
63+
$node = $this->repository->findWhere(['daemonSecret' => $request->header('X-Access-Node')]);
64+
} catch (RecordNotFoundException $exception) {
65+
throw new HttpException(401);
6466
}
6567

68+
$request->attributes->set('node', $node);
69+
6670
return $next($request);
6771
}
6872
}

app/Http/Middleware/EncryptCookies.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ class EncryptCookies extends BaseEncrypter
1111
*
1212
* @var array
1313
*/
14-
protected $except = [
15-
];
14+
protected $except = [];
1615
}

app/Http/Middleware/LanguageMiddleware.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,38 @@
99

1010
namespace Pterodactyl\Http\Middleware;
1111

12-
use Auth;
1312
use Closure;
14-
use Session;
15-
use Settings;
13+
use Illuminate\Http\Request;
1614
use Illuminate\Support\Facades\App;
15+
use Illuminate\Contracts\Config\Repository;
1716

1817
class LanguageMiddleware
1918
{
19+
/**
20+
* @var \Illuminate\Contracts\Config\Repository
21+
*/
22+
private $config;
23+
24+
/**
25+
* LanguageMiddleware constructor.
26+
*
27+
* @param \Illuminate\Contracts\Config\Repository $config
28+
*/
29+
public function __construct(Repository $config)
30+
{
31+
$this->config = $config;
32+
}
33+
2034
/**
2135
* Handle an incoming request.
2236
*
2337
* @param \Illuminate\Http\Request $request
2438
* @param \Closure $next
2539
* @return mixed
2640
*/
27-
public function handle($request, Closure $next)
41+
public function handle(Request $request, Closure $next)
2842
{
29-
// if (Session::has('applocale')) {
30-
// App::setLocale(Session::get('applocale'));
31-
// } elseif (Auth::check() && isset(Auth::user()->language)) {
32-
// Session::put('applocale', Auth::user()->language);
33-
// App::setLocale(Auth::user()->language);
34-
// } else {
35-
// App::setLocale(Settings::get('default_language', 'en'));
36-
// }
37-
App::setLocale('en');
43+
App::setLocale($this->config->get('app.locale', 'en'));
3844

3945
return $next($request);
4046
}

app/Http/Middleware/RedirectIfAuthenticated.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@
33
namespace Pterodactyl\Http\Middleware;
44

55
use Closure;
6-
use Illuminate\Support\Facades\Auth;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Auth\AuthManager;
78

89
class RedirectIfAuthenticated
910
{
11+
/**
12+
* @var \Illuminate\Contracts\Auth\Guard
13+
*/
14+
private $authManager;
15+
16+
/**
17+
* RedirectIfAuthenticated constructor.
18+
*
19+
* @param \Illuminate\Auth\AuthManager $authManager
20+
*/
21+
public function __construct(AuthManager $authManager)
22+
{
23+
$this->authManager = $authManager;
24+
}
25+
1026
/**
1127
* Handle an incoming request.
1228
*
@@ -15,9 +31,9 @@ class RedirectIfAuthenticated
1531
* @param string|null $guard
1632
* @return mixed
1733
*/
18-
public function handle($request, Closure $next, $guard = null)
34+
public function handle(Request $request, Closure $next, string $guard = null)
1935
{
20-
if (Auth::guard($guard)->check()) {
36+
if ($this->authManager->guard($guard)->check()) {
2137
return redirect(route('index'));
2238
}
2339

app/Http/Middleware/RequireTwoFactorAuthentication.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Pterodactyl\Http\Middleware;
1111

1212
use Closure;
13+
use Illuminate\Http\Request;
1314
use Krucas\Settings\Settings;
1415
use Prologue\Alerts\AlertsMessageBag;
1516

@@ -22,28 +23,35 @@ class RequireTwoFactorAuthentication
2223
/**
2324
* @var \Prologue\Alerts\AlertsMessageBag
2425
*/
25-
protected $alert;
26+
private $alert;
2627

2728
/**
2829
* @var \Krucas\Settings\Settings
2930
*/
30-
protected $settings;
31+
private $settings;
3132

3233
/**
33-
* All TOTP related routes.
34+
* The names of routes that should be accessable without 2FA enabled.
3435
*
3536
* @var array
3637
*/
37-
protected $ignoreRoutes = [
38-
'account.security',
39-
'account.security.revoke',
40-
'account.security.totp',
41-
'account.security.totp.set',
42-
'account.security.totp.disable',
43-
'auth.totp',
44-
'auth.logout',
38+
protected $except = [
39+
'account.security',
40+
'account.security.revoke',
41+
'account.security.totp',
42+
'account.security.totp.set',
43+
'account.security.totp.disable',
44+
'auth.totp',
45+
'auth.logout',
4546
];
4647

48+
/**
49+
* The route to redirect a user to to enable 2FA.
50+
*
51+
* @var string
52+
*/
53+
protected $redirectRoute = 'account.security';
54+
4755
/**
4856
* RequireTwoFactorAuthentication constructor.
4957
*
@@ -63,15 +71,15 @@ public function __construct(AlertsMessageBag $alert, Settings $settings)
6371
* @param \Closure $next
6472
* @return mixed
6573
*/
66-
public function handle($request, Closure $next)
74+
public function handle(Request $request, Closure $next)
6775
{
6876
// Ignore non-users
6977
if (! $request->user()) {
7078
return $next($request);
7179
}
7280

7381
// Skip the 2FA pages
74-
if (in_array($request->route()->getName(), $this->ignoreRoutes)) {
82+
if (in_array($request->route()->getName(), $this->except)) {
7583
return $next($request);
7684
}
7785

@@ -93,8 +101,8 @@ public function handle($request, Closure $next)
93101
break;
94102
}
95103

96-
$this->alert->danger('The administrator has required 2FA to be enabled. You must enable it before you can do any other action.')->flash();
104+
$this->alert->danger(trans('auth.2fa_must_be_enabled'))->flash();
97105

98-
return redirect()->route('account.security');
106+
return redirect()->route($this->redirectRoute);
99107
}
100108
}

0 commit comments

Comments
 (0)