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.
" code=".
dol_trunc(
GETPOST(
'code'), 5));
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']);
154 $token = $apiService->requestAccessToken(
GETPOST(
'code'), $state);
157 $extraparams = $token->getExtraParams();
158 $jwt = explode(
'.', $extraparams[
'id_token']);
161 if (!empty($jwt[1])) {
162 $userinfo = json_decode(base64_decode($jwt[1]),
true);
185 $backtourl = $_SESSION[
"backtourlsavedbeforeoauthjump"];
186 unset($_SESSION[
"backtourlsavedbeforeoauthjump"]);
188 header(
'Location: '.$backtourl);
191 print $e->getMessage();
197 $_SESSION[
"backtourlsavedbeforeoauthjump"] = $backtourl;
198 $_SESSION[
"oauthkeyforproviderbeforeoauthjump"] = $keyforprovider;
199 $_SESSION[
'oauthstateanticsrf'] = $state;
201 if (!preg_match(
'/^forlogin/', $state)) {
202 $apiService->setApprouvalPrompt(
'force');
208 $url = $apiService->getAuthorizationUri(array(
'state' => $state));
210 $url = $apiService->getAuthorizationUri();
214 $url .=
'&nonce='.bin2hex(random_bytes(64/8));
216 if (!preg_match(
'/^forlogin/', $state)) {
223 header(
'Location: '.$url);