2021-07-28 04:23:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
2021-07-29 03:53:54 +00:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Laravel\Sanctum\Contracts\HasAbilities;
|
2022-02-20 18:07:12 +00:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2021-08-07 22:20:43 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2021-07-28 04:23:11 +00:00
|
|
|
|
2021-07-29 03:53:54 +00:00
|
|
|
class PersonalAccessToken extends Model implements HasAbilities
|
2021-07-28 04:23:11 +00:00
|
|
|
{
|
2021-08-07 22:20:43 +00:00
|
|
|
use HasFactory;
|
2022-02-20 18:07:12 +00:00
|
|
|
use SoftDeletes;
|
2021-08-07 22:20:43 +00:00
|
|
|
|
2021-07-28 04:23:11 +00:00
|
|
|
public const RESOURCE_NAME = 'personal_access_token';
|
2021-07-29 03:53:54 +00:00
|
|
|
|
2021-07-29 04:13:49 +00:00
|
|
|
/**
|
|
|
|
* The length of the raw API token.
|
|
|
|
*/
|
|
|
|
public const TOKEN_LENGTH = 32;
|
|
|
|
|
2021-07-29 03:53:54 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'user_id' => 'int',
|
|
|
|
'abilities' => 'json',
|
|
|
|
'last_used_at' => 'datetime',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'description',
|
|
|
|
'token',
|
|
|
|
'token_id',
|
|
|
|
'abilities',
|
|
|
|
];
|
|
|
|
|
2021-07-29 04:13:49 +00:00
|
|
|
public static array $validationRules = [
|
|
|
|
'token' => 'required|string',
|
|
|
|
'token_id' => 'required|string|size:16',
|
|
|
|
'description' => 'required|nullable|string|max:500',
|
|
|
|
'last_used_at' => 'nullable|date',
|
|
|
|
];
|
|
|
|
|
2021-07-29 03:53:54 +00:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
2021-07-29 04:05:23 +00:00
|
|
|
/**
|
|
|
|
* Required for support with Laravel Sanctum.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2021-08-07 23:10:24 +00:00
|
|
|
*
|
2021-07-29 04:05:23 +00:00
|
|
|
* @see \Laravel\Sanctum\Guard::supportsTokens()
|
|
|
|
*/
|
|
|
|
public function tokenable()
|
|
|
|
{
|
|
|
|
return $this->user();
|
|
|
|
}
|
|
|
|
|
2021-07-29 03:53:54 +00:00
|
|
|
/**
|
|
|
|
* Determine if the token has a given ability.
|
|
|
|
*
|
|
|
|
* @param string $ability
|
2021-08-07 23:10:24 +00:00
|
|
|
*
|
2021-07-29 03:53:54 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function can($ability)
|
|
|
|
{
|
|
|
|
return in_array('*', $this->abilities) ||
|
|
|
|
array_key_exists($ability, array_flip($this->abilities));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the token is missing a given ability.
|
|
|
|
*
|
|
|
|
* @param string $ability
|
2021-08-07 23:10:24 +00:00
|
|
|
*
|
2021-07-29 03:53:54 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function cant($ability)
|
|
|
|
{
|
|
|
|
return !$this->can($ability);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the token instance matching the given token.
|
|
|
|
*
|
|
|
|
* @param string $token
|
2021-08-07 23:10:24 +00:00
|
|
|
*
|
2021-07-29 03:53:54 +00:00
|
|
|
* @return \Pterodactyl\Models\PersonalAccessToken|null
|
|
|
|
*/
|
|
|
|
public static function findToken($token)
|
|
|
|
{
|
|
|
|
if (strpos($token, '_') === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = Str::substr($token, 0, 16);
|
|
|
|
$token = Str::substr($token, strlen($id));
|
|
|
|
|
|
|
|
return static::where('token_id', $id)->where('token', hash('sha256', $token))->first();
|
|
|
|
}
|
2021-07-29 04:13:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a new identifier for a personal access token.
|
|
|
|
*/
|
|
|
|
public static function generateTokenIdentifier(): string
|
|
|
|
{
|
|
|
|
return 'ptdl_' . Str::random(11);
|
|
|
|
}
|
2021-07-28 04:23:11 +00:00
|
|
|
}
|