1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Auth\Cycle; |
13
|
|
|
|
14
|
|
|
use Cycle\Annotated\Annotation as Cycle; |
15
|
|
|
use Spiral\Auth\TokenInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @Cycle\Entity(table="auth_tokens") |
19
|
|
|
*/ |
20
|
|
|
final class Token implements TokenInterface |
21
|
|
|
{ |
22
|
|
|
/** @Cycle\Column(type="string(64)", primary=true) */ |
23
|
|
|
private $id; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $secretValue; |
27
|
|
|
|
28
|
|
|
/** @Cycle\Column(type="string(128)", name="hashed_value") */ |
29
|
|
|
private $hashedValue; |
30
|
|
|
|
31
|
|
|
/** @Cycle\Column(type="datetime") */ |
32
|
|
|
private $createdAt; |
33
|
|
|
|
34
|
|
|
/** @Cycle\Column(type="datetime", nullable=true) */ |
35
|
|
|
private $expiresAt; |
36
|
|
|
|
37
|
|
|
/** @Cycle\Column(type="blob") */ |
38
|
|
|
private $payload; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $id |
42
|
|
|
* @param string $secretValue |
43
|
|
|
* @param array $payload |
44
|
|
|
* @param \DateTimeImmutable $createdAt |
45
|
|
|
* @param \DateTimeInterface|null $expiresAt |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
string $id, |
49
|
|
|
string $secretValue, |
50
|
|
|
array $payload, |
51
|
|
|
\DateTimeImmutable $createdAt, |
52
|
|
|
\DateTimeInterface $expiresAt = null |
53
|
|
|
) { |
54
|
|
|
$this->id = $id; |
55
|
|
|
|
56
|
|
|
$this->secretValue = $secretValue; |
57
|
|
|
$this->hashedValue = hash('sha512', $secretValue); |
58
|
|
|
|
59
|
|
|
$this->createdAt = $createdAt; |
60
|
|
|
$this->expiresAt = $expiresAt; |
61
|
|
|
|
62
|
|
|
$this->payload = json_encode($payload); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $value |
67
|
|
|
*/ |
68
|
|
|
public function setSecretValue(string $value): void |
69
|
|
|
{ |
70
|
|
|
$this->secretValue = $value; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritDoc |
75
|
|
|
*/ |
76
|
|
|
public function getID(): string |
77
|
|
|
{ |
78
|
|
|
return sprintf('%s:%s', $this->id, $this->secretValue); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getHashedValue(): string |
85
|
|
|
{ |
86
|
|
|
return $this->hashedValue; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return \DateTimeInterface |
91
|
|
|
*/ |
92
|
|
|
public function getCreatedAt(): \DateTimeInterface |
93
|
|
|
{ |
94
|
|
|
return $this->createdAt; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritDoc |
99
|
|
|
*/ |
100
|
|
|
public function getExpiresAt(): ?\DateTimeInterface |
101
|
|
|
{ |
102
|
|
|
return $this->expiresAt; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritDoc |
107
|
|
|
*/ |
108
|
|
|
public function getPayload(): array |
109
|
|
|
{ |
110
|
|
|
return json_decode($this->payload, true); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|