forked from Wavyzz/dolibarr
FIX use of a google print oauth token restored. No more conflict with
other tokens.
This commit is contained in:
@@ -102,19 +102,20 @@ class printing_printgcp extends PrintingDriver
|
|||||||
|
|
||||||
$this->db = $db;
|
$this->db = $db;
|
||||||
|
|
||||||
if (!$conf->oauth->enabled) {
|
if (!isModEnabled('oauth')) {
|
||||||
$this->conf[] = array(
|
$this->conf[] = array(
|
||||||
'varname' => 'PRINTGCP_INFO',
|
'varname' => 'PRINTGCP_INFO',
|
||||||
'info' => $langs->transnoentitiesnoconv("WarningModuleNotActive", "OAuth"),
|
'info' => $langs->transnoentitiesnoconv("WarningModuleNotActive", "OAuth"),
|
||||||
'type' => 'info',
|
'type' => 'info',
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
$keyforprovider = ''; // @FIXME
|
$keyforprovider = 'googleprint';
|
||||||
|
|
||||||
$this->google_id = getDolGlobalString('OAUTH_GOOGLE_ID');
|
$this->google_id = getDolGlobalString('OAUTH_GOOGLE_ID');
|
||||||
$this->google_secret = getDolGlobalString('OAUTH_GOOGLE_SECRET');
|
$this->google_secret = getDolGlobalString('OAUTH_GOOGLE_SECRET');
|
||||||
// Token storage
|
// Token storage
|
||||||
$storage = new DoliStorage($this->db, $conf, $keyforprovider);
|
$storage = new DoliStorage($this->db, $conf, $keyforprovider);
|
||||||
|
|
||||||
//$storage->clearToken($this->OAUTH_SERVICENAME_GOOGLE);
|
//$storage->clearToken($this->OAUTH_SERVICENAME_GOOGLE);
|
||||||
// Setup the credentials for the requests
|
// Setup the credentials for the requests
|
||||||
$credentials = new Credentials(
|
$credentials = new Credentials(
|
||||||
@@ -123,11 +124,15 @@ class printing_printgcp extends PrintingDriver
|
|||||||
$urlwithroot.'/core/modules/oauth/google_oauthcallback.php'
|
$urlwithroot.'/core/modules/oauth/google_oauthcallback.php'
|
||||||
);
|
);
|
||||||
$access = ($storage->hasAccessToken($this->OAUTH_SERVICENAME_GOOGLE) ? 'HasAccessToken' : 'NoAccessToken');
|
$access = ($storage->hasAccessToken($this->OAUTH_SERVICENAME_GOOGLE) ? 'HasAccessToken' : 'NoAccessToken');
|
||||||
|
|
||||||
$serviceFactory = new \OAuth\ServiceFactory();
|
$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());
|
$apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
|
||||||
'@phan-var-force OAuth\OAuth2\Service\Google $apiService'; // createService is only ServiceInterface
|
'@phan-var-force OAuth\OAuth2\Service\Google $apiService'; // createService is only ServiceInterface
|
||||||
|
|
||||||
$token_ok = true;
|
$token_ok = true;
|
||||||
try {
|
try {
|
||||||
|
// Do a select into oauth_token to get existing token
|
||||||
$token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
|
$token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->errors[] = $e->getMessage();
|
$this->errors[] = $e->getMessage();
|
||||||
@@ -153,7 +158,11 @@ class printing_printgcp extends PrintingDriver
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($this->google_id != '' && $this->google_secret != '') {
|
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(
|
$this->conf[] = array(
|
||||||
'varname' => 'PRINTGCP_TOKEN_ACCESS',
|
'varname' => 'PRINTGCP_TOKEN_ACCESS',
|
||||||
'info' => $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');
|
$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 {
|
} 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
|
// do not display submit button
|
||||||
@@ -264,7 +277,7 @@ class printing_printgcp extends PrintingDriver
|
|||||||
global $conf;
|
global $conf;
|
||||||
$ret = array();
|
$ret = array();
|
||||||
|
|
||||||
$keyforprovider = ''; // @FIXME
|
$keyforprovider = 'googleprint';
|
||||||
|
|
||||||
// Token storage
|
// Token storage
|
||||||
$storage = new DoliStorage($this->db, $conf, $keyforprovider);
|
$storage = new DoliStorage($this->db, $conf, $keyforprovider);
|
||||||
@@ -407,7 +420,7 @@ class printing_printgcp extends PrintingDriver
|
|||||||
'contentType' => $contenttype,
|
'contentType' => $contenttype,
|
||||||
);
|
);
|
||||||
|
|
||||||
$keyforprovider = ''; // @FIXME
|
$keyforprovider = 'googleprint';
|
||||||
|
|
||||||
// Dolibarr Token storage
|
// Dolibarr Token storage
|
||||||
$storage = new DoliStorage($this->db, $conf, $keyforprovider);
|
$storage = new DoliStorage($this->db, $conf, $keyforprovider);
|
||||||
@@ -462,7 +475,7 @@ class printing_printgcp extends PrintingDriver
|
|||||||
$error = 0;
|
$error = 0;
|
||||||
$html = '';
|
$html = '';
|
||||||
|
|
||||||
$keyforprovider = ''; // @FIXME
|
$keyforprovider = 'googleprint';
|
||||||
|
|
||||||
// Token storage
|
// Token storage
|
||||||
$storage = new DoliStorage($this->db, $conf, $keyforprovider);
|
$storage = new DoliStorage($this->db, $conf, $keyforprovider);
|
||||||
|
|||||||
@@ -164,9 +164,9 @@ class DoliStorage implements TokenStorageInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Load token and other data from a $service
|
* 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)
|
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 = "SELECT token, datec, tms, state FROM ".MAIN_DB_PREFIX."oauth_token";
|
||||||
$sql .= " WHERE service = '".$this->db->escape($servicepluskeyforprovider)."'";
|
$sql .= " WHERE service = '".$this->db->escape($servicepluskeyforprovider)."'";
|
||||||
$sql .= " AND entity IN (".getEntity('oauth_token').")";
|
$sql .= " AND entity IN (".getEntity('oauth_token').")";
|
||||||
|
|
||||||
$resql = $this->db->query($sql);
|
$resql = $this->db->query($sql);
|
||||||
if (! $resql) {
|
if (! $resql) {
|
||||||
dol_print_error($this->db);
|
dol_print_error($this->db);
|
||||||
}
|
}
|
||||||
$result = $this->db->fetch_array($resql);
|
$result = $this->db->fetch_array($resql);
|
||||||
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
|
include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
|
||||||
$tokenobj = unserialize(dolDecrypt($result['token']));
|
$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->token = dolDecrypt($result['token']);
|
||||||
$this->date_creation = $this->db->jdate($result['datec']);
|
$this->date_creation = $this->db->jdate($result['datec']);
|
||||||
$this->date_modification = $this->db->jdate($result['tms']);
|
$this->date_modification = $this->db->jdate($result['tms']);
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ print load_fiche_titre($langs->trans("PrintingSetup"), $linkback, 'title_setup')
|
|||||||
$head = printingAdminPrepareHead($mode);
|
$head = printingAdminPrepareHead($mode);
|
||||||
|
|
||||||
if ($mode == 'setup' && $user->admin) {
|
if ($mode == 'setup' && $user->admin) {
|
||||||
print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=setup&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="token" value="'.newToken().'">';
|
||||||
print '<input type="hidden" name="action" value="setconst">';
|
print '<input type="hidden" name="action" value="setconst">';
|
||||||
|
|
||||||
@@ -140,7 +140,7 @@ if ($mode == 'setup' && $user->admin) {
|
|||||||
print '<table class="noborder centpercent">'."\n";
|
print '<table class="noborder centpercent">'."\n";
|
||||||
print '<tr class="liste_titre">';
|
print '<tr class="liste_titre">';
|
||||||
print '<th>'.$langs->trans("Parameters").'</th>';
|
print '<th>'.$langs->trans("Parameters").'</th>';
|
||||||
print '<th>'.$langs->trans("Value").'</th>';
|
print '<th></th>';
|
||||||
print '<th> </th>';
|
print '<th> </th>';
|
||||||
print "</tr>\n";
|
print "</tr>\n";
|
||||||
$submit_enabled = 0;
|
$submit_enabled = 0;
|
||||||
@@ -160,8 +160,7 @@ if ($mode == 'setup' && $user->admin) {
|
|||||||
}
|
}
|
||||||
require_once $classfile;
|
require_once $classfile;
|
||||||
$classname = 'printing_'.$driver;
|
$classname = 'printing_'.$driver;
|
||||||
$printer = new $classname($db);
|
$printer = new $classname($db); // Example: new printing_printgcp(). This run the construct that load the token. TODO Move this into another load function().
|
||||||
$langs->load('printing');
|
|
||||||
|
|
||||||
$i = 0;
|
$i = 0;
|
||||||
$submit_enabled = 0;
|
$submit_enabled = 0;
|
||||||
@@ -195,7 +194,16 @@ if ($mode == 'setup' && $user->admin) {
|
|||||||
print $langs->trans($key['varname']);
|
print $langs->trans($key['varname']);
|
||||||
}
|
}
|
||||||
print '</td>';
|
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>';
|
print '<td>';
|
||||||
//var_dump($key);
|
//var_dump($key);
|
||||||
if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
|
if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
|
||||||
@@ -220,7 +228,7 @@ if ($mode == 'setup' && $user->admin) {
|
|||||||
$i++;
|
$i++;
|
||||||
|
|
||||||
if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
|
if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
|
||||||
$keyforprovider = ''; // @BUG This must be set
|
$keyforprovider = 'googleprint';
|
||||||
|
|
||||||
// Token
|
// Token
|
||||||
print '<tr class="oddeven">';
|
print '<tr class="oddeven">';
|
||||||
|
|||||||
Reference in New Issue
Block a user