56 lines
1.3 KiB
PHP
56 lines
1.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
use Laravel\Sanctum\PersonalAccessToken;
|
||
|
|
|
||
|
|
class DeviceCode extends Model
|
||
|
|
{
|
||
|
|
protected $fillable = [
|
||
|
|
'code', 'poll_token_hash', 'user_id', 'personal_access_token_id',
|
||
|
|
'expires_at', 'consumed_at',
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Human-typable code: 4 + 4 upper-case letters/digits without ambiguous
|
||
|
|
* characters, joined by a dash (e.g. WDJB-MJDH).
|
||
|
|
*/
|
||
|
|
public static function generateCode(): string
|
||
|
|
{
|
||
|
|
$alphabet = 'ABCDEFGHJKMNPQRSTUVWXYZ23456789';
|
||
|
|
|
||
|
|
$segment = fn () => substr(str_shuffle(str_repeat($alphabet, 8)), 0, 4);
|
||
|
|
|
||
|
|
return $segment().'-'.$segment();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Random secret the CLI keeps; only its hash is stored.
|
||
|
|
*/
|
||
|
|
public static function generatePollToken(): string
|
||
|
|
{
|
||
|
|
return Str::random(48);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function user(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function personalAccessToken(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(PersonalAccessToken::class, 'personal_access_token_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function casts(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'expires_at' => 'datetime',
|
||
|
|
'consumed_at' => 'datetime',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|