<?php12namespace Pterodactyl\Models;34use Illuminate\Support\Str;5use Webmozart\Assert\Assert;6use Pterodactyl\Services\Acl\Api\AdminAcl;7use Illuminate\Database\Eloquent\Relations\BelongsTo;8use Illuminate\Database\Eloquent\Factories\HasFactory;910/**11* Pterodactyl\Models\ApiKey.12*13* @property int $id14* @property int $user_id15* @property int $key_type16* @property string $identifier17* @property string $token18* @property array|null $allowed_ips19* @property string|null $memo20* @property \Illuminate\Support\Carbon|null $last_used_at21* @property \Illuminate\Support\Carbon|null $expires_at22* @property \Illuminate\Support\Carbon|null $created_at23* @property \Illuminate\Support\Carbon|null $updated_at24* @property int $r_servers25* @property int $r_nodes26* @property int $r_allocations27* @property int $r_users28* @property int $r_locations29* @property int $r_nests30* @property int $r_eggs31* @property int $r_database_hosts32* @property int $r_server_databases33* @property User $tokenable34* @property User $user35*36* @method static \Database\Factories\ApiKeyFactory factory(...$parameters)37* @method static \Illuminate\Database\Eloquent\Builder|ApiKey newModelQuery()38* @method static \Illuminate\Database\Eloquent\Builder|ApiKey newQuery()39* @method static \Illuminate\Database\Eloquent\Builder|ApiKey query()40* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereAllowedIps($value)41* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereCreatedAt($value)42* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereId($value)43* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereIdentifier($value)44* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereKeyType($value)45* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereLastUsedAt($value)46* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereMemo($value)47* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRAllocations($value)48* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRDatabaseHosts($value)49* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereREggs($value)50* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRLocations($value)51* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRNests($value)52* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRNodes($value)53* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRServerDatabases($value)54* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRServers($value)55* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRUsers($value)56* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereToken($value)57* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereUpdatedAt($value)58* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereUserId($value)59*60* @mixin \Eloquent61*/62class ApiKey extends Model63{64/** @use HasFactory<\Database\Factories\ApiKeyFactory> */65use HasFactory;6667/**68* The resource name for this model when it is transformed into an69* API representation using fractal.70*/71public const RESOURCE_NAME = 'api_key';72/**73* Different API keys that can exist on the system.74*/75public const TYPE_NONE = 0;76public const TYPE_ACCOUNT = 1;77/* @deprecated */78public const TYPE_APPLICATION = 2;79/* @deprecated */80public const TYPE_DAEMON_USER = 3;81/* @deprecated */82public const TYPE_DAEMON_APPLICATION = 4;83/**84* The length of API key identifiers.85*/86public const IDENTIFIER_LENGTH = 16;87/**88* The length of the actual API key that is encrypted and stored89* in the database.90*/91public const KEY_LENGTH = 32;9293/**94* The table associated with the model.95*/96protected $table = 'api_keys';9798/**99* Cast values to correct type.100*/101protected $casts = [102'allowed_ips' => 'array',103'user_id' => 'int',104'last_used_at' => 'datetime',105'expires_at' => 'datetime',106self::CREATED_AT => 'datetime',107self::UPDATED_AT => 'datetime',108'r_' . AdminAcl::RESOURCE_USERS => 'int',109'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int',110'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'int',111'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'int',112'r_' . AdminAcl::RESOURCE_EGGS => 'int',113'r_' . AdminAcl::RESOURCE_LOCATIONS => 'int',114'r_' . AdminAcl::RESOURCE_NESTS => 'int',115'r_' . AdminAcl::RESOURCE_NODES => 'int',116'r_' . AdminAcl::RESOURCE_SERVERS => 'int',117];118119/**120* Fields that are mass assignable.121*/122protected $fillable = [123'identifier',124'token',125'allowed_ips',126'memo',127'last_used_at',128'expires_at',129];130131/**132* Fields that should not be included when calling toArray() or toJson()133* on this model.134*/135protected $hidden = ['token'];136137/**138* Rules to protect against invalid data entry to DB.139*/140public static array $validationRules = [141'user_id' => 'required|exists:users,id',142'key_type' => 'present|integer|min:0|max:4',143'identifier' => 'required|string|size:16|unique:api_keys,identifier',144'token' => 'required|string',145'memo' => 'required|nullable|string|max:500',146'allowed_ips' => 'nullable|array',147'allowed_ips.*' => 'string',148'last_used_at' => 'nullable|date',149'expires_at' => 'nullable|date',150'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3',151'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'integer|min:0|max:3',152'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'integer|min:0|max:3',153'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'integer|min:0|max:3',154'r_' . AdminAcl::RESOURCE_EGGS => 'integer|min:0|max:3',155'r_' . AdminAcl::RESOURCE_LOCATIONS => 'integer|min:0|max:3',156'r_' . AdminAcl::RESOURCE_NESTS => 'integer|min:0|max:3',157'r_' . AdminAcl::RESOURCE_NODES => 'integer|min:0|max:3',158'r_' . AdminAcl::RESOURCE_SERVERS => 'integer|min:0|max:3',159];160161/**162* Returns the user this token is assigned to.163*/164public function user(): BelongsTo165{166return $this->belongsTo(User::class);167}168169/**170* Required for support with Laravel Sanctum.171*172* @see \Laravel\Sanctum\Guard::supportsTokens()173*/174public function tokenable(): BelongsTo175{176return $this->user();177}178179/**180* Finds the model matching the provided token.181*/182public static function findToken(string $token): ?self183{184$identifier = substr($token, 0, self::IDENTIFIER_LENGTH);185186$model = static::where('identifier', $identifier)->first();187if (!is_null($model) && decrypt($model->token) === substr($token, strlen($identifier))) {188return $model;189}190191return null;192}193194/**195* Returns the standard prefix for API keys in the system.196*/197public static function getPrefixForType(int $type): string198{199Assert::oneOf($type, [self::TYPE_ACCOUNT, self::TYPE_APPLICATION]);200201return $type === self::TYPE_ACCOUNT ? 'ptlc_' : 'ptla_';202}203204/**205* Generates a new identifier for an API key.206*/207public static function generateTokenIdentifier(int $type): string208{209$prefix = self::getPrefixForType($type);210211return $prefix . Str::random(self::IDENTIFIER_LENGTH - strlen($prefix));212}213}214215216