fix(Azure OpenAI Chat Model Node): Simplify Azure Entra ID Authentication and Auto-Refresh token (#15335)

This commit is contained in:
oleg
2025-05-13 10:48:51 +02:00
committed by GitHub
parent 4302c5f474
commit e750d5366e
4 changed files with 54 additions and 27 deletions

View File

@@ -1,4 +1,6 @@
import type { TokenCredential, AccessToken } from '@azure/identity';
import type { ClientOAuth2TokenData } from '@n8n/client-oauth2';
import { ClientOAuth2 } from '@n8n/client-oauth2';
import type { INode } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
@@ -20,10 +22,25 @@ export class N8nOAuth2TokenCredential implements TokenCredential {
if (!this.credential?.oauthTokenData?.access_token) {
throw new NodeOperationError(this.node, 'Failed to retrieve access token');
}
const oAuthClient = new ClientOAuth2({
clientId: this.credential.clientId,
clientSecret: this.credential.clientSecret,
accessTokenUri: this.credential.accessTokenUrl,
scopes: this.credential.scope?.split(' '),
authentication: this.credential.authentication,
authorizationUri: this.credential.authUrl,
additionalBodyProperties: {
resource: 'https://cognitiveservices.azure.com/',
},
});
const token = await oAuthClient.credentials.getToken();
const data = token.data as ClientOAuth2TokenData & {
expires_on: number;
};
return {
token: this.credential.oauthTokenData.access_token,
expiresOnTimestamp: this.credential.oauthTokenData.expires_on,
token: data.access_token,
expiresOnTimestamp: data.expires_on,
};
} catch (error) {
// Re-throw with better error message