UNPKG

@n8n/n8n-nodes-langchain

Version:
67 lines • 2.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.N8nOAuth2TokenCredential = void 0; const client_oauth2_1 = require("@n8n/client-oauth2"); const n8n_workflow_1 = require("n8n-workflow"); const requireTenantId_1 = require("./requireTenantId"); const types_1 = require("../types"); function expiresOnTimestamp(data) { const expiresIn = Number(data.expires_in); if (Number.isFinite(expiresIn) && expiresIn > 0) return Date.now() + expiresIn * 1000; const expiresOn = Number(data.expires_on); if (Number.isFinite(expiresOn) && expiresOn > 0) return expiresOn * 1000; return Date.now(); } class N8nOAuth2TokenCredential { constructor(node, credential, audience = types_1.AZURE_OPENAI_INFERENCE_AUDIENCE, egressFilter) { this.node = node; this.credential = credential; this.audience = audience; this.egressFilter = egressFilter; } async getToken() { (0, requireTenantId_1.requireTenantId)(this.node, this.credential.tenantId); try { const oAuthClient = new client_oauth2_1.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: `${this.audience}/`, }, ssrfBridge: this.egressFilter, }); const token = await oAuthClient.credentials.getToken(); return { token: token.data.access_token, expiresOnTimestamp: expiresOnTimestamp(token.data), }; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.node, 'Failed to retrieve OAuth2 access token', error); } } async getDeploymentDetails() { if (this.credential.endpointType === 'foundry') { return { apiVersion: this.credential.apiVersion ?? '', endpoint: this.credential.foundryEndpoint, resourceName: this.credential.resourceName ?? '', endpointType: 'foundry', foundryEndpoint: this.credential.foundryEndpoint, }; } return { apiVersion: this.credential.apiVersion ?? '', endpoint: this.credential.endpoint, resourceName: this.credential.resourceName ?? '', }; } } exports.N8nOAuth2TokenCredential = N8nOAuth2TokenCredential; //# sourceMappingURL=N8nOAuth2TokenCredential.js.map