Fix up API handling logic for keys and set a prefix on all keys
This commit is contained in:
parent
8605d175d6
commit
b051718afe
11 changed files with 88 additions and 31 deletions
|
@ -6,6 +6,7 @@ use Illuminate\Support\Str;
|
|||
use Laravel\Sanctum\Sanctum;
|
||||
use Pterodactyl\Models\ApiKey;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Pterodactyl\Extensions\Laravel\Sanctum\NewAccessToken;
|
||||
|
||||
/**
|
||||
|
@ -13,25 +14,28 @@ use Pterodactyl\Extensions\Laravel\Sanctum\NewAccessToken;
|
|||
*/
|
||||
trait HasAccessTokens
|
||||
{
|
||||
use HasApiTokens;
|
||||
use HasApiTokens {
|
||||
tokens as private _tokens;
|
||||
createToken as private _createToken;
|
||||
}
|
||||
|
||||
public function tokens()
|
||||
public function tokens(): HasMany
|
||||
{
|
||||
return $this->hasMany(Sanctum::$personalAccessTokenModel);
|
||||
}
|
||||
|
||||
public function createToken(string $name, array $abilities = ['*'])
|
||||
public function createToken(?string $memo, ?array $ips): NewAccessToken
|
||||
{
|
||||
/** @var \Pterodactyl\Models\ApiKey $token */
|
||||
$token = $this->tokens()->create([
|
||||
$token = $this->tokens()->forceCreate([
|
||||
'user_id' => $this->id,
|
||||
'key_type' => ApiKey::TYPE_ACCOUNT,
|
||||
'identifier' => ApiKey::generateTokenIdentifier(),
|
||||
'identifier' => ApiKey::generateTokenIdentifier(ApiKey::TYPE_ACCOUNT),
|
||||
'token' => encrypt($plain = Str::random(ApiKey::KEY_LENGTH)),
|
||||
'memo' => $name,
|
||||
'allowed_ips' => [],
|
||||
'memo' => $memo ?? '',
|
||||
'allowed_ips' => $ips ?? [],
|
||||
]);
|
||||
|
||||
return new NewAccessToken($token, $token->identifier . $plain);
|
||||
return new NewAccessToken($token, $plain);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue