1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2021. Hilmi Erdem Keren |
4
|
|
|
* license MIT |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Erdemkeren\Otp; |
8
|
|
|
|
9
|
|
|
use Carbon\Carbon; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class OtpToken. |
13
|
|
|
*/ |
14
|
|
|
class OtpToken |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* OtpToken constructor. |
18
|
|
|
* |
19
|
|
|
* @param array $attributes |
20
|
|
|
*/ |
21
|
|
|
public function __construct(private array $attributes) |
22
|
|
|
{ |
23
|
|
|
if (! array_key_exists('created_at', $this->attributes)) { |
24
|
|
|
$this->attributes['created_at'] = $this->getNow(); |
25
|
|
|
$this->attributes['updated_at'] = $this->getNow(); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the plain text. |
31
|
|
|
* |
32
|
|
|
* @return string|null |
33
|
|
|
*/ |
34
|
|
|
public function plainText(): ?string |
35
|
|
|
{ |
36
|
|
|
return $this->getAttributeValue('plain_text'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the cipher text of the token. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function cipherText(): string |
45
|
|
|
{ |
46
|
|
|
return $this->getAttributeValue('cipher_text'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the expiry time of the token. |
51
|
|
|
* |
52
|
|
|
* @return int |
53
|
|
|
*/ |
54
|
|
|
public function expiryTime(): int |
55
|
|
|
{ |
56
|
|
|
return $this->getAttributeValue('expiry_time'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the time left for the expiration. |
61
|
|
|
* |
62
|
|
|
* @return int |
63
|
|
|
*/ |
64
|
|
|
public function timeLeft(): int |
65
|
|
|
{ |
66
|
|
|
return $this->getNow()->diffInSeconds($this->expiresAt(), false); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the expiry date. |
71
|
|
|
* |
72
|
|
|
* @return Carbon |
73
|
|
|
*/ |
74
|
|
|
public function expiresAt(): Carbon |
75
|
|
|
{ |
76
|
|
|
return (clone $this->createdAt())->addSeconds($this->expiryTime()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the authenticable id. |
81
|
|
|
* |
82
|
|
|
* @return int|string |
83
|
|
|
*/ |
84
|
|
|
public function authenticableId(): int|string |
85
|
|
|
{ |
86
|
|
|
return $this->getAttributeValue('authenticable_id'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the date token was created. |
91
|
|
|
* |
92
|
|
|
* @return Carbon |
93
|
|
|
*/ |
94
|
|
|
public function createdAt(): Carbon |
95
|
|
|
{ |
96
|
|
|
return new Carbon($this->getAttributeValue('created_at')); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the last date the token was updated. |
101
|
|
|
* |
102
|
|
|
* @return Carbon |
103
|
|
|
*/ |
104
|
|
|
public function updatedAt(): Carbon |
105
|
|
|
{ |
106
|
|
|
return new Carbon($this->getAttributeValue('updated_at')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get a new instance of the token |
111
|
|
|
* with an extended expiry time. |
112
|
|
|
* |
113
|
|
|
* @param int $secs |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function extend(int $secs): self |
117
|
|
|
{ |
118
|
|
|
return new static(array_merge($this->attributes, [ |
119
|
|
|
'expiry_time' => $this->expiryTime() + $secs, |
120
|
|
|
'updated_at' => $this->getNow(), |
121
|
|
|
])); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get a new instance of the token |
126
|
|
|
* with a refreshed expiry time. |
127
|
|
|
*/ |
128
|
|
|
public function refresh(): self |
129
|
|
|
{ |
130
|
|
|
return $this->extend($this->getNow()->diffInSeconds($this->updatedAt())); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get a new instance of the token |
135
|
|
|
* that is expired. |
136
|
|
|
*/ |
137
|
|
|
public function invalidate(): self |
138
|
|
|
{ |
139
|
|
|
return new static(array_merge($this->attributes, [ |
140
|
|
|
'expiry_time' => 0, |
141
|
|
|
'updated_at' => $this->getNow(), |
142
|
|
|
])); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Determine if the token is expired or not. |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function expired(): bool |
151
|
|
|
{ |
152
|
|
|
return $this->timeLeft() <= 0; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get the token without the plain text. |
157
|
|
|
* |
158
|
|
|
* return self |
159
|
|
|
*/ |
160
|
|
|
public function withoutPlainText(): self |
161
|
|
|
{ |
162
|
|
|
return new static(array_filter(array_merge($this->attributes, [ |
163
|
|
|
'plain_text' => null, |
164
|
|
|
]))); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the array representation of the token. |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
public function toArray(): array |
173
|
|
|
{ |
174
|
|
|
return $this->attributes; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the date time at the moment. |
179
|
|
|
* |
180
|
|
|
* @return Carbon |
181
|
|
|
*/ |
182
|
|
|
private function getNow(): Carbon |
183
|
|
|
{ |
184
|
|
|
return Carbon::now(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the value of an attribute. |
189
|
|
|
* |
190
|
|
|
* @param string $key |
191
|
|
|
* @return mixed |
192
|
|
|
*/ |
193
|
|
|
private function getAttributeValue(string $key): mixed |
194
|
|
|
{ |
195
|
|
|
return array_key_exists($key, $this->attributes) |
196
|
|
|
? $this->attributes[$key] |
197
|
|
|
: null; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|