2021-07-29 03:53:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Models\Traits;
|
|
|
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
|
|
use Pterodactyl\Models\PersonalAccessToken;
|
2021-08-07 21:52:58 +00:00
|
|
|
use Pterodactyl\Extensions\Laravel\Sanctum\NewAccessToken;
|
2021-07-29 03:53:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @mixin \Pterodactyl\Models\Model
|
|
|
|
*/
|
|
|
|
trait HasAccessTokens
|
|
|
|
{
|
|
|
|
use HasApiTokens;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function tokens()
|
|
|
|
{
|
|
|
|
return $this->hasMany(PersonalAccessToken::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new personal access token for the user. The token will be returned
|
|
|
|
* as the first element of the array, and the plain-text token will be the second.
|
|
|
|
*/
|
2021-08-07 21:52:58 +00:00
|
|
|
public function createToken(string $description, array $abilities = ['*']): NewAccessToken
|
2021-07-29 03:53:54 +00:00
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\PersonalAccessToken $token */
|
|
|
|
$token = $this->tokens()->create([
|
|
|
|
'user_id' => $this->id,
|
|
|
|
'description' => $description,
|
2021-07-29 04:13:49 +00:00
|
|
|
'token' => hash('sha256', $plain = Str::random(PersonalAccessToken::TOKEN_LENGTH)),
|
|
|
|
'token_id' => PersonalAccessToken::generateTokenIdentifier(),
|
2021-07-29 03:53:54 +00:00
|
|
|
'abilities' => $abilities,
|
|
|
|
]);
|
|
|
|
|
2021-08-07 21:52:58 +00:00
|
|
|
return new NewAccessToken($token, $token->token_id . $plain);
|
2021-07-29 03:53:54 +00:00
|
|
|
}
|
|
|
|
}
|