2
0
forked from Wavyzz/dolibarr

FIX use of a google print oauth token restored. No more conflict with

other tokens.
This commit is contained in:
ldestailleur
2025-07-20 17:15:14 +02:00
parent 689ca56d8f
commit 449109d0ba
3 changed files with 43 additions and 15 deletions

View File

@@ -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);

View File

@@ -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']);

View File

@@ -129,7 +129,7 @@ print load_fiche_titre($langs->trans("PrintingSetup"), $linkback, 'title_setup')
$head = printingAdminPrepareHead($mode);
if ($mode == 'setup' && $user->admin) {
print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=setup&amp;driver='.$driver.'" autocomplete="off">';
print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=setup&driver='.urlencode($driver).'" autocomplete="off">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="setconst">';
@@ -140,7 +140,7 @@ if ($mode == 'setup' && $user->admin) {
print '<table class="noborder centpercent">'."\n";
print '<tr class="liste_titre">';
print '<th>'.$langs->trans("Parameters").'</th>';
print '<th>'.$langs->trans("Value").'</th>';
print '<th></th>';
print '<th>&nbsp;</th>';
print "</tr>\n";
$submit_enabled = 0;
@@ -160,8 +160,7 @@ if ($mode == 'setup' && $user->admin) {
}
require_once $classfile;
$classname = 'printing_'.$driver;
$printer = new $classname($db);
$langs->load('printing');
$printer = new $classname($db); // Example: new printing_printgcp(). This run the construct that load the token. TODO Move this into another load function().
$i = 0;
$submit_enabled = 0;
@@ -195,7 +194,16 @@ if ($mode == 'setup' && $user->admin) {
print $langs->trans($key['varname']);
}
print '</td>';
print '<td>'.$langs->trans($key['info']).'</td>';
print '<td>';
// Example $key['info'] = $langs->trans("GoogleAuthNotConfigured");
if ($key['info'] == 'GoogleAuthNotConfigured') {
$keyforprovider = 'googleprint';
print $langs->trans($key['info']);
print '. You must use Label "'.$keyforprovider.'" with scope "cloud_print"';
} else {
print $langs->trans($key['info']);
}
print '</td>';
print '<td>';
//var_dump($key);
if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
@@ -220,7 +228,7 @@ if ($mode == 'setup' && $user->admin) {
$i++;
if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
$keyforprovider = ''; // @BUG This must be set
$keyforprovider = 'googleprint';
// Token
print '<tr class="oddeven">';