Plugin: Azure: Request a new access token when it expires - refs BT#21930

pull/5763/head
Angel Fernando Quiroz Campos 1 year ago
parent 228c3dc8fd
commit c9d99a60db
No known key found for this signature in database
GPG Key ID: B284841AE3E562CD
  1. 6
      plugin/azure_active_directory/src/AzureActiveDirectory.php
  2. 17
      plugin/azure_active_directory/src/AzureCommand.php
  3. 9
      plugin/azure_active_directory/src/AzureSyncUsergroupsCommand.php
  4. 15
      plugin/azure_active_directory/src/AzureSyncUsersCommand.php

@ -213,7 +213,7 @@ class AzureActiveDirectory extends Plugin
* @throws Exception * @throws Exception
*/ */
public function registerUser( public function registerUser(
AccessTokenInterface $token, AccessTokenInterface &$token,
Azure $provider, Azure $provider,
array $azureUserInfo, array $azureUserInfo,
string $apiGroupsRef = 'me/memberOf', string $apiGroupsRef = 'me/memberOf',
@ -319,7 +319,7 @@ class AzureActiveDirectory extends Plugin
* @throws Exception * @throws Exception
*/ */
private function formatUserData( private function formatUserData(
AccessTokenInterface $token, AccessTokenInterface &$token,
Azure $provider, Azure $provider,
array $azureUserInfo, array $azureUserInfo,
string $apiGroupsRef, string $apiGroupsRef,
@ -378,7 +378,7 @@ class AzureActiveDirectory extends Plugin
* @throws Exception * @throws Exception
*/ */
private function getUserRoleAndCheckIsAdmin( private function getUserRoleAndCheckIsAdmin(
AccessTokenInterface $token, AccessTokenInterface &$token,
Azure $provider, Azure $provider,
string $apiRef = 'me/memberOf', string $apiRef = 'me/memberOf',
string $groupObjectIdKey = 'objectId' string $groupObjectIdKey = 'objectId'

@ -2,6 +2,8 @@
/* For license terms, see /license.txt */ /* For license terms, see /license.txt */
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Token\AccessTokenInterface;
use TheNetworg\OAuth2\Client\Provider\Azure; use TheNetworg\OAuth2\Client\Provider\Azure;
class AzureCommand class AzureCommand
@ -21,4 +23,19 @@ class AzureCommand
$this->plugin->get_settings(true); $this->plugin->get_settings(true);
$this->provider = $this->plugin->getProviderForApiGraph(); $this->provider = $this->plugin->getProviderForApiGraph();
} }
/**
* @throws IdentityProviderException
*/
protected function getToken(?AccessTokenInterface $currentToken = null): AccessTokenInterface
{
if (!$currentToken || ($currentToken->getExpires() && !$currentToken->getRefreshToken())) {
return $this->provider->getAccessToken(
'client_credentials',
['resource' => $this->provider->resource]
);
}
return $currentToken;
}
} }

@ -15,10 +15,7 @@ class AzureSyncUsergroupsCommand extends AzureCommand
{ {
yield 'Synchronizing groups from Azure.'; yield 'Synchronizing groups from Azure.';
$token = $this->provider->getAccessToken( $token = $this->getToken();
'client_credentials',
['resource' => $this->provider->resource]
);
foreach ($this->getAzureGroups($token) as $azureGroupInfo) { foreach ($this->getAzureGroups($token) as $azureGroupInfo) {
$usergroup = new UserGroup(); $usergroup = new UserGroup();
@ -80,6 +77,8 @@ class AzureSyncUsergroupsCommand extends AzureCommand
); );
do { do {
$token = $this->getToken($token);
try { try {
$azureGroupsRequest = $this->provider->request('get', "groups?$query", $token); $azureGroupsRequest = $this->provider->request('get', "groups?$query", $token);
} catch (Exception $e) { } catch (Exception $e) {
@ -121,6 +120,8 @@ class AzureSyncUsergroupsCommand extends AzureCommand
$hasNextLink = false; $hasNextLink = false;
do { do {
$token = $this->getToken($token);
try { try {
$azureGroupMembersRequest = $this->provider->request( $azureGroupMembersRequest = $this->provider->request(
'get', 'get',

@ -15,15 +15,14 @@ class AzureSyncUsersCommand extends AzureCommand
{ {
yield 'Synchronizing users from Azure.'; yield 'Synchronizing users from Azure.';
$token = $this->provider->getAccessToken( $token = $this->getToken();
'client_credentials',
['resource' => $this->provider->resource]
);
$existingUsers = []; $existingUsers = [];
foreach ($this->getAzureUsers($token) as $azureUserInfo) { foreach ($this->getAzureUsers($token) as $azureUserInfo) {
try { try {
$token = $this->getToken($token);
$userId = $this->plugin->registerUser( $userId = $this->plugin->registerUser(
$token, $token,
$this->provider, $this->provider,
@ -95,8 +94,14 @@ class AzureSyncUsersCommand extends AzureCommand
); );
do { do {
$token = $this->getToken($token);
try { try {
$azureUsersRequest = $this->provider->request('get', "users?$query", $token); $azureUsersRequest = $this->provider->request(
'get',
"users?$query",
$token
);
} catch (Exception $e) { } catch (Exception $e) {
throw new Exception('Exception when requesting users from Azure: '.$e->getMessage()); throw new Exception('Exception when requesting users from Azure: '.$e->getMessage());
} }

Loading…
Cancel
Save