-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
RegistrationToken.php
78 lines (66 loc) · 1.95 KB
/
RegistrationToken.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\User;
use Carbon\Carbon;
use Flarum\Database\AbstractModel;
use Flarum\User\Exception\InvalidConfirmationTokenException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
/**
* @property string $token
* @property string $provider
* @property string $identifier
* @property array $user_attributes
* @property array $payload
* @property \Carbon\Carbon $created_at
*
* @method static self validOrFail(string $token)
*/
class RegistrationToken extends AbstractModel
{
protected $casts = [
'user_attributes' => 'array',
'payload' => 'array',
'created_at' => 'datetime'
];
/**
* Use a custom primary key for this model.
*
* @var bool
*/
public $incrementing = false;
protected $primaryKey = 'token';
/**
* Generate an auth token for the specified user.
*/
public static function generate(string $provider, string $identifier, array $attributes, array $payload): static
{
$token = new static;
$token->token = Str::random(40);
$token->provider = $provider;
$token->identifier = $identifier;
$token->user_attributes = $attributes;
$token->payload = $payload;
$token->created_at = Carbon::now();
return $token;
}
/**
* Find the token with the given ID, and assert that it has not expired.
*
* @throws InvalidConfirmationTokenException
*/
public function scopeValidOrFail(Builder $query, string $token): ?RegistrationToken
{
/** @var RegistrationToken|null $token */
$token = $query->find($token);
if (! $token || $token->created_at->lessThan(Carbon::now()->subDay())) {
throw new InvalidConfirmationTokenException;
}
return $token;
}
}