2017-11-19 20:34:55 +00:00
|
|
|
<?php
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
namespace Pterodactyl\Tests\Unit\Http\Middleware\Api;
|
2017-11-19 20:34:55 +00:00
|
|
|
|
|
|
|
use Mockery as m;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
2018-07-15 05:58:33 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2018-01-14 18:06:15 +00:00
|
|
|
use Pterodactyl\Models\ApiKey;
|
2017-11-19 20:34:55 +00:00
|
|
|
use Illuminate\Auth\AuthManager;
|
2018-01-13 22:06:19 +00:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2018-02-25 21:34:01 +00:00
|
|
|
use Pterodactyl\Http\Middleware\Api\AuthenticateKey;
|
2017-11-19 20:34:55 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Pterodactyl\Tests\Unit\Http\Middleware\MiddlewareTestCase;
|
2017-11-19 20:34:55 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
2020-06-24 04:59:37 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
2017-11-19 20:34:55 +00:00
|
|
|
|
|
|
|
class AuthenticateKeyTest extends MiddlewareTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Auth\AuthManager|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $auth;
|
|
|
|
|
2018-01-13 22:06:19 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $encrypter;
|
|
|
|
|
2017-11-19 20:34:55 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-11-19 20:34:55 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->auth = m::mock(AuthManager::class);
|
2018-01-13 22:06:19 +00:00
|
|
|
$this->encrypter = m::mock(Encrypter::class);
|
2017-11-19 20:34:55 +00:00
|
|
|
$this->repository = m::mock(ApiKeyRepositoryInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a missing bearer token will throw an exception.
|
|
|
|
*/
|
|
|
|
public function testMissingBearerTokenThrowsException()
|
|
|
|
{
|
2018-07-15 05:58:33 +00:00
|
|
|
$this->request->shouldReceive('user')->andReturnNull();
|
2017-11-19 20:34:55 +00:00
|
|
|
$this->request->shouldReceive('bearerToken')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
try {
|
2018-02-25 21:34:01 +00:00
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION);
|
2017-11-19 20:34:55 +00:00
|
|
|
} catch (HttpException $exception) {
|
|
|
|
$this->assertEquals(401, $exception->getStatusCode());
|
|
|
|
$this->assertEquals(['WWW-Authenticate' => 'Bearer'], $exception->getHeaders());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-13 15:05:52 +00:00
|
|
|
* Test that an invalid API identifier throws an exception.
|
2017-11-19 20:34:55 +00:00
|
|
|
*/
|
2018-01-13 22:06:19 +00:00
|
|
|
public function testInvalidIdentifier()
|
2017-11-19 20:34:55 +00:00
|
|
|
{
|
2020-06-24 04:59:37 +00:00
|
|
|
$this->expectException(AccessDeniedHttpException::class);
|
|
|
|
|
2017-11-19 20:34:55 +00:00
|
|
|
$this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn('abcd1234');
|
2021-01-23 20:33:34 +00:00
|
|
|
$this->repository->shouldReceive('findFirstWhere')->andThrow(new RecordNotFoundException());
|
2017-11-19 20:34:55 +00:00
|
|
|
|
2018-02-25 21:34:01 +00:00
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION);
|
2017-11-19 20:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a valid token can continue past the middleware.
|
|
|
|
*/
|
|
|
|
public function testValidToken()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$model = ApiKey::factory()->make();
|
2017-11-19 20:34:55 +00:00
|
|
|
|
2018-01-13 22:06:19 +00:00
|
|
|
$this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn($model->identifier . 'decrypted');
|
2018-01-14 19:30:55 +00:00
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([
|
|
|
|
['identifier', '=', $model->identifier],
|
|
|
|
['key_type', '=', ApiKey::TYPE_APPLICATION],
|
|
|
|
])->once()->andReturn($model);
|
2018-01-13 22:06:19 +00:00
|
|
|
$this->encrypter->shouldReceive('decrypt')->with($model->token)->once()->andReturn('decrypted');
|
2017-11-19 20:34:55 +00:00
|
|
|
$this->auth->shouldReceive('guard->loginUsingId')->with($model->user_id)->once()->andReturnNull();
|
|
|
|
|
2018-01-14 19:30:55 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
|
2021-01-23 20:09:16 +00:00
|
|
|
'last_used_at' => CarbonImmutable::now(),
|
2018-01-14 19:30:55 +00:00
|
|
|
])->once()->andReturnNull();
|
|
|
|
|
2018-02-25 21:34:01 +00:00
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION);
|
|
|
|
$this->assertEquals($model, $this->request->attributes->get('api_key'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a valid token can continue past the middleware when set as a user token.
|
|
|
|
*/
|
|
|
|
public function testValidTokenWithUserKey()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$model = ApiKey::factory()->make();
|
2018-02-25 21:34:01 +00:00
|
|
|
|
|
|
|
$this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn($model->identifier . 'decrypted');
|
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([
|
|
|
|
['identifier', '=', $model->identifier],
|
|
|
|
['key_type', '=', ApiKey::TYPE_ACCOUNT],
|
|
|
|
])->once()->andReturn($model);
|
|
|
|
$this->encrypter->shouldReceive('decrypt')->with($model->token)->once()->andReturn('decrypted');
|
|
|
|
$this->auth->shouldReceive('guard->loginUsingId')->with($model->user_id)->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
|
2021-01-23 20:09:16 +00:00
|
|
|
'last_used_at' => CarbonImmutable::now(),
|
2018-02-25 21:34:01 +00:00
|
|
|
])->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_ACCOUNT);
|
2017-11-19 20:34:55 +00:00
|
|
|
$this->assertEquals($model, $this->request->attributes->get('api_key'));
|
|
|
|
}
|
|
|
|
|
2018-07-15 05:58:33 +00:00
|
|
|
/**
|
|
|
|
* Test that we can still make it though this middleware if the user is logged in and passing
|
|
|
|
* through a cookie.
|
|
|
|
*/
|
|
|
|
public function testAccessWithoutToken()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->make(['id' => 123]);
|
2018-07-15 05:58:33 +00:00
|
|
|
|
|
|
|
$this->request->shouldReceive('user')->andReturn($user);
|
|
|
|
$this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturnNull();
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_ACCOUNT);
|
|
|
|
$model = $this->request->attributes->get('api_key');
|
|
|
|
|
|
|
|
$this->assertSame(ApiKey::TYPE_ACCOUNT, $model->key_type);
|
|
|
|
$this->assertSame(123, $model->user_id);
|
|
|
|
$this->assertNull($model->identifier);
|
|
|
|
}
|
|
|
|
|
2018-01-13 22:06:19 +00:00
|
|
|
/**
|
|
|
|
* Test that a valid token identifier with an invalid token attached to it
|
|
|
|
* triggers an exception.
|
|
|
|
*/
|
|
|
|
public function testInvalidTokenForIdentifier()
|
|
|
|
{
|
2020-06-24 04:59:37 +00:00
|
|
|
$this->expectException(AccessDeniedHttpException::class);
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$model = ApiKey::factory()->make();
|
2018-01-13 22:06:19 +00:00
|
|
|
|
|
|
|
$this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn($model->identifier . 'asdf');
|
2018-01-14 19:30:55 +00:00
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([
|
|
|
|
['identifier', '=', $model->identifier],
|
|
|
|
['key_type', '=', ApiKey::TYPE_APPLICATION],
|
|
|
|
])->once()->andReturn($model);
|
2018-01-13 22:06:19 +00:00
|
|
|
$this->encrypter->shouldReceive('decrypt')->with($model->token)->once()->andReturn('decrypted');
|
|
|
|
|
2018-02-25 21:34:01 +00:00
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION);
|
2018-01-13 22:06:19 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 20:34:55 +00:00
|
|
|
/**
|
|
|
|
* Return an instance of the middleware with mocked dependencies for testing.
|
|
|
|
*/
|
|
|
|
private function getMiddleware(): AuthenticateKey
|
|
|
|
{
|
2018-01-13 22:06:19 +00:00
|
|
|
return new AuthenticateKey($this->repository, $this->auth, $this->encrypter);
|
2017-11-19 20:34:55 +00:00
|
|
|
}
|
|
|
|
}
|