misc_pterodactyl-panel/app/Models/Traits/HasAccessTokens.php

42 lines
1.1 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Pterodactyl\Models\Traits;
use Illuminate\Support\Str;
use Laravel\Sanctum\Sanctum;
use Pterodactyl\Models\ApiKey;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Pterodactyl\Extensions\Laravel\Sanctum\NewAccessToken;
/**
* @mixin \Pterodactyl\Models\Model
*/
trait HasAccessTokens
{
use HasApiTokens {
tokens as private _tokens;
createToken as private _createToken;
}
public function tokens(): HasMany
{
return $this->hasMany(Sanctum::$personalAccessTokenModel);
}
public function createToken(?string $memo, ?array $ips): NewAccessToken
{
/** @var \Pterodactyl\Models\ApiKey $token */
$token = $this->tokens()->forceCreate([
'user_id' => $this->id,
'key_type' => ApiKey::TYPE_ACCOUNT,
'identifier' => ApiKey::generateTokenIdentifier(ApiKey::TYPE_ACCOUNT),
'token' => encrypt($plain = Str::random(ApiKey::KEY_LENGTH)),
'memo' => $memo ?? '',
'allowed_ips' => $ips ?? [],
]);
return new NewAccessToken($token, $plain);
}
}