summaryrefslogtreecommitdiff
path: root/src/Models/Tokens.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Models/Tokens.php')
-rw-r--r--src/Models/Tokens.php137
1 files changed, 134 insertions, 3 deletions
diff --git a/src/Models/Tokens.php b/src/Models/Tokens.php
index a94c876..4ad8e1d 100644
--- a/src/Models/Tokens.php
+++ b/src/Models/Tokens.php
@@ -2,6 +2,7 @@
namespace App\Models;
+use App\Database;
use App\Support\ConnectsToDatabase;
class Tokens implements ConnectsToDatabase
@@ -9,7 +10,9 @@ 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 = "",
)
{}
@@ -19,23 +22,151 @@ class Tokens implements ConnectsToDatabase
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(): ?self
- {}
+ 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;
+ }
}