Rename APIKey to ApiKey

This commit is contained in:
Dane Everitt 2018-01-14 12:06:15 -06:00
parent 7aa540b895
commit ad3a954256
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
14 changed files with 53 additions and 53 deletions

View file

@ -9,17 +9,17 @@
namespace Pterodactyl\Contracts\Repository; namespace Pterodactyl\Contracts\Repository;
use Pterodactyl\Models\APIKey; use Pterodactyl\Models\ApiKey;
interface ApiKeyRepositoryInterface extends RepositoryInterface interface ApiKeyRepositoryInterface extends RepositoryInterface
{ {
/** /**
* Load permissions for a key onto the model. * Load permissions for a key onto the model.
* *
* @param \Pterodactyl\Models\APIKey $model * @param \Pterodactyl\Models\ApiKey $model
* @param bool $refresh * @param bool $refresh
* @deprecated * @deprecated
* @return \Pterodactyl\Models\APIKey * @return \Pterodactyl\Models\ApiKey
*/ */
public function loadPermissions(APIKey $model, bool $refresh = false): APIKey; public function loadPermissions(ApiKey $model, bool $refresh = false): ApiKey;
} }

View file

@ -4,7 +4,7 @@ namespace Pterodactyl\Http\Middleware\Api\Admin;
use Closure; use Closure;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Models\APIKey; use Pterodactyl\Models\ApiKey;
use Illuminate\Auth\AuthManager; use Illuminate\Auth\AuthManager;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
@ -61,8 +61,8 @@ class AuthenticateKey
} }
$raw = $request->bearerToken(); $raw = $request->bearerToken();
$identifier = substr($raw, 0, APIKey::IDENTIFIER_LENGTH); $identifier = substr($raw, 0, ApiKey::IDENTIFIER_LENGTH);
$token = substr($raw, APIKey::IDENTIFIER_LENGTH); $token = substr($raw, ApiKey::IDENTIFIER_LENGTH);
try { try {
$model = $this->repository->findFirstWhere([['identifier', '=', $identifier]]); $model = $this->repository->findFirstWhere([['identifier', '=', $identifier]]);

View file

@ -2,7 +2,7 @@
namespace Pterodactyl\Http\Requests\API\Admin; namespace Pterodactyl\Http\Requests\API\Admin;
use Pterodactyl\Models\APIKey; use Pterodactyl\Models\ApiKey;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Pterodactyl\Exceptions\PterodactylException; use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Services\Acl\Api\AdminAcl as Acl; use Pterodactyl\Services\Acl\Api\AdminAcl as Acl;
@ -66,9 +66,9 @@ abstract class ApiAdminRequest extends FormRequest
/** /**
* Return the API key being used for the request. * Return the API key being used for the request.
* *
* @return \Pterodactyl\Models\APIKey * @return \Pterodactyl\Models\ApiKey
*/ */
public function key(): APIKey public function key(): ApiKey
{ {
return $this->attributes->get('api_key'); return $this->attributes->get('api_key');
} }

View file

@ -10,7 +10,7 @@ use Illuminate\Contracts\Encryption\Encrypter;
use Sofa\Eloquence\Contracts\CleansAttributes; use Sofa\Eloquence\Contracts\CleansAttributes;
use Sofa\Eloquence\Contracts\Validable as ValidableContract; use Sofa\Eloquence\Contracts\Validable as ValidableContract;
class APIKey extends Model implements CleansAttributes, ValidableContract class ApiKey extends Model implements CleansAttributes, ValidableContract
{ {
use Eloquence, Validable; use Eloquence, Validable;

View file

@ -13,7 +13,7 @@ use File;
use Cache; use Cache;
use Carbon; use Carbon;
use Request; use Request;
use Pterodactyl\Models\APIKey; use Pterodactyl\Models\ApiKey;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Pterodactyl\Services\ApiKeyService; use Pterodactyl\Services\ApiKeyService;
@ -51,7 +51,7 @@ class MacroServiceProvider extends ServiceProvider
'ApiKeyMacro', 'ApiKeyMacro',
'ApiKeyMacro:Key:' . $parts[0], 'ApiKeyMacro:Key:' . $parts[0],
])->remember('ApiKeyMacro.' . $parts[0], Carbon::now()->addMinutes(15), function () use ($parts) { ])->remember('ApiKeyMacro.' . $parts[0], Carbon::now()->addMinutes(15), function () use ($parts) {
return APIKey::where('public', $parts[0])->first(); return ApiKey::where('public', $parts[0])->first();
}); });
} }

View file

@ -2,7 +2,7 @@
namespace Pterodactyl\Repositories\Eloquent; namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\APIKey; use Pterodactyl\Models\ApiKey;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
class ApiKeyRepository extends EloquentRepository implements ApiKeyRepositoryInterface class ApiKeyRepository extends EloquentRepository implements ApiKeyRepositoryInterface
@ -14,18 +14,18 @@ class ApiKeyRepository extends EloquentRepository implements ApiKeyRepositoryInt
*/ */
public function model() public function model()
{ {
return APIKey::class; return ApiKey::class;
} }
/** /**
* Load permissions for a key onto the model. * Load permissions for a key onto the model.
* *
* @param \Pterodactyl\Models\APIKey $model * @param \Pterodactyl\Models\ApiKey $model
* @param bool $refresh * @param bool $refresh
* @deprecated * @deprecated
* @return \Pterodactyl\Models\APIKey * @return \Pterodactyl\Models\ApiKey
*/ */
public function loadPermissions(APIKey $model, bool $refresh = false): APIKey public function loadPermissions(ApiKey $model, bool $refresh = false): ApiKey
{ {
if (! $model->relationLoaded('permissions') || $refresh) { if (! $model->relationLoaded('permissions') || $refresh) {
$model->load('permissions'); $model->load('permissions');

View file

@ -2,7 +2,7 @@
namespace Pterodactyl\Services\Acl\Api; namespace Pterodactyl\Services\Acl\Api;
use Pterodactyl\Models\APIKey; use Pterodactyl\Models\ApiKey;
class AdminAcl class AdminAcl
{ {
@ -54,12 +54,12 @@ class AdminAcl
* Determine if an API Key model has permission to access a given resource * Determine if an API Key model has permission to access a given resource
* at a specific action level. * at a specific action level.
* *
* @param \Pterodactyl\Models\APIKey $key * @param \Pterodactyl\Models\ApiKey $key
* @param string $resource * @param string $resource
* @param int $action * @param int $action
* @return bool * @return bool
*/ */
public static function check(APIKey $key, string $resource, int $action = self::READ) public static function check(ApiKey $key, string $resource, int $action = self::READ)
{ {
return self::can(data_get($key, self::COLUMN_IDENTIFER . $resource, self::NONE), $action); return self::can(data_get($key, self::COLUMN_IDENTIFER . $resource, self::NONE), $action);
} }

View file

@ -2,7 +2,7 @@
namespace Pterodactyl\Services\Api; namespace Pterodactyl\Services\Api;
use Pterodactyl\Models\APIKey; use Pterodactyl\Models\ApiKey;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
@ -36,15 +36,15 @@ class KeyCreationService
* stored in the database. * stored in the database.
* *
* @param array $data * @param array $data
* @return \Pterodactyl\Models\APIKey * @return \Pterodactyl\Models\ApiKey
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/ */
public function handle(array $data): APIKey public function handle(array $data): ApiKey
{ {
$data = array_merge($data, [ $data = array_merge($data, [
'identifier' => str_random(APIKey::IDENTIFIER_LENGTH), 'identifier' => str_random(ApiKey::IDENTIFIER_LENGTH),
'token' => $this->encrypter->encrypt(str_random(APIKey::KEY_LENGTH)), 'token' => $this->encrypter->encrypt(str_random(ApiKey::KEY_LENGTH)),
]); ]);
$instance = $this->repository->create($data, true, true); $instance = $this->repository->create($data, true, true);

View file

@ -3,7 +3,7 @@
namespace Pterodactyl\Transformers\Api\Admin; namespace Pterodactyl\Transformers\Api\Admin;
use Cake\Chronos\Chronos; use Cake\Chronos\Chronos;
use Pterodactyl\Models\APIKey; use Pterodactyl\Models\ApiKey;
use Illuminate\Container\Container; use Illuminate\Container\Container;
use League\Fractal\TransformerAbstract; use League\Fractal\TransformerAbstract;
use Pterodactyl\Services\Acl\Api\AdminAcl; use Pterodactyl\Services\Acl\Api\AdminAcl;
@ -13,17 +13,17 @@ abstract class BaseTransformer extends TransformerAbstract
const RESPONSE_TIMEZONE = 'UTC'; const RESPONSE_TIMEZONE = 'UTC';
/** /**
* @var \Pterodactyl\Models\APIKey * @var \Pterodactyl\Models\ApiKey
*/ */
private $key; private $key;
/** /**
* Set the HTTP request class being used for this request. * Set the HTTP request class being used for this request.
* *
* @param \Pterodactyl\Models\APIKey $key * @param \Pterodactyl\Models\ApiKey $key
* @return $this * @return $this
*/ */
public function setKey(APIKey $key) public function setKey(ApiKey $key)
{ {
$this->key = $key; $this->key = $key;
@ -33,9 +33,9 @@ abstract class BaseTransformer extends TransformerAbstract
/** /**
* Return the request instance being used for this transformer. * Return the request instance being used for this transformer.
* *
* @return \Pterodactyl\Models\APIKey * @return \Pterodactyl\Models\ApiKey
*/ */
public function getKey(): APIKey public function getKey(): ApiKey
{ {
return $this->key; return $this->key;
} }

View file

@ -223,11 +223,11 @@ $factory->define(Pterodactyl\Models\DaemonKey::class, function (Faker $faker) {
]; ];
}); });
$factory->define(Pterodactyl\Models\APIKey::class, function (Faker $faker) { $factory->define(Pterodactyl\Models\ApiKey::class, function (Faker $faker) {
return [ return [
'id' => $faker->unique()->randomNumber(), 'id' => $faker->unique()->randomNumber(),
'user_id' => $faker->randomNumber(), 'user_id' => $faker->randomNumber(),
'identifier' => str_random(Pterodactyl\Models\APIKey::IDENTIFIER_LENGTH), 'identifier' => str_random(Pterodactyl\Models\ApiKey::IDENTIFIER_LENGTH),
'token' => 'encrypted_string', 'token' => 'encrypted_string',
'memo' => 'Test Function Key', 'memo' => 'Test Function Key',
'created_at' => \Carbon\Carbon::now()->toDateTimeString(), 'created_at' => \Carbon\Carbon::now()->toDateTimeString(),

View file

@ -4,7 +4,7 @@ namespace Tests\Unit\Http\Controllers\Base;
use Mockery as m; use Mockery as m;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Pterodactyl\Models\APIKey; use Pterodactyl\Models\ApiKey;
use Prologue\Alerts\AlertsMessageBag; use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Services\Api\KeyCreationService; use Pterodactyl\Services\Api\KeyCreationService;
use Tests\Unit\Http\Controllers\ControllerTestCase; use Tests\Unit\Http\Controllers\ControllerTestCase;
@ -88,7 +88,7 @@ class APIControllerTest extends ControllerTestCase
{ {
$this->setRequestMockClass(ApiKeyFormRequest::class); $this->setRequestMockClass(ApiKeyFormRequest::class);
$model = $this->generateRequestUserModel(['root_admin' => $admin]); $model = $this->generateRequestUserModel(['root_admin' => $admin]);
$keyModel = factory(APIKey::class)->make(); $keyModel = factory(ApiKey::class)->make();
if ($admin) { if ($admin) {
$this->request->shouldReceive('input')->with('admin_permissions', [])->once()->andReturn(['admin.permission']); $this->request->shouldReceive('input')->with('admin_permissions', [])->once()->andReturn(['admin.permission']);

View file

@ -2,7 +2,7 @@
namespace Tests\Unit\Http\Middleware\Api\Admin; namespace Tests\Unit\Http\Middleware\Api\Admin;
use Pterodactyl\Models\APIKey; use Pterodactyl\Models\ApiKey;
use Tests\Unit\Http\Middleware\MiddlewareTestCase; use Tests\Unit\Http\Middleware\MiddlewareTestCase;
use Pterodactyl\Http\Middleware\Api\Admin\AuthenticateIPAccess; use Pterodactyl\Http\Middleware\Api\Admin\AuthenticateIPAccess;
@ -13,7 +13,7 @@ class AuthenticateIPAccessTest extends MiddlewareTestCase
*/ */
public function testWithNoIPRestrictions() public function testWithNoIPRestrictions()
{ {
$model = factory(APIKey::class)->make(['allowed_ips' => []]); $model = factory(ApiKey::class)->make(['allowed_ips' => []]);
$this->setRequestAttribute('api_key', $model); $this->setRequestAttribute('api_key', $model);
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
@ -25,7 +25,7 @@ class AuthenticateIPAccessTest extends MiddlewareTestCase
*/ */
public function testWithValidIP() public function testWithValidIP()
{ {
$model = factory(APIKey::class)->make(['allowed_ips' => ['127.0.0.1']]); $model = factory(ApiKey::class)->make(['allowed_ips' => ['127.0.0.1']]);
$this->setRequestAttribute('api_key', $model); $this->setRequestAttribute('api_key', $model);
$this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('127.0.0.1'); $this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('127.0.0.1');
@ -38,7 +38,7 @@ class AuthenticateIPAccessTest extends MiddlewareTestCase
*/ */
public function testValidIPAganistCIDRRange() public function testValidIPAganistCIDRRange()
{ {
$model = factory(APIKey::class)->make(['allowed_ips' => ['192.168.1.1/28']]); $model = factory(ApiKey::class)->make(['allowed_ips' => ['192.168.1.1/28']]);
$this->setRequestAttribute('api_key', $model); $this->setRequestAttribute('api_key', $model);
$this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('192.168.1.15'); $this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('192.168.1.15');
@ -54,7 +54,7 @@ class AuthenticateIPAccessTest extends MiddlewareTestCase
*/ */
public function testWithInvalidIP() public function testWithInvalidIP()
{ {
$model = factory(APIKey::class)->make(['allowed_ips' => ['127.0.0.1']]); $model = factory(ApiKey::class)->make(['allowed_ips' => ['127.0.0.1']]);
$this->setRequestAttribute('api_key', $model); $this->setRequestAttribute('api_key', $model);
$this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('127.0.0.2'); $this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('127.0.0.2');

View file

@ -3,7 +3,7 @@
namespace Tests\Unit\Http\Middleware\Api; namespace Tests\Unit\Http\Middleware\Api;
use Mockery as m; use Mockery as m;
use Pterodactyl\Models\APIKey; use Pterodactyl\Models\ApiKey;
use Illuminate\Auth\AuthManager; use Illuminate\Auth\AuthManager;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Tests\Unit\Http\Middleware\MiddlewareTestCase; use Tests\Unit\Http\Middleware\MiddlewareTestCase;
@ -74,7 +74,7 @@ class AuthenticateKeyTest extends MiddlewareTestCase
*/ */
public function testValidToken() public function testValidToken()
{ {
$model = factory(APIKey::class)->make(); $model = factory(ApiKey::class)->make();
$this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn($model->identifier . 'decrypted'); $this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn($model->identifier . 'decrypted');
$this->repository->shouldReceive('findFirstWhere')->with([['identifier', '=', $model->identifier]])->once()->andReturn($model); $this->repository->shouldReceive('findFirstWhere')->with([['identifier', '=', $model->identifier]])->once()->andReturn($model);
@ -93,7 +93,7 @@ class AuthenticateKeyTest extends MiddlewareTestCase
*/ */
public function testInvalidTokenForIdentifier() public function testInvalidTokenForIdentifier()
{ {
$model = factory(APIKey::class)->make(); $model = factory(ApiKey::class)->make();
$this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn($model->identifier . 'asdf'); $this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn($model->identifier . 'asdf');
$this->repository->shouldReceive('findFirstWhere')->with([['identifier', '=', $model->identifier]])->once()->andReturn($model); $this->repository->shouldReceive('findFirstWhere')->with([['identifier', '=', $model->identifier]])->once()->andReturn($model);

View file

@ -5,7 +5,7 @@ namespace Tests\Unit\Services\Api;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use phpmock\phpunit\PHPMock; use phpmock\phpunit\PHPMock;
use Pterodactyl\Models\APIKey; use Pterodactyl\Models\ApiKey;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Services\Api\KeyCreationService; use Pterodactyl\Services\Api\KeyCreationService;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
@ -40,48 +40,48 @@ class KeyCreationServiceTest extends TestCase
*/ */
public function testKeyIsCreated() public function testKeyIsCreated()
{ {
$model = factory(APIKey::class)->make(); $model = factory(ApiKey::class)->make();
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random') $this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
->expects($this->exactly(2))->willReturnCallback(function ($length) { ->expects($this->exactly(2))->willReturnCallback(function ($length) {
return 'str_' . $length; return 'str_' . $length;
}); });
$this->encrypter->shouldReceive('encrypt')->with('str_' . APIKey::KEY_LENGTH)->once()->andReturn($model->token); $this->encrypter->shouldReceive('encrypt')->with('str_' . ApiKey::KEY_LENGTH)->once()->andReturn($model->token);
$this->repository->shouldReceive('create')->with([ $this->repository->shouldReceive('create')->with([
'test-data' => 'test', 'test-data' => 'test',
'identifier' => 'str_' . APIKey::IDENTIFIER_LENGTH, 'identifier' => 'str_' . ApiKey::IDENTIFIER_LENGTH,
'token' => $model->token, 'token' => $model->token,
], true, true)->once()->andReturn($model); ], true, true)->once()->andReturn($model);
$response = $this->getService()->handle(['test-data' => 'test']); $response = $this->getService()->handle(['test-data' => 'test']);
$this->assertNotEmpty($response); $this->assertNotEmpty($response);
$this->assertInstanceOf(APIKey::class, $response); $this->assertInstanceOf(ApiKey::class, $response);
$this->assertSame($model, $response); $this->assertSame($model, $response);
} }
public function testIdentifierAndTokenAreOnlySetByFunction() public function testIdentifierAndTokenAreOnlySetByFunction()
{ {
$model = factory(APIKey::class)->make(); $model = factory(ApiKey::class)->make();
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random') $this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
->expects($this->exactly(2))->willReturnCallback(function ($length) { ->expects($this->exactly(2))->willReturnCallback(function ($length) {
return 'str_' . $length; return 'str_' . $length;
}); });
$this->encrypter->shouldReceive('encrypt')->with('str_' . APIKey::KEY_LENGTH)->once()->andReturn($model->token); $this->encrypter->shouldReceive('encrypt')->with('str_' . ApiKey::KEY_LENGTH)->once()->andReturn($model->token);
$this->repository->shouldReceive('create')->with([ $this->repository->shouldReceive('create')->with([
'identifier' => 'str_' . APIKey::IDENTIFIER_LENGTH, 'identifier' => 'str_' . ApiKey::IDENTIFIER_LENGTH,
'token' => $model->token, 'token' => $model->token,
], true, true)->once()->andReturn($model); ], true, true)->once()->andReturn($model);
$response = $this->getService()->handle(['identifier' => 'customIdentifier', 'token' => 'customToken']); $response = $this->getService()->handle(['identifier' => 'customIdentifier', 'token' => 'customToken']);
$this->assertNotEmpty($response); $this->assertNotEmpty($response);
$this->assertInstanceOf(APIKey::class, $response); $this->assertInstanceOf(ApiKey::class, $response);
$this->assertSame($model, $response); $this->assertSame($model, $response);
} }