UNPKG

azure-kusto-data

Version:
54 lines 3.04 kB
import * as TokenProvider from "./tokenProvider.js"; import { KustoAuthenticationError } from "./errors.js"; import { BasicTokenProvider, CallbackTokenProvider, UserPromptProvider, TokenCredentialProvider } from "./tokenProvider.js"; export class AadHelper { constructor(kcsb) { if (!kcsb.dataSource) { throw new Error("Invalid string builder - missing dataSource"); } if (!!kcsb.aadUserId && !!kcsb.password) { this.tokenProvider = new TokenProvider.UserPassTokenProvider(kcsb.dataSource, kcsb.aadUserId, kcsb.password, kcsb.authorityId, kcsb.timeoutMs); } else if (!!kcsb.applicationClientId && !!kcsb.applicationKey) { this.tokenProvider = new TokenProvider.ApplicationKeyTokenProvider(kcsb.dataSource, kcsb.applicationClientId, kcsb.applicationKey, kcsb.authorityId, kcsb.timeoutMs); } else if (!!kcsb.applicationClientId && (!!kcsb.applicationCertificatePrivateKey || !!kcsb.applicationCertificatePath)) { this.tokenProvider = new TokenProvider.ApplicationCertificateTokenProvider(kcsb.dataSource, kcsb.applicationClientId, kcsb.applicationCertificatePrivateKey, kcsb.applicationCertificatePath, kcsb.applicationCertificateSendX5c, kcsb.authorityId, kcsb.timeoutMs); } else if (kcsb.useManagedIdentityAuth) { this.tokenProvider = new TokenProvider.MsiTokenProvider(kcsb.dataSource, kcsb.msiClientId, kcsb.authorityId, kcsb.timeoutMs); } else if (kcsb.useAzLoginAuth) { this.tokenProvider = new TokenProvider.AzCliTokenProvider(kcsb.dataSource, kcsb.authorityId, kcsb.timeoutMs); } else if (kcsb.accessToken) { this.tokenProvider = new BasicTokenProvider(kcsb.dataSource, kcsb.accessToken); } else if (kcsb.useUserPromptAuth) { this.tokenProvider = new UserPromptProvider(kcsb.dataSource, kcsb.interactiveCredentialOptions, kcsb.timeoutMs); } else if (kcsb.tokenProvider) { this.tokenProvider = new CallbackTokenProvider(kcsb.dataSource, kcsb.tokenProvider); } else if (kcsb.useDeviceCodeAuth) { this.tokenProvider = new TokenProvider.DeviceLoginTokenProvider(kcsb.dataSource, kcsb.deviceCodeCallback, kcsb.authorityId, kcsb.timeoutMs); } else if (kcsb.tokenCredential) { this.tokenProvider = new TokenCredentialProvider(kcsb.dataSource, kcsb.tokenCredential, kcsb.timeoutMs); } } async getAuthHeader() { if (!this.tokenProvider) { return null; } try { const token = await this.tokenProvider.acquireToken(); return `${token.tokenType} ${token.accessToken}`; } catch (e) { throw new KustoAuthenticationError(e instanceof Error ? e.message : `${e}`, e instanceof Error ? e : undefined, this.tokenProvider.constructor.name, this.tokenProvider.context()); } } } export default AadHelper; //# sourceMappingURL=security.js.map