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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
<?php
namespace App\Models;
use App\Database;
use App\Support\ConnectsToDatabase;
class Tokens implements ConnectsToDatabase
{
public function __construct(
private string $accessToken,
private string $refreshToken,
private \DateTime $expiresAt,
private string $userId,
private \DateTime $createdAt,
private string $deviceId = "",
)
{}
public static function fromDatabase(array $row): self
{
return new self(
$row["access_token"],
$row["refresh_token"],
new \DateTime($row["expires_at"]),
$row["user_id"],
new \DateTime($row["created_at"]),
$row["device_id"],
);
}
public static function fetch(string $userId = "", string $deviceId = "", bool $isExpired = false): ?self
{
if (empty($userId)) {
throw new \InvalidArgumentException("missing user id");
}
$isExpiredSql = "";
if ($isExpired) {
$isExpiredSql = "and expires_at <= current_timestamp";
} else {
$isExpiredSql = "and expires_at > current_timestamp";
}
$row = [];
if (empty($deviceId)) {
$row = Database::getInstance()->query(
<<<SQL
select * from tokens
where user_id=:user_id and device_id is null
$isExpiredSql
SQL,
[
"user_id" => $userId,
]
)->fetch();
} else {
$row = Database::getInstance()->query(
<<<SQL
select * from tokens
where user_id=:user_id and device_id=:device_id
$isExpiredSql
SQL,
[
"user_id" => $userId,
"device_id" => $deviceId,
]
)->fetch();
}
if (empty($row)) {
return null;
}
return self::fromDatabase($row);
}
public static function fetchAll(): array
{}
public static function fetchWithRefreshToken(string $refreshToken): ?self
{
$row = Database::getInstance()->query(
<<<SQL
select * from tokens
where refresh_token=:refresh_token
order by created_at desc
SQL,
[
"refresh_token" => $refreshToken,
]
)->fetch();
if (empty($row)) {
return null;
}
return self::fromDatabase($row);
}
public static function new(string $userId, string $deviceId, string $expiryTime = ""): self
{
$expiryTime = ($expiryTime ?: ($_ENV["TOKEN_DEFAULT_LIFETIME"] ?? "")) ?: "5min";
return new self(
md5($userId . random_bytes(512)),
md5($userId . random_bytes(512)),
(new \DateTime("now"))->modify("+$expiryTime"),
$userId,
new \DateTime("now"),
$deviceId,
);
}
public function insert(): bool
{
return !! Database::getInstance()->query(<<<SQL
insert into tokens (access_token, refresh_token, expires_at, user_id, device_id, created_at)
values (:access_token, :refresh_token, to_timestamp(:expires_at), :user_id, :device_id, to_timestamp(:created_at))
SQL, [
"access_token" => $this->accessToken,
"refresh_token" => $this->refreshToken,
"expires_at" => $this->expiresAt->format("U.v"),
"user_id" => $this->userId,
"device_id" => $this->deviceId,
"created_at" => $this->createdAt->format("U.v"),
]);
}
public function update(): bool
{}
public function delete(): bool
{}
public function getExpiresIn(): int
{
return intval(
($this->expiresAt->format("U.v") - (new \DateTime("now"))->format("U.v")) * 1000.0
);
}
public function getAccessToken(): string
{
return $this->accessToken;
}
public function getRefreshToken(): string
{
return $this->refreshToken;
}
public function getExpiresAt(): int
{
return $this->expiresAt;
}
public function getUserId(): string
{
return $this->userId;
}
public function getDeviceId(): string
{
return $this->deviceId;
}
public function getCreatedAt(): int
{
return $this->createdAt;
}
}
|