From ecfca4dad147dab2fafe0fff288e53adc1c3acc2 Mon Sep 17 00:00:00 2001 From: Semih Serhat Karakaya Date: Sun, 25 Oct 2020 15:06:43 -0700 Subject: [PATCH] refresh the token lifetime in every access with that token --- lib/Db/Wopi.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/Db/Wopi.php b/lib/Db/Wopi.php index 768421d5c..f71beb4b9 100644 --- a/lib/Db/Wopi.php +++ b/lib/Db/Wopi.php @@ -20,6 +20,8 @@ class Wopi extends \OCA\Richdocuments\Db { // Tokens expire after this many seconds (not defined by WOPI specs). const TOKEN_LIFETIME_SECONDS = 36000; + // If the expiry is closer than this time, it will be refreshed. + const TOKEN_REFRESH_THRESHOLD_SECONDS = 3600; const ATTR_CAN_VIEW = 0; const ATTR_CAN_UPDATE = 1; @@ -93,6 +95,10 @@ public function getWopiForToken($token) { return false; } + if ($row['expiry'] - self::TOKEN_REFRESH_THRESHOLD_SECONDS <= \time()) { + $this->refreshTokenExpiry($token); + } + return [ 'owner' => $row['owner_uid'], 'editor' => $row['editor_uid'], @@ -100,4 +106,17 @@ public function getWopiForToken($token) { 'server_host' => $row['server_host'] ]; } + + /** + * Refresh token life time + * + * @param string $token + * @return boolean + */ + protected function refreshTokenExpiry($token) { + $count = \OC::$server->getDatabaseConnection()->executeUpdate('UPDATE `*PREFIX*richdocuments_wopi` SET `expiry` = ? WHERE `token` = ?', + [\time() + self::TOKEN_LIFETIME_SECONDS, $token] + ); + return $count > 0; + } }