Files
coolify/app/Models/PersonalAccessToken.php
T
Andras BacsaiandClaude Opus 5.5 263eab5d27 feat(api-tokens): roll a token to replace its secret
Keep the description, permissions, scopes, and expiry. Move the row actions into a dropdown menu and measure the dropdown panel after it is shown, so menus at the right edge stay on screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 13:06:43 +01:00

51 lines
1.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Support\Str;
use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;
class PersonalAccessToken extends SanctumPersonalAccessToken
{
protected $fillable = [
'name',
'token',
'abilities',
'expires_at',
'api_token_expiration_warning_sent_at',
'team_id',
'scopes',
];
protected function casts(): array
{
return [
'api_token_expiration_warning_sent_at' => 'datetime',
'scopes' => 'array',
];
}
public static function generateSecret(): string
{
return sprintf(
'%s%s%s',
config('sanctum.token_prefix', ''),
$tokenEntropy = Str::random(40),
hash('crc32b', $tokenEntropy)
);
}
/**
* Replace the secret and keep the abilities, scopes, and expiry. The old secret stops working at once.
*
* @return string the new plain-text token
*/
public function roll(): string
{
$secret = self::generateSecret();
$this->forceFill(['token' => hash('sha256', $secret), 'last_used_at' => null])->save();
return $this->getKey().'|'.$secret;
}
}