29 require
'../../../main.inc.php';
30 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
31 use OAuth\Common\Storage\DoliStorage;
32 use OAuth\Common\Consumer\Credentials;
33 use OAuth\OAuth2\Service\Google;
36 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
37 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
42 $action =
GETPOST(
'action',
'aZ09');
43 $backtourl =
GETPOST(
'backtourl',
'alpha');
44 $keyforprovider =
GETPOST(
'keyforprovider',
'aZ09');
45 if (!
GETPOSTISSET(
'keyforprovider') && !empty($_SESSION[
"oauthkeyforproviderbeforeoauthjump"]) && (
GETPOST(
'code') || $action ==
'delete')) {
47 $keyforprovider = $_SESSION[
"oauthkeyforproviderbeforeoauthjump"];
57 $currentUri =
$uriFactory->createFromAbsolute($urlwithroot.
'/core/modules/oauth/google_oauthcallback.php');
65 $serviceFactory = new \OAuth\ServiceFactory();
66 $httpClient = new \OAuth\Common\Http\Client\CurlClient();
70 $serviceFactory->setHttpClient($httpClient);
73 $keyforparamid =
'OAUTH_GOOGLE'.($keyforprovider ?
'-'.$keyforprovider :
'').
'_ID';
74 $keyforparamsecret =
'OAUTH_GOOGLE'.($keyforprovider ?
'-'.$keyforprovider :
'').
'_SECRET';
75 $credentials =
new Credentials(
78 $currentUri->getAbsoluteUri()
82 $statewithscopeonly =
'';
83 $statewithanticsrfonly =
'';
85 $requestedpermissionsarray = array();
88 $statewithscopeonly = preg_replace(
'/\-.*$/',
'', $state);
89 $requestedpermissionsarray = explode(
',', $statewithscopeonly);
90 $statewithanticsrfonly = preg_replace(
'/^.*\-/',
'', $state);
93 if ($action !=
'delete' && (empty($statewithscopeonly) || empty($requestedpermissionsarray))) {
95 header(
'Location: '.$backtourl);
103 $storage =
new DoliStorage($db, $conf, $keyforprovider);
108 $apiService = $serviceFactory->createService(
'Google', $credentials, $storage, $requestedpermissionsarray);
112 $apiService->setAccessType(
'offline');
115 $langs->load(
"oauth");
118 accessforbidden(
'Setup of service is not complete. Customer ID is missing');
121 accessforbidden(
'Setup of service is not complete. Secret key is missing');
130 if ($action ==
'delete') {
131 $storage->clearToken(
'Google');
135 header(
'Location: '.$backtourl);
140 dol_syslog(
"We are coming from the oauth provider page keyforprovider=".$keyforprovider);
143 if (isset($_SESSION[
'oauthstateanticsrf']) && $state != $_SESSION[
'oauthstateanticsrf']) {
144 print
'Value for state = '.dol_escape_htmltag($state).
' differs from value in $_SESSION["oauthstateanticsrf"]. Code is refused.';
145 unset($_SESSION[
'oauthstateanticsrf']);
155 $token = $apiService->requestAccessToken(
GETPOST(
'code'), $state);
158 $extraparams = $token->getExtraParams();
159 $jwt = explode(
'.', $extraparams[
'id_token']);
162 if (!empty($jwt[1])) {
163 $userinfo = json_decode(base64_decode($jwt[1]),
true);
186 $backtourl = $_SESSION[
"backtourlsavedbeforeoauthjump"];
187 unset($_SESSION[
"backtourlsavedbeforeoauthjump"]);
189 header(
'Location: '.$backtourl);
192 print $e->getMessage();
198 $_SESSION[
"backtourlsavedbeforeoauthjump"] = $backtourl;
199 $_SESSION[
"oauthkeyforproviderbeforeoauthjump"] = $keyforprovider;
200 $_SESSION[
'oauthstateanticsrf'] = $state;
202 if (!preg_match(
'/^forlogin/', $state)) {
203 $apiService->setApprouvalPrompt(
'force');
209 $url = $apiService->getAuthorizationUri(array(
'state' => $state));
211 $url = $apiService->getAuthorizationUri();
215 $url .=
'&nonce='.bin2hex(random_bytes(64/8));
217 if (!preg_match(
'/^forlogin/', $state)) {
222 header(
'Location: '.$url);
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!GETPOSTISSET('keyforprovider') &&!empty($_SESSION["oauthkeyforproviderbeforeoauthjump"]) &&(GETPOST('code')|| $action=='delete')) $uriFactory
Create a new instance of the URI class with the current URI, stripping the query string.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.