diff --git a/htdocs/core/modules/oauth/getgoogleoauthcallback.php b/htdocs/core/modules/oauth/getgoogleoauthcallback.php index e514f6abe32..f1e0ddd03f3 100644 --- a/htdocs/core/modules/oauth/getgoogleoauthcallback.php +++ b/htdocs/core/modules/oauth/getgoogleoauthcallback.php @@ -30,6 +30,8 @@ use OAuth\Common\Consumer\Credentials; use OAuth\Common\Token\TokenInterface; use OAuth\OAuth2\Service\Google; +$action = GETPOST('action', 'alpha'); + /** * Create a new instance of the URI class with the current URI, stripping the query string */ @@ -59,45 +61,73 @@ $apiService = $serviceFactory->createService('Google', $credentials, $storage, a // access type needed for google refresh token $apiService->setAccessType('offline'); -//print '
'.print_r($apiService,true).'
'; -//print 'Has access Token: '.($storage->hasAccessToken('Google')?'Yes':'No').''; -//print 'Has Author State: '.($storage->hasAuthorizationState('Google')?'Yes':'No').''; -//print 'Authorization State: '.$storage->retrieveAuthorizationState('Google').''; -//print '
'.print_r($token,true).'
'; -if (! empty($_GET['code'])) { +if ($action == 'delete') { + // delete token llxHeader('',$langs->trans("OAuthSetup")); $linkback=''.$langs->trans("BackToModuleList").''; print load_fiche_titre($langs->trans("OAuthSetup"),$linkback,'title_setup'); + dol_fiche_head(); + $storage->clearToken('Google'); + dol_fiche_end(); + + +} elseif (! empty($_GET['code'])) { + llxHeader('',$langs->trans("OAuthSetup")); + + $linkback=''.$langs->trans("BackToModuleList").''; + print load_fiche_titre($langs->trans("OAuthSetup"),$linkback,'title_setup'); + + dol_fiche_head(); // retrieve the CSRF state parameter $state = isset($_GET['state']) ? $_GET['state'] : null; + print ''; + // looking for a token already stored in db + //try { + // $token = $storage->retrieveAccessToken('Google'); + // $old_token=1; + //} catch (Exception $e) { + // $old_token=0; + //} + //if ($old_token==1) { + // print ''; + // print ''; + //} + //$refreshtoken = $token->getRefreshToken(); + + // This was a callback request from service, get the token try { - $token = $storage->retrieveAccessToken('Google'); + $apiService->requestAccessToken($_GET['code'], $state); } catch (Exception $e) { print $e->getMessage(); } - //print '
'.print_r($token->getRefreshToken(),true).'
'; - //$refreshtoken = $token->getRefreshToken(); - // This was a callback request from service, get the token - $apiService->requestAccessToken($_GET['code'], $state); //print '
'.print_r($apiService,true).'
'; + // retrieve new token in db try { $token = $storage->retrieveAccessToken('Google'); + $new_token=1; } catch (Exception $e) { - print $e->getMessage(); + $new_token=0; } $newrefreshtoken = $token->getRefreshToken(); if (empty($newrefreshtoken) && ! empty($refreshtoken)) { $token->setRefreshToken($refreshtoken); $storage->storeAccessToken('Google', $token); } - print ''; + if ($new_token==1) { + print ''; + print ''; + } + //print ''; //$apiService->refreshAccessToken($token); //print '
'.print_r($apiService,true).'
'; //$token = $storage->retrieveAccessToken('Google'); //print ''; + print ''; + print '
'.$langs->trans('OldTokenStored').'
'.print_r($token,true).'
'.print_r($token,true).'
'.$langs->trans('NewTokenStored').'
'.print_r($token,true).'
'.print_r($token,true).'
'.print_r($token,true).'
Applications associées à votre compte
'; + dol_fiche_end(); } else { $url = $apiService->getAuthorizationUri(); // we go on google authorization page diff --git a/htdocs/core/modules/printing/printgcp.modules.php b/htdocs/core/modules/printing/printgcp.modules.php index 599a6645222..7010ec7de78 100644 --- a/htdocs/core/modules/printing/printgcp.modules.php +++ b/htdocs/core/modules/printing/printgcp.modules.php @@ -118,7 +118,11 @@ class printing_printgcp extends PrintingDriver $this->conf[] = array('varname'=>'PRINTGCP_TOKEN_EXPIRED', 'info'=>($expire?'Yes':'No'), 'type'=>'info'); $this->conf[] = array('varname'=>'PRINTGCP_TOKEN_EXPIRE_AT', 'info'=>(date("Y-m-d H:i:s", $token->getEndOfLife())), 'type'=>'info'); } - $this->conf[] = array('varname'=>'PRINTGCP_AUTHLINK', 'link'=>$urlwithroot.'/core/modules/oauth/getgoogleoauthcallback.php', 'type'=>'authlink'); + if (!$storage->hasAccessToken('Google')) { + $this->conf[] = array('varname'=>'PRINTGCP_AUTHLINK', 'link'=>$urlwithroot.'/core/modules/oauth/getgoogleoauthcallback.php', 'type'=>'authlink'); + } else { + $this->conf[] = array('varname'=>'PRINTGCP_DELETE_TOKEN', 'link'=>$urlwithroot.'/core/modules/oauth/getgoogleoauthcallback.php?action=delete', 'type'=>'delete'); + } } else { $this->conf[] = array('varname'=>'PRINTGCP_INFO', 'info'=>'GoogleAuthNotConfigured', 'type'=>'info'); } diff --git a/htdocs/includes/OAuth/Common/Storage/DoliStorage.php b/htdocs/includes/OAuth/Common/Storage/DoliStorage.php index 7a46d106e66..2e4b9c195a2 100644 --- a/htdocs/includes/OAuth/Common/Storage/DoliStorage.php +++ b/htdocs/includes/OAuth/Common/Storage/DoliStorage.php @@ -138,14 +138,15 @@ class DoliStorage implements TokenStorageInterface { // TODO // get previously saved tokens - $tokens = $this->session->get($this->key); + //$tokens = $this->retrieveAccessToken($service); - if (is_array($tokens) && array_key_exists($service, $tokens)) { - unset($tokens[$service]); + //if (is_array($tokens) && array_key_exists($service, $tokens)) { + // unset($tokens[$service]); - // Replace the stored tokens array - $this->conf->set($this->key, $tokens); - } + $sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token"; + $sql.= " WHERE service='".$service."'"; + $resql = $this->db->query($sql); + //} // allow chaining return $this; @@ -182,8 +183,6 @@ class DoliStorage implements TokenStorageInterface public function storeAuthorizationState($service, $state) { // TODO save or update - // get previously saved tokens - //$states = $this->conf->get($this->stateKey); if (!is_array($states)) { $states = array(); @@ -192,10 +191,22 @@ class DoliStorage implements TokenStorageInterface $states[$service] = $state; $this->states[$service] = $state; - // save - $sql = "INSERT INTO ".MAIN_DB_PREFIX."oauth_state (service, state, entity)"; - $sql.= " VALUES ('".$service."', '".$state."', 1)"; + $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."oauth_state"; + $sql.= " WHERE service='".$service."' AND entity=1"; $resql = $this->db->query($sql); + $obj = $this->db->fetch_array($resql); + if ($obj) { + // update + $sql = "UPDATE ".MAIN_DB_PREFIX."oauth_state"; + $sql.= " SET state='".$this->db->escape($state)."'"; + $sql.= " WHERE rowid='".$obj['rowid']."'"; + $resql = $this->db->query($sql); + } else { + // save + $sql = "INSERT INTO ".MAIN_DB_PREFIX."oauth_state (service, state, entity)"; + $sql.= " VALUES ('".$service."', '".$state."', 1)"; + $resql = $this->db->query($sql); + } // allow chaining return $this; diff --git a/htdocs/printing/admin/printing.php b/htdocs/printing/admin/printing.php index 2f7e2f2da7f..ac6f030ae16 100644 --- a/htdocs/printing/admin/printing.php +++ b/htdocs/printing/admin/printing.php @@ -161,6 +161,13 @@ if ($mode == 'setup' && $user->admin) print ' '; print ''."\n"; break; + case "delete": + print ''; + print ''.$langs->trans($key['varname']).''; + print ''.$langs->trans('DeleteAccess').''; + print ' '; + print ''."\n"; + break; case "info": print ''; print ''.$langs->trans($key['varname']).'';