Fix mutitenant MS oauth

This commit is contained in:
Laurent Destailleur 2024-10-03 00:05:33 +02:00
parent 50d2613124
commit b37773cf94
5 changed files with 16 additions and 10 deletions

View File

@ -338,7 +338,7 @@ function getSupportedOauth2Array()
'urlforcredentials' => 'https://portal.azure.com/',
// User.Read is a microsoftgraph scope, if it's not working, do not select it
'availablescopes' => 'openid,offline_access,profile,email,User.Read,https://outlook.office.com/.default',
'returnurl' => '/core/modules/oauth/microsoft_oauthcallback.php'
'returnurl' => '/core/modules/oauth/microsoft2_oauthcallback.php'
);
if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 2) {
$supportedoauth2array['OAUTH_GENERIC_NAME'] = array(

View File

@ -49,6 +49,7 @@ $genericstring = 'MICROSOFT2';
$uriFactory = new \OAuth\Common\Http\Uri\UriFactory();
//$currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER);
//$currentUri->setQuery('');
$currentUri = $uriFactory->createFromAbsolute($urlwithroot.'/core/modules/oauth/microsoft2_oauthcallback.php');
@ -64,19 +65,21 @@ $httpClient = new \OAuth\Common\Http\Client\CurlClient();
//$httpClient->setCurlParameters($params);
$serviceFactory->setHttpClient($httpClient);
// Dolibarr storage
$storage = new DoliStorage($db, $conf, $keyforprovider);
// Setup the credentials for the requests
$keyforparamid = 'OAUTH_'.$genericstring.($keyforprovider ? '-'.$keyforprovider : '').'_ID';
$keyforparamsecret = 'OAUTH_'.$genericstring.($keyforprovider ? '-'.$keyforprovider : '').'_SECRET';
$keyforparamtenant = 'OAUTH_'.$genericstring.($keyforprovider ? '-'.$keyforprovider : '').'_TENANT';
// Dolibarr storage
$storage = new DoliStorage($db, $conf, $keyforprovider, getDolGlobalString($keyforparamtenant));
$credentials = new Credentials(
getDolGlobalString($keyforparamid),
getDolGlobalString($keyforparamsecret),
$currentUri->getAbsoluteUri()
);
$state = GETPOST('state');
$requestedpermissionsarray = array();

View File

@ -63,13 +63,14 @@ $httpClient = new \OAuth\Common\Http\Client\CurlClient();
//$httpClient->setCurlParameters($params);
$serviceFactory->setHttpClient($httpClient);
// Dolibarr storage
$storage = new DoliStorage($db, $conf, $keyforprovider);
// Setup the credentials for the requests
$keyforparamid = 'OAUTH_'.$genericstring.($keyforprovider ? '-'.$keyforprovider : '').'_ID';
$keyforparamsecret = 'OAUTH_'.$genericstring.($keyforprovider ? '-'.$keyforprovider : '').'_SECRET';
$keyforparamtenant = 'OAUTH_'.$genericstring.($keyforprovider ? '-'.$keyforprovider : '').'_TENANT';
// Dolibarr storage
$storage = new DoliStorage($db, $conf, $keyforprovider, getDolGlobalString($keyforparamtenant));
$credentials = new Credentials(
getDolGlobalString($keyforparamid),
getDolGlobalString($keyforparamsecret),

View File

@ -71,8 +71,9 @@ class DoliStorage implements TokenStorageInterface
* @param DoliDB $db Database handler
* @param \Conf $conf Conf object
* @param string $keyforprovider Key to manage several providers of the same type. For example 'abc' will be added to 'Google' to defined storage key.
* @param string $tenant Value of tenant if used
*/
public function __construct(DoliDB $db, \Conf $conf, $keyforprovider = '')
public function __construct(DoliDB $db, \Conf $conf, $keyforprovider = '', $tenant = '')
{
$this->db = $db;
$this->conf = $conf;
@ -80,6 +81,7 @@ class DoliStorage implements TokenStorageInterface
$this->token = '';
$this->tokens = array();
$this->states = array();
$this->tenant = $tenant;
//$this->key = $key;
//$this->stateKey = $stateKey;
}
@ -381,7 +383,7 @@ class DoliStorage implements TokenStorageInterface
{
// Set/Reset tenant now so it will be defined for.
// TODO We must store it into the table llx_oauth_token
$this->tenant = getDolGlobalString('OAUTH_MICROSOFT'.($this->keyforprovider ? '-'.$this->keyforprovider : '').'_TENANT');
//$this->tenant = getDolGlobalString('OAUTH_MICROSOFT'.($this->keyforprovider ? '-'.$this->keyforprovider : '').'_TENANT');
return $this->tenant;
}

View File

@ -10,7 +10,7 @@ use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Http\Uri\UriInterface;
class Microsoft extends AbstractService
class Microsoft2 extends AbstractService
{
const SCOPE_BASIC = 'basic';
const SCOPE_OFFLINE_ACCESS = 'offline_access';