diff --git a/plugins/catalog-backend-module-msgraph/src/microsoftGraph/client.ts b/plugins/catalog-backend-module-msgraph/src/microsoftGraph/client.ts index 8a9b2fd161..457a83c703 100644 --- a/plugins/catalog-backend-module-msgraph/src/microsoftGraph/client.ts +++ b/plugins/catalog-backend-module-msgraph/src/microsoftGraph/client.ts @@ -106,7 +106,14 @@ export class MicrosoftGraphClient { path: string, query?: ODataQuery, ): AsyncIterable { - let response = await this.requestApi(path, query); + + const headers: Record = query?.search ? { + // Eventual consistency is required to use $search. + // If a new user/group is not found, it'll eventually be imported on a subsequent read + ConsistencyLevel: 'eventual', + } : {} + + let response = await this.requestApi(path, query, headers); for (;;) { if (response.status !== 200) { @@ -125,7 +132,7 @@ export class MicrosoftGraphClient { return; } - response = await this.requestRaw(result['@odata.nextLink']); + response = await this.requestRaw(result['@odata.nextLink'], headers); } } @@ -135,8 +142,9 @@ export class MicrosoftGraphClient { * @public * @param path - Resource in Microsoft Graph * @param query - OData Query {@link ODataQuery} + * @param headers - optional HTTP headers */ - async requestApi(path: string, query?: ODataQuery): Promise { + async requestApi(path: string, query?: ODataQuery, headers?: Record): Promise { const queryString = qs.stringify( { $search: query?.search, @@ -151,15 +159,16 @@ export class MicrosoftGraphClient { }, ); - return await this.requestRaw(`${this.baseUrl}/${path}${queryString}`); + return await this.requestRaw(`${this.baseUrl}/${path}${queryString}`, headers); } /** * Makes a HTTP call to Graph API with token * * @param url - HTTP Endpoint of Graph API + * @param headers - optional HTTP headers */ - async requestRaw(url: string): Promise { + async requestRaw(url: string, headers?: Record): Promise { // Make sure that we always have a valid access token (might be cached) const token = await this.pca.acquireTokenByClientCredential({ scopes: ['https://graph.microsoft.com/.default'], @@ -171,12 +180,8 @@ export class MicrosoftGraphClient { return await fetch(url, { headers: { + ...headers, Authorization: `Bearer ${token.accessToken}`, - - // Eventual consistency is required to use $search. - // Groups/Users are not changed that frequently to require strong consistency - // If a new user/group is not found, it'll eventually be imported on a subsequent call - ConsistencyLevel: 'eventual', }, }); }