From 75fbebbcb6b560a2dfa1d6b4e0991f89e5a23984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 11 Aug 2022 10:03:44 +0200 Subject: [PATCH 1/3] token was not refreshing --- htdocs/core/class/CMailFile.class.php | 36 ++++++++++++++++++- .../modules/printing/printgcp.modules.php | 1 - 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php index 54573f92c7a..9aa761419a2 100644 --- a/htdocs/core/class/CMailFile.class.php +++ b/htdocs/core/class/CMailFile.class.php @@ -31,6 +31,7 @@ */ use OAuth\Common\Storage\DoliStorage; +use OAuth\Common\Consumer\Credentials; /** * Class to send emails (with attachments or not) * Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext,$replyto); @@ -981,8 +982,41 @@ class CMailFile require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php'; $storage = new DoliStorage($db, $conf); + try { - $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME); + $token_ok = true; + try { + $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME); + } catch (Exception $e) { + $this->errors[] = $e->getMessage(); + $token_ok = false; + } + $expire = false; + // Is token expired or will token expire in the next 30 seconds + if (is_object($tokenobj)) { + $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30)); + } + // Token expired so we refresh it + if ($token_ok && $expire) { + try { + $credentials = new Credentials( + getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_ID'), + getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_SECRET'), + getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_URLAUTHORIZE') + ); + $serviceFactory = new \OAuth\ServiceFactory(); + $oauthname = explode('-', $OAUTH_SERVICENAME); + // ex service is Google-Emails we need only the first part Google + $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array()); + // il faut sauvegarder le refresh token car google ne le donne qu'une seule fois + $refreshtoken = $tokenobj->getRefreshToken(); + $tokenobj = $apiService->refreshAccessToken($tokenobj); + $tokenobj->setRefreshToken($refreshtoken); + $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj); + } catch (Exception $e) { + $this->errors[] = $e->getMessage(); + } + } if (is_object($tokenobj)) { $this->transport->setAuthMode('XOAUTH2'); $this->transport->setPassword($tokenobj->getAccessToken()); diff --git a/htdocs/core/modules/printing/printgcp.modules.php b/htdocs/core/modules/printing/printgcp.modules.php index 964558de8df..391f5b435d7 100644 --- a/htdocs/core/modules/printing/printgcp.modules.php +++ b/htdocs/core/modules/printing/printgcp.modules.php @@ -137,7 +137,6 @@ class printing_printgcp extends PrintingDriver $this->errors[] = $e->getMessage(); $token_ok = false; } - //var_dump($this->errors);exit; $expire = false; // Is token expired or will token expire in the next 30 seconds From b748665296de45a4805c6913c651ae88f751d5ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 11 Aug 2022 10:16:20 +0200 Subject: [PATCH 2/3] token was not refreshing --- htdocs/core/class/CMailFile.class.php | 42 ++++++++++----------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php index 9aa761419a2..69a5b0ba902 100644 --- a/htdocs/core/class/CMailFile.class.php +++ b/htdocs/core/class/CMailFile.class.php @@ -984,38 +984,28 @@ class CMailFile $storage = new DoliStorage($db, $conf); try { - $token_ok = true; - try { - $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME); - } catch (Exception $e) { - $this->errors[] = $e->getMessage(); - $token_ok = false; - } + $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME); $expire = false; // Is token expired or will token expire in the next 30 seconds if (is_object($tokenobj)) { $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30)); } // Token expired so we refresh it - if ($token_ok && $expire) { - try { - $credentials = new Credentials( - getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_ID'), - getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_SECRET'), - getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_URLAUTHORIZE') - ); - $serviceFactory = new \OAuth\ServiceFactory(); - $oauthname = explode('-', $OAUTH_SERVICENAME); - // ex service is Google-Emails we need only the first part Google - $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array()); - // il faut sauvegarder le refresh token car google ne le donne qu'une seule fois - $refreshtoken = $tokenobj->getRefreshToken(); - $tokenobj = $apiService->refreshAccessToken($tokenobj); - $tokenobj->setRefreshToken($refreshtoken); - $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj); - } catch (Exception $e) { - $this->errors[] = $e->getMessage(); - } + if (is_object($tokenobj) && $expire) { + $credentials = new Credentials( + getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_ID'), + getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_SECRET'), + getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_URLAUTHORIZE') + ); + $serviceFactory = new \OAuth\ServiceFactory(); + $oauthname = explode('-', $OAUTH_SERVICENAME); + // ex service is Google-Emails we need only the first part Google + $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array()); + // il faut sauvegarder le refresh token car google ne le donne qu'une seule fois + $refreshtoken = $tokenobj->getRefreshToken(); + $tokenobj = $apiService->refreshAccessToken($tokenobj); + $tokenobj->setRefreshToken($refreshtoken); + $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj); } if (is_object($tokenobj)) { $this->transport->setAuthMode('XOAUTH2'); From d7bccaa70c6f7ec310a65a9258f9373b7580fc05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 11 Aug 2022 10:53:15 +0200 Subject: [PATCH 3/3] token was not refreshing --- htdocs/core/class/CMailFile.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php index 69a5b0ba902..79291aacdf0 100644 --- a/htdocs/core/class/CMailFile.class.php +++ b/htdocs/core/class/CMailFile.class.php @@ -993,9 +993,9 @@ class CMailFile // Token expired so we refresh it if (is_object($tokenobj) && $expire) { $credentials = new Credentials( - getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_ID'), - getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_SECRET'), - getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_URLAUTHORIZE') + getDolGlobalString('OAUTH_'.getDolGlobalString('MAIN_MAIL_SMTPS_OAUTH_SERVICE').'_ID'), + getDolGlobalString('OAUTH_'.getDolGlobalString('MAIN_MAIL_SMTPS_OAUTH_SERVICE').'_SECRET'), + getDolGlobalString('OAUTH_'.getDolGlobalString('MAIN_MAIL_SMTPS_OAUTH_SERVICE').'_URLAUTHORIZE') ); $serviceFactory = new \OAuth\ServiceFactory(); $oauthname = explode('-', $OAUTH_SERVICENAME);