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