2016-01-16 01:26:50 +01:00
|
|
|
<?php
|
2016-12-07 23:46:38 +01:00
|
|
|
|
2016-01-16 01:26:50 +01:00
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
2017-06-25 22:31:50 +02:00
|
|
|
use Sofa\Eloquence\Eloquence;
|
|
|
|
use Sofa\Eloquence\Validable;
|
2016-01-16 01:26:50 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2018-01-12 05:49:46 +01:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2018-01-13 23:06:19 +01:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2017-08-12 22:29:01 +02:00
|
|
|
use Sofa\Eloquence\Contracts\CleansAttributes;
|
2017-06-25 22:31:50 +02:00
|
|
|
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
|
2016-01-16 01:26:50 +01:00
|
|
|
|
2018-01-14 19:06:15 +01:00
|
|
|
class ApiKey extends Model implements CleansAttributes, ValidableContract
|
2016-01-16 01:26:50 +01:00
|
|
|
{
|
2017-06-25 22:31:50 +02:00
|
|
|
use Eloquence, Validable;
|
2017-04-08 18:05:29 +02:00
|
|
|
|
2018-01-14 19:11:04 +01:00
|
|
|
/**
|
|
|
|
* Different API keys that can exist on the system.
|
|
|
|
*/
|
|
|
|
const TYPE_NONE = 0;
|
|
|
|
const TYPE_USER = 1;
|
|
|
|
const TYPE_APPLICATION = 2;
|
|
|
|
const TYPE_DAEMON_USER = 3;
|
|
|
|
const TYPE_DAEMON_APPLICATION = 4;
|
|
|
|
|
2018-01-13 23:06:19 +01:00
|
|
|
/**
|
|
|
|
* The length of API key identifiers.
|
|
|
|
*/
|
|
|
|
const IDENTIFIER_LENGTH = 16;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The length of the actual API key that is encrypted and stored
|
|
|
|
* in the database.
|
|
|
|
*/
|
2017-11-19 20:32:17 +01:00
|
|
|
const KEY_LENGTH = 32;
|
|
|
|
|
2016-01-16 01:26:50 +01:00
|
|
|
/**
|
|
|
|
* The table associated with the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'api_keys';
|
|
|
|
|
2017-04-02 06:11:52 +02:00
|
|
|
/**
|
|
|
|
* Cast values to correct type.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'allowed_ips' => 'json',
|
2018-01-12 05:49:46 +01:00
|
|
|
'user_id' => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_USERS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_DATABASES => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_EGGS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_LOCATIONS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_NESTS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_NODES => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_PACKS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_SERVERS => 'int',
|
2017-04-02 06:11:52 +02:00
|
|
|
];
|
|
|
|
|
2016-01-16 07:20:27 +01:00
|
|
|
/**
|
2018-01-12 05:49:46 +01:00
|
|
|
* Fields that are mass assignable.
|
2016-01-16 07:20:27 +01:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-01-12 05:49:46 +01:00
|
|
|
protected $fillable = [
|
2018-01-13 23:06:19 +01:00
|
|
|
'identifier',
|
2018-01-12 05:49:46 +01:00
|
|
|
'token',
|
|
|
|
'allowed_ips',
|
|
|
|
'memo',
|
|
|
|
];
|
2017-02-10 23:29:10 +01:00
|
|
|
|
2018-01-13 23:06:19 +01:00
|
|
|
/**
|
|
|
|
* Fields that should not be included when calling toArray() or toJson()
|
|
|
|
* on this model.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = ['token'];
|
|
|
|
|
2017-06-25 22:31:50 +02:00
|
|
|
/**
|
|
|
|
* Rules defining what fields must be passed when making a model.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $applicationRules = [
|
2018-01-13 23:06:19 +01:00
|
|
|
'identifier' => 'required',
|
2017-06-25 22:31:50 +02:00
|
|
|
'memo' => 'required',
|
|
|
|
'user_id' => 'required',
|
2017-11-19 20:32:17 +01:00
|
|
|
'token' => 'required',
|
2017-06-25 22:31:50 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rules to protect aganist invalid data entry to DB.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $dataIntegrityRules = [
|
|
|
|
'user_id' => 'exists:users,id',
|
2018-01-13 23:06:19 +01:00
|
|
|
'identifier' => 'string|size:16|unique:api_keys,identifier',
|
|
|
|
'token' => 'string',
|
2017-06-25 22:31:50 +02:00
|
|
|
'memo' => 'nullable|string|max:500',
|
|
|
|
'allowed_ips' => 'nullable|json',
|
2018-01-13 23:06:19 +01:00
|
|
|
'last_used_at' => 'nullable|date',
|
2018-01-12 05:49:46 +01:00
|
|
|
'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_DATABASES => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_EGGS => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_LOCATIONS => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_NESTS => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_NODES => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_PACKS => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_SERVERS => 'integer|min:0|max:3',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dates = [
|
|
|
|
self::CREATED_AT,
|
|
|
|
self::UPDATED_AT,
|
2018-01-13 23:06:19 +01:00
|
|
|
'last_used_at',
|
2017-06-25 22:31:50 +02:00
|
|
|
];
|
|
|
|
|
2018-01-13 23:06:19 +01:00
|
|
|
/**
|
|
|
|
* Return a decrypted version of the token.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDecryptedTokenAttribute()
|
|
|
|
{
|
|
|
|
return app()->make(Encrypter::class)->decrypt($this->token);
|
|
|
|
}
|
2016-01-16 01:26:50 +01:00
|
|
|
}
|