1
1
mirror of https://github.com/pterodactyl/panel.git synced 2024-11-26 02:52:30 +01:00
Pterodactyl-Panel/app/Models/Traits/HasAccessTokens.php

42 lines
1.1 KiB
PHP

<?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);
}
}