diff --git a/packages/nodes-base/credentials/MicrosoftCosmosDbSharedKeyApi.credentials.ts b/packages/nodes-base/credentials/MicrosoftCosmosDbSharedKeyApi.credentials.ts index 66c4248df7..581dc24449 100644 --- a/packages/nodes-base/credentials/MicrosoftCosmosDbSharedKeyApi.credentials.ts +++ b/packages/nodes-base/credentials/MicrosoftCosmosDbSharedKeyApi.credentials.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-unsafe-argument */ import { ApplicationError, type ICredentialDataDecryptedObject, @@ -64,11 +65,13 @@ export class MicrosoftCosmosDbSharedKeyApi implements ICredentialType { } requestOptions.headers ??= {}; + const date = new Date().toUTCString().toLowerCase(); requestOptions.headers = { ...requestOptions.headers, 'x-ms-date': date, 'x-ms-version': '2018-12-31', + 'Cache-Control': 'no-cache', }; if (credentials.sessionToken) { @@ -76,7 +79,6 @@ export class MicrosoftCosmosDbSharedKeyApi implements ICredentialType { } let url; - if (requestOptions.url) { url = new URL(requestOptions.baseURL + requestOptions.url); //@ts-ignore @@ -86,7 +88,6 @@ export class MicrosoftCosmosDbSharedKeyApi implements ICredentialType { } const pathSegments = url?.pathname.split('/').filter((segment) => segment); - let resourceType = ''; let resourceId = ''; @@ -116,23 +117,22 @@ export class MicrosoftCosmosDbSharedKeyApi implements ICredentialType { throw new ApplicationError('Unable to determine resourceType and resourceId from the URL.'); } - console.log('resourceId', resourceId); - console.log('resourceType', resourceType); - if (requestOptions.method) { - const authToken = getAuthorizationTokenUsingMasterKey( - requestOptions.method, - resourceType, - resourceId, - date, - credentials.key as string, - ); + let authToken = ''; - requestOptions.headers[HeaderConstants.AUTHORIZATION] = authToken; + if (credentials.key) { + authToken = getAuthorizationTokenUsingMasterKey( + requestOptions.method, + resourceType, + resourceId, + credentials.key as string, + ); + } + + requestOptions.headers[HeaderConstants.AUTHORIZATION] = encodeURIComponent(authToken); + await new Promise((resolve) => setTimeout(resolve, 500)); } - console.log('Final requestOptions:', requestOptions); - return requestOptions; } } diff --git a/packages/nodes-base/nodes/Microsoft/CosmosDB/GenericFunctions.ts b/packages/nodes-base/nodes/Microsoft/CosmosDB/GenericFunctions.ts index ae66d76aca..c92e5c9489 100644 --- a/packages/nodes-base/nodes/Microsoft/CosmosDB/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Microsoft/CosmosDB/GenericFunctions.ts @@ -45,17 +45,12 @@ export function getAuthorizationTokenUsingMasterKey( verb: string, resourceType: string, resourceId: string, - date: string, masterKey: string, ): string { - const key = Buffer.from(masterKey, 'base64'); - const payload = - `${verb.toLowerCase()}\n` + - `${resourceType.toLowerCase()}\n` + - `${resourceId}\n` + - `${date.toLowerCase()}\n` + - '\n'; + const date = new Date().toUTCString().toLowerCase(); + const key = Buffer.from(masterKey, 'base64'); + const payload = `${verb.toLowerCase()}\n${resourceType.toLowerCase()}\n${resourceId}\n${date.toLowerCase()}\n\n`; const hmacSha256 = crypto.createHmac('sha256', key); const signature = hmacSha256.update(payload, 'utf8').digest('base64');