Google Cloud Print

This commit is contained in:
frederic34
2015-11-05 18:04:00 +01:00
parent 1927f47eaa
commit 3ef6a92bb1
4 changed files with 77 additions and 25 deletions

View File

@@ -30,6 +30,8 @@ use OAuth\Common\Consumer\Credentials;
use OAuth\Common\Token\TokenInterface; use OAuth\Common\Token\TokenInterface;
use OAuth\OAuth2\Service\Google; 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 * 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 // access type needed for google refresh token
$apiService->setAccessType('offline'); $apiService->setAccessType('offline');
//print '<pre>'.print_r($apiService,true).'</pre>'; if ($action == 'delete') {
//print 'Has access Token: '.($storage->hasAccessToken('Google')?'Yes':'No').'</ br>'; // delete token
//print 'Has Author State: '.($storage->hasAuthorizationState('Google')?'Yes':'No').'</ br>';
//print 'Authorization State: '.$storage->retrieveAuthorizationState('Google').'</ br>';
//print '<td><pre>'.print_r($token,true).'</pre></td>';
if (! empty($_GET['code'])) {
llxHeader('',$langs->trans("OAuthSetup")); llxHeader('',$langs->trans("OAuthSetup"));
$linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>'; $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
print load_fiche_titre($langs->trans("OAuthSetup"),$linkback,'title_setup'); 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='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
print load_fiche_titre($langs->trans("OAuthSetup"),$linkback,'title_setup');
dol_fiche_head();
// retrieve the CSRF state parameter // retrieve the CSRF state parameter
$state = isset($_GET['state']) ? $_GET['state'] : null; $state = isset($_GET['state']) ? $_GET['state'] : null;
print '<table>';
// 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 '<tr><td>'.$langs->trans('OldTokenStored').'</td><td></td></tr>';
// print '<tr><td><pre>'.print_r($token,true).'</pre></td></tr>';
//}
//$refreshtoken = $token->getRefreshToken();
// This was a callback request from service, get the token
try { try {
$token = $storage->retrieveAccessToken('Google'); $apiService->requestAccessToken($_GET['code'], $state);
} catch (Exception $e) { } catch (Exception $e) {
print $e->getMessage(); print $e->getMessage();
} }
//print '<pre>'.print_r($token->getRefreshToken(),true).'</pre>';
//$refreshtoken = $token->getRefreshToken();
// This was a callback request from service, get the token
$apiService->requestAccessToken($_GET['code'], $state);
//print '<pre>'.print_r($apiService,true).'</pre>'; //print '<pre>'.print_r($apiService,true).'</pre>';
// retrieve new token in db
try { try {
$token = $storage->retrieveAccessToken('Google'); $token = $storage->retrieveAccessToken('Google');
$new_token=1;
} catch (Exception $e) { } catch (Exception $e) {
print $e->getMessage(); $new_token=0;
} }
$newrefreshtoken = $token->getRefreshToken(); $newrefreshtoken = $token->getRefreshToken();
if (empty($newrefreshtoken) && ! empty($refreshtoken)) { if (empty($newrefreshtoken) && ! empty($refreshtoken)) {
$token->setRefreshToken($refreshtoken); $token->setRefreshToken($refreshtoken);
$storage->storeAccessToken('Google', $token); $storage->storeAccessToken('Google', $token);
} }
print '<td><pre>'.print_r($token,true).'</pre></td>'; if ($new_token==1) {
print '<tr><td>'.$langs->trans('NewTokenStored').'</td><td></td></tr>';
print '<tr><td><pre>'.print_r($token,true).'</pre></td></tr>';
}
//print '<td><pre>'.print_r($token,true).'</pre></td>';
//$apiService->refreshAccessToken($token); //$apiService->refreshAccessToken($token);
//print '<pre>'.print_r($apiService,true).'</pre>'; //print '<pre>'.print_r($apiService,true).'</pre>';
//$token = $storage->retrieveAccessToken('Google'); //$token = $storage->retrieveAccessToken('Google');
//print '<td><pre>'.print_r($token,true).'</pre></td>'; //print '<td><pre>'.print_r($token,true).'</pre></td>';
print '<td><a href="https://security.google.com/settings/security/permissions" target="_blank">Applications associées à votre compte</a></td>';
print '</table>';
dol_fiche_end();
} else { } else {
$url = $apiService->getAuthorizationUri(); $url = $apiService->getAuthorizationUri();
// we go on google authorization page // we go on google authorization page

View File

@@ -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_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_TOKEN_EXPIRE_AT', 'info'=>(date("Y-m-d H:i:s", $token->getEndOfLife())), 'type'=>'info');
} }
if (!$storage->hasAccessToken('Google')) {
$this->conf[] = array('varname'=>'PRINTGCP_AUTHLINK', 'link'=>$urlwithroot.'/core/modules/oauth/getgoogleoauthcallback.php', 'type'=>'authlink'); $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 { } else {
$this->conf[] = array('varname'=>'PRINTGCP_INFO', 'info'=>'GoogleAuthNotConfigured', 'type'=>'info'); $this->conf[] = array('varname'=>'PRINTGCP_INFO', 'info'=>'GoogleAuthNotConfigured', 'type'=>'info');
} }

View File

@@ -138,14 +138,15 @@ class DoliStorage implements TokenStorageInterface
{ {
// TODO // TODO
// get previously saved tokens // get previously saved tokens
$tokens = $this->session->get($this->key); //$tokens = $this->retrieveAccessToken($service);
if (is_array($tokens) && array_key_exists($service, $tokens)) { //if (is_array($tokens) && array_key_exists($service, $tokens)) {
unset($tokens[$service]); // unset($tokens[$service]);
// Replace the stored tokens array $sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token";
$this->conf->set($this->key, $tokens); $sql.= " WHERE service='".$service."'";
} $resql = $this->db->query($sql);
//}
// allow chaining // allow chaining
return $this; return $this;
@@ -182,8 +183,6 @@ class DoliStorage implements TokenStorageInterface
public function storeAuthorizationState($service, $state) public function storeAuthorizationState($service, $state)
{ {
// TODO save or update // TODO save or update
// get previously saved tokens
//$states = $this->conf->get($this->stateKey);
if (!is_array($states)) { if (!is_array($states)) {
$states = array(); $states = array();
@@ -192,10 +191,22 @@ class DoliStorage implements TokenStorageInterface
$states[$service] = $state; $states[$service] = $state;
$this->states[$service] = $state; $this->states[$service] = $state;
$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 // save
$sql = "INSERT INTO ".MAIN_DB_PREFIX."oauth_state (service, state, entity)"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."oauth_state (service, state, entity)";
$sql.= " VALUES ('".$service."', '".$state."', 1)"; $sql.= " VALUES ('".$service."', '".$state."', 1)";
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
}
// allow chaining // allow chaining
return $this; return $this;

View File

@@ -161,6 +161,13 @@ if ($mode == 'setup' && $user->admin)
print '<td>&nbsp;</td>'; print '<td>&nbsp;</td>';
print '</tr>'."\n"; print '</tr>'."\n";
break; break;
case "delete":
print '<tr '.$bc[$var].'>';
print '<td>'.$langs->trans($key['varname']).'</td>';
print '<td class="button"><a href="'.$key['link'].'">'.$langs->trans('DeleteAccess').'</a></td>';
print '<td>&nbsp;</td>';
print '</tr>'."\n";
break;
case "info": case "info":
print '<tr '.$bc[$var].'>'; print '<tr '.$bc[$var].'>';
print '<td'.($key['required']?' class=required':'').'>'.$langs->trans($key['varname']).'</td>'; print '<td'.($key['required']?' class=required':'').'>'.$langs->trans($key['varname']).'</td>';