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;
|
|
|
|
|
2022-05-22 20:57:06 +02:00
|
|
|
use Illuminate\Support\Str;
|
2018-01-12 05:49:46 +01:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2022-05-22 20:57:06 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2016-01-16 01:26:50 +01:00
|
|
|
|
2020-03-23 02:15:38 +01:00
|
|
|
/**
|
2022-05-22 20:57:06 +02:00
|
|
|
* Pterodactyl\Models\ApiKey.
|
|
|
|
*
|
2021-01-28 05:52:11 +01:00
|
|
|
* @property int $id
|
|
|
|
* @property int $user_id
|
|
|
|
* @property int $key_type
|
2022-05-22 20:57:06 +02:00
|
|
|
* @property string|null $identifier
|
2021-01-28 05:52:11 +01:00
|
|
|
* @property string $token
|
2022-05-22 20:57:06 +02:00
|
|
|
* @property array|null $allowed_ips
|
|
|
|
* @property string|null $memo
|
|
|
|
* @property \Illuminate\Support\Carbon|null $last_used_at
|
|
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
|
|
* @property int $r_servers
|
|
|
|
* @property int $r_nodes
|
|
|
|
* @property int $r_allocations
|
|
|
|
* @property int $r_users
|
|
|
|
* @property int $r_locations
|
|
|
|
* @property int $r_nests
|
|
|
|
* @property int $r_eggs
|
|
|
|
* @property int $r_database_hosts
|
|
|
|
* @property int $r_server_databases
|
|
|
|
* @property \Pterodactyl\Models\User $tokenable
|
|
|
|
* @property \Pterodactyl\Models\User $user
|
|
|
|
*
|
|
|
|
* @method static \Database\Factories\ApiKeyFactory factory(...$parameters)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey newModelQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey newQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey query()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereAllowedIps($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereCreatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereIdentifier($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereKeyType($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereLastUsedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereMemo($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRAllocations($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRDatabaseHosts($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereREggs($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRLocations($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRNests($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRNodes($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRServerDatabases($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRServers($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRUsers($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereToken($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereUpdatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereUserId($value)
|
|
|
|
* @mixin \Eloquent
|
2020-03-23 02:15:38 +01:00
|
|
|
*/
|
2020-04-04 08:22:35 +02:00
|
|
|
class ApiKey extends Model
|
2016-01-16 01:26:50 +01:00
|
|
|
{
|
2021-01-23 21:09:16 +01:00
|
|
|
/**
|
|
|
|
* The resource name for this model when it is transformed into an
|
|
|
|
* API representation using fractal.
|
|
|
|
*/
|
2021-01-23 21:33:34 +01:00
|
|
|
public const RESOURCE_NAME = 'api_key';
|
2018-01-14 19:11:04 +01:00
|
|
|
/**
|
|
|
|
* Different API keys that can exist on the system.
|
|
|
|
*/
|
2021-01-23 21:33:34 +01:00
|
|
|
public const TYPE_NONE = 0;
|
|
|
|
public const TYPE_ACCOUNT = 1;
|
2022-05-22 20:57:06 +02:00
|
|
|
/* @deprecated */
|
2021-01-23 21:33:34 +01:00
|
|
|
public const TYPE_APPLICATION = 2;
|
2022-05-22 20:57:06 +02:00
|
|
|
/* @deprecated */
|
2021-01-23 21:33:34 +01:00
|
|
|
public const TYPE_DAEMON_USER = 3;
|
2022-05-22 20:57:06 +02:00
|
|
|
/* @deprecated */
|
2021-01-23 21:33:34 +01:00
|
|
|
public const TYPE_DAEMON_APPLICATION = 4;
|
2018-01-13 23:06:19 +01:00
|
|
|
/**
|
|
|
|
* The length of API key identifiers.
|
|
|
|
*/
|
2021-01-23 21:33:34 +01:00
|
|
|
public const IDENTIFIER_LENGTH = 16;
|
2018-01-13 23:06:19 +01:00
|
|
|
/**
|
|
|
|
* The length of the actual API key that is encrypted and stored
|
|
|
|
* in the database.
|
|
|
|
*/
|
2021-01-23 21:33:34 +01:00
|
|
|
public const KEY_LENGTH = 32;
|
2017-11-19 20:32:17 +01:00
|
|
|
|
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 = [
|
2020-03-29 00:06:36 +01:00
|
|
|
'allowed_ips' => 'array',
|
2018-01-12 05:49:46 +01:00
|
|
|
'user_id' => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_USERS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int',
|
2018-01-26 04:26:06 +01:00
|
|
|
'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'int',
|
2018-01-12 05:49:46 +01:00
|
|
|
'r_' . AdminAcl::RESOURCE_EGGS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_LOCATIONS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_NESTS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_NODES => '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',
|
2018-01-14 20:30:55 +01:00
|
|
|
'last_used_at',
|
2018-01-12 05:49:46 +01:00
|
|
|
];
|
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
|
|
|
/**
|
2018-05-13 16:50:56 +02:00
|
|
|
* Rules to protect against invalid data entry to DB.
|
2017-06-25 22:31:50 +02:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-09-05 07:19:57 +02:00
|
|
|
public static $validationRules = [
|
|
|
|
'user_id' => 'required|exists:users,id',
|
|
|
|
'key_type' => 'present|integer|min:0|max:4',
|
|
|
|
'identifier' => 'required|string|size:16|unique:api_keys,identifier',
|
|
|
|
'token' => 'required|string',
|
|
|
|
'memo' => 'required|nullable|string|max:500',
|
2020-03-29 00:06:36 +01:00
|
|
|
'allowed_ips' => 'nullable|array',
|
|
|
|
'allowed_ips.*' => 'string',
|
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',
|
2018-01-26 04:26:06 +01:00
|
|
|
'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'integer|min:0|max:3',
|
2018-01-12 05:49:46 +01:00
|
|
|
'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_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
|
|
|
];
|
2022-05-22 20:57:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the user this token is assigned to.
|
|
|
|
*/
|
|
|
|
public function user(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Required for support with Laravel Sanctum.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*
|
|
|
|
* @see \Laravel\Sanctum\Guard::supportsTokens()
|
|
|
|
*/
|
|
|
|
public function tokenable()
|
|
|
|
{
|
|
|
|
return $this->user();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the model matching the provided token.
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
*
|
|
|
|
* @return self|null
|
|
|
|
*/
|
|
|
|
public static function findToken($token)
|
|
|
|
{
|
|
|
|
$id = Str::substr($token, 0, self::IDENTIFIER_LENGTH);
|
|
|
|
|
2022-05-22 22:05:58 +02:00
|
|
|
$model = static::where('identifier', $id)->first();
|
|
|
|
if (!is_null($model) && decrypt($model->token) === Str::substr($token, strlen($id))) {
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2022-05-22 20:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a new identifier for an API key.
|
|
|
|
*/
|
|
|
|
public static function generateTokenIdentifier(): string
|
|
|
|
{
|
|
|
|
return 'ptdl_' . Str::random(self::IDENTIFIER_LENGTH - 5);
|
|
|
|
}
|
2016-01-16 01:26:50 +01:00
|
|
|
}
|