Update access token generation to return more useful class

This commit is contained in:
Dane Everitt 2021-08-07 14:52:58 -07:00
parent fdd90b3be7
commit 9e0ec8fca8
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 32 additions and 9 deletions

View file

@ -0,0 +1,23 @@
<?php
namespace Pterodactyl\Extensions\Laravel\Sanctum;
use Pterodactyl\Models\PersonalAccessToken;
use Laravel\Sanctum\NewAccessToken as SanctumAccessToken;
/**
* @property \Pterodactyl\Models\PersonalAccessToken $accessToken
*/
class NewAccessToken extends SanctumAccessToken
{
/**
* NewAccessToken constructor.
*
* @noinspection PhpMissingParentConstructorInspection
*/
public function __construct(PersonalAccessToken $accessToken, string $plainTextToken)
{
$this->accessToken = $accessToken;
$this->plainTextToken = $plainTextToken;
}
}

View file

@ -37,12 +37,12 @@ class ApiKeyController extends ClientApiController
// TODO: this should accept an array of different scopes to apply as permissions
// for the token. Right now it allows any account level permission.
[$token, $plaintext] = $request->user()->createToken($request->input('description'));
$token = $request->user()->createToken($request->input('description'));
return $this->fractal->item($token)
return $this->fractal->item($token->accessToken)
->transformWith(PersonalAccessTokenTransformer::class)
->addMeta([
'secret_token' => $plaintext,
'secret_token' => $token->plainTextToken,
])
->toArray();
}

View file

@ -5,6 +5,9 @@ namespace Pterodactyl\Http\Requests\Api;
use Illuminate\Foundation\Http\FormRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* @method \Pterodactyl\Models\User user($guard = null)
*/
abstract class ApiRequest extends FormRequest
{
/**

View file

@ -5,6 +5,7 @@ namespace Pterodactyl\Models\Traits;
use Illuminate\Support\Str;
use Laravel\Sanctum\HasApiTokens;
use Pterodactyl\Models\PersonalAccessToken;
use Pterodactyl\Extensions\Laravel\Sanctum\NewAccessToken;
/**
* @mixin \Pterodactyl\Models\Model
@ -24,12 +25,8 @@ trait HasAccessTokens
/**
* 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.
*
* @param string $description
* @param string[] $abilities
* @return array
*/
public function createToken(string $description, array $abilities = ['*']): array
public function createToken(string $description, array $abilities = ['*']): NewAccessToken
{
/** @var \Pterodactyl\Models\PersonalAccessToken $token */
$token = $this->tokens()->create([
@ -40,6 +37,6 @@ trait HasAccessTokens
'abilities' => $abilities,
]);
return [$token, $token->token_id . $plain];
return new NewAccessToken($token, $token->token_id . $plain);
}
}