27require
'../../../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
29require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
39use OAuth\Common\Storage\DoliStorage;
40use OAuth\Common\Consumer\Credentials;
44$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
48$action =
GETPOST(
'action',
'aZ09');
49$backtourl =
GETPOST(
'backtourl',
'alpha');
50$keyforprovider =
GETPOST(
'keyforprovider',
'aZ09');
51if (empty($keyforprovider) && !empty($_SESSION[
"oauthkeyforproviderbeforeoauthjump"]) && (
GETPOST(
'code') || $action ==
'delete')) {
52 $keyforprovider = $_SESSION[
"oauthkeyforproviderbeforeoauthjump"];
54$genericstring =
'MICROSOFT';
62$currentUri =
$uriFactory->createFromAbsolute($urlwithroot.
'/core/modules/oauth/microsoft_oauthcallback.php');
70$serviceFactory = new \OAuth\ServiceFactory();
71$httpClient = new \OAuth\Common\Http\Client\CurlClient();
75$serviceFactory->setHttpClient($httpClient);
78$keyforparamid =
'OAUTH_'.$genericstring.($keyforprovider ?
'-'.$keyforprovider :
'').
'_ID';
79$keyforparamsecret =
'OAUTH_'.$genericstring.($keyforprovider ?
'-'.$keyforprovider :
'').
'_SECRET';
80$keyforparamtenant =
'OAUTH_'.$genericstring.($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
85$credentials =
new Credentials(
88 $currentUri->getAbsoluteUri()
93$requestedpermissionsarray = array();
95 $requestedpermissionsarray = explode(
',', $state);
97if ($action !=
'delete' && empty($requestedpermissionsarray)) {
98 $langs->load(
"oauth");
99 print $langs->trans(
"OAuthErrorNoScopeInState",
'OAUTH_'.$genericstring.($keyforprovider ?
'-'.$keyforprovider :
'').
'_SCOPE');
110 $nameofservice = ucfirst(strtolower($genericstring));
111 $apiService = $serviceFactory->createService($nameofservice, $credentials, $storage, $requestedpermissionsarray);
112 '@phan-var-force OAuth\OAuth2\Service\AbstractService|OAuth\OAuth1\Service\AbstractService $apiService';
114 print $e->getMessage();
124if (empty($apiService)) {
125 print
'Error, failed to create serviceFactory';
132$langs->load(
"oauth");
135 accessforbidden(
'Setup of service is not complete. Customer ID is missing ('.$keyforparamid.
')');
138 accessforbidden(
'Setup of service is not complete. Secret key is missing ('.$keyforparamsecret.
')');
141 accessforbidden(
'Setup of service is not complete. Tenant/Annuary ID key is missing ('.$keyforparamtenant.
')');
149if ($action ==
'delete' && (!empty($user->admin) || $user->id ==
GETPOSTINT(
'userid'))) {
151 $storage->clearToken($genericstring);
155 if (empty($backtourl)) {
156 $backtourl = DOL_URL_ROOT.
'/';
159 header(
'Location: '.$backtourl);
183 $token = $apiService->requestAccessToken(
GETPOST(
'code'));
193 $backtourl = $_SESSION[
"backtourlsavedbeforeoauthjump"];
194 unset($_SESSION[
"backtourlsavedbeforeoauthjump"]);
196 header(
'Location: '.$backtourl);
200 dol_syslog(basename(__FILE__).
' requestAccessToken failed: '.$e->getMessage().
' - '.$diag, LOG_ERR);
201 print $langs->trans(
"OAuthErrorTokenRequestFailed",
dol_escape_htmltag($e->getMessage()));
202 print
'<br>'.dol_escape_htmltag($diag);
207 $_SESSION[
"backtourlsavedbeforeoauthjump"] = $backtourl;
208 $_SESSION[
"oauthkeyforproviderbeforeoauthjump"] = $keyforprovider;
209 $_SESSION[
'oauthstateanticsrf'] = $state;
219 $params[
'state'] = $state;
221 if (!empty($params)) {
222 $url = $apiService->getAuthorizationUri($params);
224 $url = $apiService->getAuthorizationUri();
232 header(
'Location: '.$url);
global $dolibarr_main_url_root
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
$uriFactory
Create a new instance of the URI class with the current URI, stripping the query string.
getOauthSetupDiagnostic($genericstring, $keyforprovider='')
Return a human readable diagnostic of the setup of an OAuth2 provider entry, to append to an error me...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.