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
|
@ -3,6 +3,7 @@
|
|||
namespace Pterodactyl\Models;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Webmozart\Assert\Assert;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
|
@ -194,21 +195,33 @@ class ApiKey extends Model
|
|||
*/
|
||||
public static function findToken($token)
|
||||
{
|
||||
$id = Str::substr($token, 0, self::IDENTIFIER_LENGTH);
|
||||
$identifier = substr($token, 0, self::IDENTIFIER_LENGTH);
|
||||
|
||||
$model = static::where('identifier', $id)->first();
|
||||
if (!is_null($model) && decrypt($model->token) === Str::substr($token, strlen($id))) {
|
||||
$model = static::where('identifier', $identifier)->first();
|
||||
if (!is_null($model) && decrypt($model->token) === substr($token, strlen($identifier))) {
|
||||
return $model;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the standard prefix for API keys in the system.
|
||||
*/
|
||||
public static function getPrefixForType(int $type): string
|
||||
{
|
||||
Assert::oneOf($type, [self::TYPE_ACCOUNT, self::TYPE_APPLICATION]);
|
||||
|
||||
return $type === self::TYPE_ACCOUNT ? 'ptlc_' : 'ptla_';
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new identifier for an API key.
|
||||
*/
|
||||
public static function generateTokenIdentifier(): string
|
||||
public static function generateTokenIdentifier(int $type): string
|
||||
{
|
||||
return 'ptdl_' . Str::random(self::IDENTIFIER_LENGTH - 5);
|
||||
$prefix = self::getPrefixForType($type);
|
||||
|
||||
return $prefix . Str::random(self::IDENTIFIER_LENGTH - strlen($prefix));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,12 +3,12 @@
|
|||
namespace Pterodactyl\Models;
|
||||
|
||||
use Pterodactyl\Rules\Username;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Validation\Rules\In;
|
||||
use Illuminate\Auth\Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Pterodactyl\Models\Traits\HasAccessTokens;
|
||||
use Illuminate\Auth\Passwords\CanResetPassword;
|
||||
use Pterodactyl\Traits\Helpers\AvailableLanguages;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
@ -84,7 +84,7 @@ class User extends Model implements
|
|||
use Authorizable;
|
||||
use AvailableLanguages;
|
||||
use CanResetPassword;
|
||||
use HasApiTokens;
|
||||
use HasAccessTokens;
|
||||
use Notifiable;
|
||||
|
||||
public const USER_LEVEL_USER = 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue