/
RegistrationToken.php
95 lines (82 loc) · 2.24 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?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\Support\Str;
/**
* @property string $token
* @property string $provider
* @property string $identifier
* @property array $user_attributes
* @property array $payload
* @property \Carbon\Carbon $created_at
*/
class RegistrationToken extends AbstractModel
{
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at'];
protected $casts = [
'user_attributes' => 'array',
'payload' => 'array'
];
/**
* Use a custom primary key for this model.
*
* @var bool
*/
public $incrementing = false;
/**
* {@inheritdoc}
*/
protected $primaryKey = 'token';
/**
* Generate an auth token for the specified user.
*
* @param string $provider
* @param string $identifier
* @param array $attributes
* @param array $payload
* @return static
*/
public static function generate(string $provider, string $identifier, array $attributes, array $payload)
{
$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.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $token
*
* @throws InvalidConfirmationTokenException
*
* @return RegistrationToken
*/
public function scopeValidOrFail($query, string $token)
{
/** @var RegistrationToken $token */
$token = $query->find($token);
if (! $token || $token->created_at->lessThan(Carbon::now()->subDay())) {
throw new InvalidConfirmationTokenException;
}
return $token;
}
}