Skip to content

Commit e28973b

Browse files
committed
Move everything around as needed to get things setup for the client API
1 parent 8daf970 commit e28973b

File tree

17 files changed

+199
-46
lines changed

17 files changed

+199
-46
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Pterodactyl\Exceptions\Transformer;
4+
5+
use Pterodactyl\Exceptions\PterodactylException;
6+
7+
class InvalidTransformerLevelException extends PterodactylException
8+
{
9+
}

app/Http/Controllers/Api/Application/ApplicationApiController.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
use Illuminate\Container\Container;
88
use Pterodactyl\Http\Controllers\Controller;
99
use Pterodactyl\Extensions\Spatie\Fractalistic\Fractal;
10+
use Pterodactyl\Transformers\Api\Application\BaseTransformer;
11+
use Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException;
1012

1113
abstract class ApplicationApiController extends Controller
1214
{
1315
/**
1416
* @var \Illuminate\Http\Request
1517
*/
16-
private $request;
18+
protected $request;
1719

1820
/**
1921
* @var \Pterodactyl\Extensions\Spatie\Fractalistic\Fractal
@@ -54,13 +56,19 @@ public function loadDependencies(Fractal $fractal, Request $request)
5456
*
5557
* @param string $abstract
5658
* @return \Pterodactyl\Transformers\Api\Application\BaseTransformer
59+
*
60+
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
5761
*/
5862
public function getTransformer(string $abstract)
5963
{
6064
/** @var \Pterodactyl\Transformers\Api\Application\BaseTransformer $transformer */
6165
$transformer = Container::getInstance()->make($abstract);
6266
$transformer->setKey($this->request->attributes->get('api_key'));
6367

68+
if (! $transformer instanceof BaseTransformer) {
69+
throw new InvalidTransformerLevelException('Calls to ' . __METHOD__ . ' must return a transformer that is an instance of ' . __CLASS__);
70+
}
71+
6472
return $transformer;
6573
}
6674

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Application;
4+
5+
use Illuminate\Container\Container;
6+
use Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException;
7+
8+
abstract class ClientApiController extends ApplicationApiController
9+
{
10+
/**
11+
* Return an instance of an application transformer.
12+
*
13+
* @param string $abstract
14+
* @return \Pterodactyl\Transformers\Api\Client\BaseClientTransformer
15+
*
16+
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
17+
*/
18+
public function getTransformer(string $abstract)
19+
{
20+
/** @var \Pterodactyl\Transformers\Api\Client\BaseClientTransformer $transformer */
21+
$transformer = Container::getInstance()->make($abstract);
22+
$transformer->setKey($this->request->attributes->get('api_key'));
23+
24+
if (! $transformer instanceof self) {
25+
throw new InvalidTransformerLevelException('Calls to ' . __METHOD__ . ' must return a transformer that is an instance of ' . __CLASS__);
26+
}
27+
28+
return $transformer;
29+
}
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Client;
4+
5+
use Pterodactyl\Http\Controllers\Api\Application\ClientApiController;
6+
7+
class ClientController extends ClientApiController
8+
{
9+
}

app/Http/Kernel.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pterodactyl\Http;
44

5+
use Pterodactyl\Models\ApiKey;
56
use Illuminate\Auth\Middleware\Authorize;
67
use Illuminate\Auth\Middleware\Authenticate;
78
use Pterodactyl\Http\Middleware\TrimStrings;
@@ -14,11 +15,14 @@
1415
use Illuminate\Routing\Middleware\ThrottleRequests;
1516
use Pterodactyl\Http\Middleware\LanguageMiddleware;
1617
use Illuminate\Foundation\Http\Kernel as HttpKernel;
18+
use Pterodactyl\Http\Middleware\Api\AuthenticateKey;
1719
use Illuminate\Routing\Middleware\SubstituteBindings;
1820
use Pterodactyl\Http\Middleware\AccessingValidServer;
21+
use Pterodactyl\Http\Middleware\Api\SetSessionDriver;
1922
use Illuminate\View\Middleware\ShareErrorsFromSession;
2023
use Pterodactyl\Http\Middleware\RedirectIfAuthenticated;
2124
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
25+
use Pterodactyl\Http\Middleware\Api\AuthenticateIPAccess;
2226
use Pterodactyl\Http\Middleware\Api\ApiSubstituteBindings;
2327
use Illuminate\Foundation\Http\Middleware\ValidatePostSize;
2428
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
@@ -28,12 +32,9 @@
2832
use Pterodactyl\Http\Middleware\RequireTwoFactorAuthentication;
2933
use Pterodactyl\Http\Middleware\Server\DatabaseBelongsToServer;
3034
use Pterodactyl\Http\Middleware\Server\ScheduleBelongsToServer;
31-
use Pterodactyl\Http\Middleware\Api\Application\AuthenticateKey;
32-
use Pterodactyl\Http\Middleware\Api\Application\AuthenticateUser;
33-
use Pterodactyl\Http\Middleware\Api\Application\SetSessionDriver;
3435
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode;
3536
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
36-
use Pterodactyl\Http\Middleware\Api\Application\AuthenticateIPAccess;
37+
use Pterodactyl\Http\Middleware\Api\Application\AuthenticateApplicationUser;
3738
use Pterodactyl\Http\Middleware\DaemonAuthenticate as OldDaemonAuthenticate;
3839

3940
class Kernel extends HttpKernel
@@ -71,8 +72,15 @@ class Kernel extends HttpKernel
7172
'throttle:120,1',
7273
ApiSubstituteBindings::class,
7374
SetSessionDriver::class,
74-
AuthenticateKey::class,
75-
AuthenticateUser::class,
75+
'api..key:' . ApiKey::TYPE_APPLICATION,
76+
AuthenticateApplicationUser::class,
77+
AuthenticateIPAccess::class,
78+
],
79+
'client-api' => [
80+
'throttle:60,1',
81+
ApiSubstituteBindings::class,
82+
SetSessionDriver::class,
83+
'api..key:' . ApiKey::TYPE_ACCOUNT,
7684
AuthenticateIPAccess::class,
7785
],
7886
'daemon' => [
@@ -107,5 +115,8 @@ class Kernel extends HttpKernel
107115
'server..database' => DatabaseBelongsToServer::class,
108116
'server..subuser' => SubuserBelongsToServer::class,
109117
'server..schedule' => ScheduleBelongsToServer::class,
118+
119+
// API Specific Middleware
120+
'api..key' => AuthenticateKey::class,
110121
];
111122
}

app/Http/Middleware/Api/Application/AuthenticateUser.php renamed to app/Http/Middleware/Api/Application/AuthenticateApplicationUser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Http\Request;
77
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
88

9-
class AuthenticateUser
9+
class AuthenticateApplicationUser
1010
{
1111
/**
1212
* Authenticate that the currently authenticated user is an administrator

app/Http/Middleware/Api/Application/AuthenticateIPAccess.php renamed to app/Http/Middleware/Api/AuthenticateIPAccess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Pterodactyl\Http\Middleware\Api\Application;
3+
namespace Pterodactyl\Http\Middleware\Api;
44

55
use Closure;
66
use IPTools\IP;

app/Http/Middleware/Api/Application/AuthenticateKey.php renamed to app/Http/Middleware/Api/AuthenticateKey.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Pterodactyl\Http\Middleware\Api\Application;
3+
namespace Pterodactyl\Http\Middleware\Api;
44

55
use Closure;
66
use Cake\Chronos\Chronos;
@@ -50,12 +50,13 @@ public function __construct(ApiKeyRepositoryInterface $repository, AuthManager $
5050
*
5151
* @param \Illuminate\Http\Request $request
5252
* @param \Closure $next
53+
* @param int $keyType
5354
* @return mixed
5455
*
5556
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
5657
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
5758
*/
58-
public function handle(Request $request, Closure $next)
59+
public function handle(Request $request, Closure $next, int $keyType)
5960
{
6061
if (is_null($request->bearerToken())) {
6162
throw new HttpException(401, null, null, ['WWW-Authenticate' => 'Bearer']);
@@ -68,7 +69,7 @@ public function handle(Request $request, Closure $next)
6869
try {
6970
$model = $this->repository->findFirstWhere([
7071
['identifier', '=', $identifier],
71-
['key_type', '=', ApiKey::TYPE_APPLICATION],
72+
['key_type', '=', $keyType],
7273
]);
7374
} catch (RecordNotFoundException $exception) {
7475
throw new AccessDeniedHttpException;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Middleware\Api\Client;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
8+
9+
class AuthenticateClientAccess
10+
{
11+
/**
12+
* Authenticate that the currently authenticated user has permission
13+
* to access the specified server.
14+
*
15+
* @param \Illuminate\Http\Request $request
16+
* @param \Closure $next
17+
* @return mixed
18+
*/
19+
public function handle(Request $request, Closure $next)
20+
{
21+
if (is_null($request->user())) {
22+
throw new AccessDeniedHttpException('This account does not have permission to access this resource.');
23+
}
24+
25+
return $next($request);
26+
}
27+
}

app/Http/Middleware/Api/Application/SetSessionDriver.php renamed to app/Http/Middleware/Api/SetSessionDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Pterodactyl\Http\Middleware\Api\Application;
3+
namespace Pterodactyl\Http\Middleware\Api;
44

55
use Closure;
66
use Illuminate\Http\Request;

0 commit comments

Comments
 (0)