Initial pass at implementing Laravel Sanctum for authorization on the API
This commit is contained in:
parent
e313dff674
commit
bd37978a98
13 changed files with 324 additions and 220 deletions
37
app/Models/Traits/HasAccessTokens.php
Normal file
37
app/Models/Traits/HasAccessTokens.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Models\Traits;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Pterodactyl\Models\ApiKey;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Pterodactyl\Extensions\Laravel\Sanctum\NewAccessToken;
|
||||
|
||||
/**
|
||||
* @mixin \Pterodactyl\Models\Model
|
||||
*/
|
||||
trait HasAccessTokens
|
||||
{
|
||||
use HasApiTokens;
|
||||
|
||||
public function tokens()
|
||||
{
|
||||
return $this->hasMany(Sanctum::$personalAccessTokenModel);
|
||||
}
|
||||
|
||||
public function createToken(string $name, array $abilities = ['*'])
|
||||
{
|
||||
/** @var \Pterodactyl\Models\ApiKey $token */
|
||||
$token = $this->tokens()->create([
|
||||
'user_id' => $this->id,
|
||||
'key_type' => ApiKey::TYPE_ACCOUNT,
|
||||
'identifier' => ApiKey::generateTokenIdentifier(),
|
||||
'token' => encrypt($plain = Str::random(ApiKey::KEY_LENGTH)),
|
||||
'memo' => $name,
|
||||
'allowed_ips' => [],
|
||||
]);
|
||||
|
||||
return new NewAccessToken($token, $token->identifier . $plain);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue