current_timestamp"; } $row = []; if (empty($deviceId)) { $row = Database::getInstance()->query( << $userId, ] )->fetch(); } else { $row = Database::getInstance()->query( << $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( << $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(<< $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; } }