Exemplo n.º 1
1
 public function createService($serviceName)
 {
     if (!$this->storage->hasConfig($serviceName)) {
         throw new \InvalidArgumentException('Service ' . $serviceName . ' unknown or not configured!');
     }
     if (array_key_exists($serviceName, $this->objectCache)) {
         return $this->objectCache[$serviceName];
     }
     if (null === $this->storage->getConsumerKey($serviceName)) {
         throw new \InvalidArgumentException('Service ' . $serviceName . ' has a configuration file, but the consumer key is not defined!');
     }
     if (null === $this->storage->getConsumerSecret($serviceName)) {
         throw new \InvalidArgumentException('Service ' . $serviceName . ' has a configuration file, but the consumer service is not defined!');
     }
     if (null === $this->storage->getCallbackUrl($serviceName)) {
         throw new \InvalidArgumentException('Service ' . $serviceName . ' has a configuration file, but the callback url is not defined!');
     }
     $callbackUrl = $this->storage->getCallbackUrl($serviceName);
     /** @var $serviceFactory \OAuth\ServiceFactory An OAuth service factory. */
     $serviceFactory = new \OAuth\ServiceFactory();
     $uriFactory = new \OAuth\Common\Http\Uri\UriFactory();
     $currentUri = $uriFactory->createFromAbsolute($callbackUrl);
     $credentials = new Credentials($this->storage->getConsumerKey($serviceName), $this->storage->getConsumerSecret($serviceName), $currentUri->getAbsoluteUri());
     $this->objectCache[$serviceName] = $serviceFactory->createService($serviceName, $credentials, $this->storage, $this->storage->getScope($serviceName));
     return $this->objectCache[$serviceName];
 }
Exemplo n.º 2
0
use OAuth\Common\Consumer\Credentials;
use OAuth\OAuth2\Service\Google;
// Define $urlwithroot
$urlwithouturlroot = preg_replace('/' . preg_quote(DOL_URL_ROOT, '/') . '$/i', '', trim($dolibarr_main_url_root));
$urlwithroot = $urlwithouturlroot . DOL_URL_ROOT;
// This is to use external domain name found into config file
//$urlwithroot=DOL_MAIN_URL_ROOT;					// This is to use same domain name than current
$action = GETPOST('action', 'alpha');
$backtourl = GETPOST('backtourl', 'alpha');
/**
 * Create a new instance of the URI class with the current URI, stripping the query string
 */
$uriFactory = new \OAuth\Common\Http\Uri\UriFactory();
//$currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER);
//$currentUri->setQuery('');
$currentUri = $uriFactory->createFromAbsolute($urlwithroot . '/core/modules/oauth/google_oauthcallback.php');
/**
 * Load the credential for the service
 */
/** @var $serviceFactory \OAuth\ServiceFactory An OAuth service factory. */
$serviceFactory = new \OAuth\ServiceFactory();
$httpClient = new \OAuth\Common\Http\Client\CurlClient();
// TODO Set options for proxy and timeout
// $params=array('CURLXXX'=>value, ...)
//$httpClient->setCurlParameters($params);
$serviceFactory->setHttpClient($httpClient);
// Dolibarr storage
$storage = new DoliStorage($db, $conf);
// Setup the credentials for the requests
$credentials = new Credentials($conf->global->OAUTH_GOOGLE_ID, $conf->global->OAUTH_GOOGLE_SECRET, $currentUri->getAbsoluteUri());
$requestedpermissionsarray = array();