<?php12namespace Pterodactyl\Models;34use Illuminate\Support\Str;5use Webmozart\Assert\Assert;6use Pterodactyl\Services\Acl\Api\AdminAcl;7use Laravel\Sanctum\Contracts\HasAbilities;8use Illuminate\Database\Eloquent\Relations\BelongsTo;9use Illuminate\Database\Eloquent\Factories\HasFactory;1011/**12* Pterodactyl\Models\ApiKey.13*14* @property int $id15* @property int $user_id16* @property int $key_type17* @property string $identifier18* @property string $token19* @property array|null $allowed_ips20* @property string|null $memo21* @property \Illuminate\Support\Carbon|null $last_used_at22* @property \Illuminate\Support\Carbon|null $expires_at23* @property \Illuminate\Support\Carbon|null $created_at24* @property \Illuminate\Support\Carbon|null $updated_at25* @property int $r_servers26* @property int $r_nodes27* @property int $r_allocations28* @property int $r_users29* @property int $r_locations30* @property int $r_nests31* @property int $r_eggs32* @property int $r_database_hosts33* @property int $r_server_databases34* @property User $tokenable35* @property User $user36*37* @method static \Database\Factories\ApiKeyFactory factory(...$parameters)38* @method static \Illuminate\Database\Eloquent\Builder|ApiKey newModelQuery()39* @method static \Illuminate\Database\Eloquent\Builder|ApiKey newQuery()40* @method static \Illuminate\Database\Eloquent\Builder|ApiKey query()41* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereAllowedIps($value)42* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereCreatedAt($value)43* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereId($value)44* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereIdentifier($value)45* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereKeyType($value)46* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereLastUsedAt($value)47* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereMemo($value)48* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRAllocations($value)49* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRDatabaseHosts($value)50* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereREggs($value)51* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRLocations($value)52* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRNests($value)53* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRNodes($value)54* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRServerDatabases($value)55* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRServers($value)56* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereRUsers($value)57* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereToken($value)58* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereUpdatedAt($value)59* @method static \Illuminate\Database\Eloquent\Builder|ApiKey whereUserId($value)60*61* @mixin \Eloquent62*/63class ApiKey extends Model implements HasAbilities64{65/** @use HasFactory<\Database\Factories\ApiKeyFactory> */66use HasFactory;6768/**69* The resource name for this model when it is transformed into an70* API representation using fractal.71*/72public const RESOURCE_NAME = 'api_key';73/**74* Different API keys that can exist on the system.75*/76public const TYPE_NONE = 0;77public const TYPE_ACCOUNT = 1;78/* @deprecated */79public const TYPE_APPLICATION = 2;80/* @deprecated */81public const TYPE_DAEMON_USER = 3;82/* @deprecated */83public const TYPE_DAEMON_APPLICATION = 4;84/**85* The length of API key identifiers.86*/87public const IDENTIFIER_LENGTH = 16;88/**89* The length of the actual API key that is encrypted and stored90* in the database.91*/92public const KEY_LENGTH = 32;9394/**95* The table associated with the model.96*/97protected $table = 'api_keys';9899/**100* Cast values to correct type.101*/102protected $casts = [103'allowed_ips' => 'array',104'user_id' => 'int',105'last_used_at' => 'datetime',106'expires_at' => 'datetime',107self::CREATED_AT => 'datetime',108self::UPDATED_AT => 'datetime',109'r_' . AdminAcl::RESOURCE_USERS => 'int',110'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int',111'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'int',112'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'int',113'r_' . AdminAcl::RESOURCE_EGGS => 'int',114'r_' . AdminAcl::RESOURCE_LOCATIONS => 'int',115'r_' . AdminAcl::RESOURCE_NESTS => 'int',116'r_' . AdminAcl::RESOURCE_NODES => 'int',117'r_' . AdminAcl::RESOURCE_SERVERS => 'int',118];119120/**121* Fields that are mass assignable.122*/123protected $fillable = [124'identifier',125'token',126'allowed_ips',127'memo',128'last_used_at',129'expires_at',130];131132/**133* Fields that should not be included when calling toArray() or toJson()134* on this model.135*/136protected $hidden = ['token'];137138/**139* Rules to protect against invalid data entry to DB.140*/141public static array $validationRules = [142'user_id' => 'required|exists:users,id',143'key_type' => 'present|integer|min:0|max:4',144'identifier' => 'required|string|size:16|unique:api_keys,identifier',145'token' => 'required|string',146'memo' => 'required|nullable|string|max:500',147'allowed_ips' => 'nullable|array',148'allowed_ips.*' => 'string',149'last_used_at' => 'nullable|date',150'expires_at' => 'nullable|date',151'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3',152'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'integer|min:0|max:3',153'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'integer|min:0|max:3',154'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'integer|min:0|max:3',155'r_' . AdminAcl::RESOURCE_EGGS => 'integer|min:0|max:3',156'r_' . AdminAcl::RESOURCE_LOCATIONS => 'integer|min:0|max:3',157'r_' . AdminAcl::RESOURCE_NESTS => 'integer|min:0|max:3',158'r_' . AdminAcl::RESOURCE_NODES => 'integer|min:0|max:3',159'r_' . AdminAcl::RESOURCE_SERVERS => 'integer|min:0|max:3',160];161162public function can($ability)163{164// todo: this was never initially implemented and only became obvious once165// internal tooling was updated and started catching this mistake.166return false;167}168169public function cant($ability)170{171return ! $this->can($ability);172}173174/**175* Returns the user this token is assigned to.176*177* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Pterodactyl\Models\User, $this>178*/179public function user(): BelongsTo180{181return $this->belongsTo(User::class);182}183184/**185* Required for support with Laravel Sanctum.186*187* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Pterodactyl\Models\User, $this>188*189* @see \Laravel\Sanctum\Guard::supportsTokens()190*/191public function tokenable(): BelongsTo192{193return $this->user();194}195196/**197* Finds the model matching the provided token.198*/199public static function findToken(string $token): ?self200{201$identifier = substr($token, 0, self::IDENTIFIER_LENGTH);202203$model = static::where('identifier', $identifier)->first();204if (!is_null($model) && decrypt($model->token) === substr($token, strlen($identifier))) {205return $model;206}207208return null;209}210211/**212* Returns the standard prefix for API keys in the system.213*/214public static function getPrefixForType(int $type): string215{216Assert::oneOf($type, [self::TYPE_ACCOUNT, self::TYPE_APPLICATION]);217218return $type === self::TYPE_ACCOUNT ? 'ptlc_' : 'ptla_';219}220221/**222* Generates a new identifier for an API key.223*/224public static function generateTokenIdentifier(int $type): string225{226$prefix = self::getPrefixForType($type);227228return $prefix . Str::random(self::IDENTIFIER_LENGTH - strlen($prefix));229}230}231232233