33require
'../../../main.inc.php';
34require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
35require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
45use OAuth\Common\Storage\DoliStorage;
46use OAuth\Common\Consumer\Credentials;
50$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
53$action =
GETPOST(
'action',
'aZ09');
54$backtourl =
GETPOST(
'backtourl',
'alpha');
55$keyforprovider =
GETPOST(
'keyforprovider',
'aZ09');
56if (empty($keyforprovider) && !empty($_SESSION[
"oauthkeyforproviderbeforeoauthjump"]) && (
GETPOST(
'code') || $action ==
'delete')) {
57 $keyforprovider = $_SESSION[
"oauthkeyforproviderbeforeoauthjump"];
59$genericstring =
'MICROSOFT3';
68$currentUri =
$uriFactory->createFromAbsolute($urlwithroot.
'/core/modules/oauth/microsoft3_oauthcallback.php');
76$serviceFactory = new \OAuth\ServiceFactory();
77$httpClient = new \OAuth\Common\Http\Client\CurlClient();
79$serviceFactory->setHttpClient($httpClient);
82$keyforparamid =
'OAUTH_'.$genericstring.($keyforprovider ?
'-'.$keyforprovider :
'').
'_ID';
83$keyforparamsecret =
'OAUTH_'.$genericstring.($keyforprovider ?
'-'.$keyforprovider :
'').
'_SECRET';
84$keyforparamtenant =
'OAUTH_'.$genericstring.($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
89$credentials =
new Credentials(
92 $currentUri->getAbsoluteUri()
97$requestedpermissionsarray = array();
99 $requestedpermissionsarray = explode(
',', $state);
101if ($action !=
'delete' && empty($requestedpermissionsarray)) {
102 $langs->load(
"oauth");
103 print $langs->trans(
"OAuthErrorNoScopeInState",
'OAUTH_'.$genericstring.($keyforprovider ?
'-'.$keyforprovider :
'').
'_SCOPE');
109 $nameofservice = ucfirst(strtolower($genericstring));
110 $apiService = $serviceFactory->createService($nameofservice, $credentials, $storage, $requestedpermissionsarray);
111 '@phan-var-force OAuth\OAuth2\Service\AbstractService|OAuth\OAuth1\Service\AbstractService $apiService';
113 print $e->getMessage();
117if (empty($apiService)) {
118 print
'Error, failed to create serviceFactory';
122$langs->load(
"oauth");
125 accessforbidden(
'Setup of service is not complete. Customer ID is missing ('.$keyforparamid.
')');
128 accessforbidden(
'Setup of service is not complete. Secret key is missing ('.$keyforparamsecret.
')');
131 accessforbidden(
'Setup of service is not complete. Tenant/Annuary ID key is missing ('.$keyforparamtenant.
')');
138if ($action ==
'delete' && (!empty($user->admin) || $user->id ==
GETPOSTINT(
'userid'))) {
140 $storage->clearToken($genericstring);
144 if (empty($backtourl)) {
145 $backtourl = DOL_URL_ROOT.
'/';
148 header(
'Location: '.$backtourl);
159 if (isset($_SESSION[
'oauthstateanticsrf']) && $state != $_SESSION[
'oauthstateanticsrf']) {
161 unset($_SESSION[
'oauthstateanticsrf']);
170 $token = $apiService->requestAccessToken(
GETPOST(
'code'));
174 $backtourl = $_SESSION[
"backtourlsavedbeforeoauthjump"];
175 unset($_SESSION[
"backtourlsavedbeforeoauthjump"]);
177 header(
'Location: '.$backtourl);
181 dol_syslog(basename(__FILE__).
' requestAccessToken failed: '.$e->getMessage().
' - '.$diag, LOG_ERR);
182 print $langs->trans(
"OAuthErrorTokenRequestFailed",
dol_escape_htmltag($e->getMessage()));
183 print
'<br>'.dol_escape_htmltag($diag);
188 $_SESSION[
"backtourlsavedbeforeoauthjump"] = $backtourl;
189 $_SESSION[
"oauthkeyforproviderbeforeoauthjump"] = $keyforprovider;
190 $_SESSION[
'oauthstateanticsrf'] = $state;
194 $params = array(
'prompt' =>
'consent');
196 $params[
'state'] = $state;
198 $url = $apiService->getAuthorizationUri($params);
204 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.