diff --git a/htdocs/core/modules/printing/printgcp.modules.php b/htdocs/core/modules/printing/printgcp.modules.php index af35adb57d0..2ff203be61b 100644 --- a/htdocs/core/modules/printing/printgcp.modules.php +++ b/htdocs/core/modules/printing/printgcp.modules.php @@ -102,19 +102,20 @@ class printing_printgcp extends PrintingDriver $this->db = $db; - if (!$conf->oauth->enabled) { + if (!isModEnabled('oauth')) { $this->conf[] = array( 'varname' => 'PRINTGCP_INFO', 'info' => $langs->transnoentitiesnoconv("WarningModuleNotActive", "OAuth"), 'type' => 'info', ); } else { - $keyforprovider = ''; // @FIXME + $keyforprovider = 'googleprint'; $this->google_id = getDolGlobalString('OAUTH_GOOGLE_ID'); $this->google_secret = getDolGlobalString('OAUTH_GOOGLE_SECRET'); // Token storage $storage = new DoliStorage($this->db, $conf, $keyforprovider); + //$storage->clearToken($this->OAUTH_SERVICENAME_GOOGLE); // Setup the credentials for the requests $credentials = new Credentials( @@ -123,11 +124,15 @@ class printing_printgcp extends PrintingDriver $urlwithroot.'/core/modules/oauth/google_oauthcallback.php' ); $access = ($storage->hasAccessToken($this->OAUTH_SERVICENAME_GOOGLE) ? 'HasAccessToken' : 'NoAccessToken'); + $serviceFactory = new \OAuth\ServiceFactory(); + // Call $serviceFactory->buildV2Service() that do a construct with "new OAuth/OAuth2/Service/Google()" to build the $apiService object $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array()); '@phan-var-force OAuth\OAuth2\Service\Google $apiService'; // createService is only ServiceInterface + $token_ok = true; try { + // Do a select into oauth_token to get existing token $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE); } catch (Exception $e) { $this->errors[] = $e->getMessage(); @@ -153,7 +158,11 @@ class printing_printgcp extends PrintingDriver } } if ($this->google_id != '' && $this->google_secret != '') { - $this->conf[] = array('varname' => 'PRINTGCP_INFO', 'info' => 'GoogleAuthConfigured', 'type' => 'info'); + $this->conf[] = array( + 'varname' => 'PRINTGCP_INFO', + 'info' => 'GoogleAuthConfigured', + 'type' => 'info' + ); $this->conf[] = array( 'varname' => 'PRINTGCP_TOKEN_ACCESS', 'info' => $access, @@ -188,7 +197,11 @@ class printing_printgcp extends PrintingDriver $this->conf[] = array('varname'=>'PRINTGCP_AUTHLINK', 'link'=>$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?backtourl='.urlencode(DOL_URL_ROOT.'/printing/admin/printing.php?mode=setup&driver=printgcp'), 'type'=>'authlink'); }*/ } else { - $this->conf[] = array('varname' => 'PRINTGCP_INFO', 'info' => 'GoogleAuthNotConfigured', 'type' => 'info'); + $this->conf[] = array( + 'varname' => 'PRINTGCP_INFO', + 'info' => 'GoogleAuthNotConfigured', + 'type' => 'info' + ); } } // do not display submit button @@ -264,7 +277,7 @@ class printing_printgcp extends PrintingDriver global $conf; $ret = array(); - $keyforprovider = ''; // @FIXME + $keyforprovider = 'googleprint'; // Token storage $storage = new DoliStorage($this->db, $conf, $keyforprovider); @@ -407,7 +420,7 @@ class printing_printgcp extends PrintingDriver 'contentType' => $contenttype, ); - $keyforprovider = ''; // @FIXME + $keyforprovider = 'googleprint'; // Dolibarr Token storage $storage = new DoliStorage($this->db, $conf, $keyforprovider); @@ -462,7 +475,7 @@ class printing_printgcp extends PrintingDriver $error = 0; $html = ''; - $keyforprovider = ''; // @FIXME + $keyforprovider = 'googleprint'; // Token storage $storage = new DoliStorage($this->db, $conf, $keyforprovider); diff --git a/htdocs/includes/OAuth/Common/Storage/DoliStorage.php b/htdocs/includes/OAuth/Common/Storage/DoliStorage.php index c0526e24756..61ccec7d284 100644 --- a/htdocs/includes/OAuth/Common/Storage/DoliStorage.php +++ b/htdocs/includes/OAuth/Common/Storage/DoliStorage.php @@ -164,9 +164,9 @@ class DoliStorage implements TokenStorageInterface /** * Load token and other data from a $service - * Note: Token load are cumulated into array ->tokens when other properties are erased by last loaded token. + * Note: Token load is cumulated into array $this->tokens but other properties are just erased by the last loaded token. * - * @return void + * @return boolean True if success and a token exists, false if not */ public function hasAccessToken($service) { @@ -184,14 +184,21 @@ class DoliStorage implements TokenStorageInterface $sql = "SELECT token, datec, tms, state FROM ".MAIN_DB_PREFIX."oauth_token"; $sql .= " WHERE service = '".$this->db->escape($servicepluskeyforprovider)."'"; $sql .= " AND entity IN (".getEntity('oauth_token').")"; + $resql = $this->db->query($sql); if (! $resql) { dol_print_error($this->db); } $result = $this->db->fetch_array($resql); + if ($result) { include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php'; $tokenobj = unserialize(dolDecrypt($result['token'])); + if ($result['token'] && empty($tokenobj)) { + dol_syslog("Error: We found a record for the OAuth token of '.$servicepluskeyforprovider.', we we failed to decrypt it. May be the crypt/decrypt key has been modifier ?", LOG_WARNING); + //print "Error: We found a record for the OAuth token of ".$servicepluskeyforprovider.", we we failed to decrypt it. May be the crypt/decrypt key has been modifier ?"; + return false; + } $this->token = dolDecrypt($result['token']); $this->date_creation = $this->db->jdate($result['datec']); $this->date_modification = $this->db->jdate($result['tms']); diff --git a/htdocs/printing/admin/printing.php b/htdocs/printing/admin/printing.php index d1b5583f5f4..7fe990f61f3 100644 --- a/htdocs/printing/admin/printing.php +++ b/htdocs/printing/admin/printing.php @@ -129,7 +129,7 @@ print load_fiche_titre($langs->trans("PrintingSetup"), $linkback, 'title_setup') $head = printingAdminPrepareHead($mode); if ($mode == 'setup' && $user->admin) { - print '