29require
'../../../main.inc.php';
30require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
40use OAuth\Common\Storage\DoliStorage;
41use OAuth\Common\Consumer\Credentials;
45$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
48$action =
GETPOST(
'action',
'aZ09');
49$backtourl =
GETPOST(
'backtourl',
'alpha');
50$keyforprovider =
GETPOST(
'keyforprovider',
'aZ09');
51$genericstring =
'MICROSOFTAPP';
60$currentUri =
$uriFactory->createFromAbsolute($urlwithroot.
'/core/modules/oauth/microsoftapp_oauthcallback.php');
68$serviceFactory = new \OAuth\ServiceFactory();
69$httpClient = new \OAuth\Common\Http\Client\CurlClient();
71$serviceFactory->setHttpClient($httpClient);
74$keyforparamid =
'OAUTH_'.$genericstring.($keyforprovider ?
'-'.$keyforprovider :
'').
'_ID';
75$keyforparamsecret =
'OAUTH_'.$genericstring.($keyforprovider ?
'-'.$keyforprovider :
'').
'_SECRET';
76$keyforparamtenant =
'OAUTH_'.$genericstring.($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
77$keyforparamscope =
'OAUTH_'.$genericstring.($keyforprovider ?
'-'.$keyforprovider :
'').
'_SCOPE';
82$credentials =
new Credentials(
85 $currentUri->getAbsoluteUri()
91 accessforbidden(
'Setup of service is not complete. Customer ID is missing');
94 accessforbidden(
'Setup of service is not complete. Secret key is missing');
97 accessforbidden(
'Setup of service is not complete. Tenant/Annuary ID key is missing');
104if ($action ==
'delete' && (!empty($user->admin) || $user->id ==
GETPOSTINT(
'userid'))) {
106 $storage->clearToken(
'Microsoftapp');
108 setEventMessages($langs->trans(
'TokenDeleted'),
null,
'mesgs');
110 if (empty($backtourl)) {
111 $backtourl = DOL_URL_ROOT.
'/';
114 header(
'Location: '.$backtourl);
121if (empty($user->admin)) {
122 accessforbidden(
'Only an admin user can request an application token');
125if (empty($backtourl)) {
126 $backtourl = DOL_URL_ROOT.
'/admin/oauthlogintokens.php';
131$requestedpermissionsarray = array();
137 $nameofservice = ucfirst(strtolower($genericstring));
138 $apiService = $serviceFactory->createService($nameofservice, $credentials, $storage, $requestedpermissionsarray);
140 if (!($apiService instanceof OAuth\OAuth2\Service\Microsoftapp)) {
141 print
'Error, failed to create serviceFactory';
145 dol_syslog(basename(__FILE__).
" Request app-only access token (client credentials grant flow) for service ".$nameofservice.($keyforprovider ?
'-'.$keyforprovider :
''));
147 $apiService->requestNewAccessToken();
149 setEventMessages($langs->trans(
'NewTokenStored'),
null,
'mesgs');
151 setEventMessages($e->getMessage(),
null,
'errors');
152 dol_syslog(basename(__FILE__).
" Failed to get app-only access token: ".$e->getMessage(), LOG_ERR);
155header(
'Location: '.$backtourl);
global $dolibarr_main_url_root
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.
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.
$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.