UNPKG

@azure/event-hubs

Version:
120 lines (119 loc) 4.38 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var withAuth_exports = {}; __export(withAuth_exports, { openCbsSession: () => openCbsSession, withAuth: () => withAuth }); module.exports = __toCommonJS(withAuth_exports); var import_core_amqp = require("@azure/core-amqp"); var import_timerLoop = require("./util/timerLoop.js"); var import_logger = require("./logger.js"); async function withAuth(callback, context, audience, timeoutInMs, logger, options) { const info = await getTokenInfo(context.tokenCredential, audience); await setupClaimNegotiation(context, audience, info, timeoutInMs, logger, options); await callback(); async function createTask() { try { const info2 = await getTokenInfo(context.tokenCredential, audience); await setupClaimNegotiation(context, audience, info2, timeoutInMs, logger, options); logger.verbose( `next token renewal is in ${info2.timeToLiveInMs} milliseconds @(${new Date( Date.now() + info2.timeToLiveInMs ).toString()}).` ); } catch (err) { logger.verbose(`an error occurred while renewing the token: ${(0, import_logger.logObj)(err)}`); } } const loop = (0, import_timerLoop.createTimerLoop)(info.timeToLiveInMs, createTask); loop.start(); return loop; } async function openCbsSession(client, timeoutAfterStartTime, { abortSignal } = {}) { return import_core_amqp.defaultCancellableLock.acquire( client.cbsLock, () => client.init({ abortSignal, timeoutInMs: Math.max(timeoutAfterStartTime - Date.now(), 0) }), { abortSignal, timeoutInMs: Math.max(timeoutAfterStartTime - Date.now(), 0) } ); } async function getAadToken(cred) { const token = await cred.getToken(import_core_amqp.Constants.aadEventHubsScope); if (!token) { throw new Error(`Failed to get token from the provided "TokenCredential" object`); } return { token, type: import_core_amqp.TokenType.CbsTokenTypeJwt, timeToLiveInMs: token.expiresOnTimestamp - Date.now() - 2 * 60 * 1e3 }; } async function getSharedKeyBasedToken(cred, audience) { return { token: await cred.getToken(audience), type: import_core_amqp.TokenType.CbsTokenTypeSas, timeToLiveInMs: 45 * 60 * 1e3 }; } async function getTokenInfo(cred, audience) { return (0, import_core_amqp.isSasTokenProvider)(cred) ? getSharedKeyBasedToken(cred, audience) : getAadToken(cred); } function negotiateClaim({ token, type }, audience, cbsSession, timeoutAfterStartTime, lock, abortSignal) { return import_core_amqp.defaultCancellableLock.acquire( lock, () => cbsSession.negotiateClaim(audience, token.token, type, { abortSignal, timeoutInMs: Math.max(timeoutAfterStartTime - Date.now(), 0) }), { abortSignal, timeoutInMs: Math.max(timeoutAfterStartTime - Date.now(), 0) } ); } async function setupClaimNegotiation(context, audience, info, timeoutInMs, logger, { abortSignal }) { const startTime = Date.now(); logger.verbose( `acquiring cbs lock: '${context.cbsSession.cbsLock}' for creating the cbs session` ); await openCbsSession(context.cbsSession, timeoutInMs + startTime, { abortSignal }); logger.verbose(`acquiring cbs lock: '${context.negotiateClaimLock}' for cbs auth`); await negotiateClaim( info, audience, context.cbsSession, timeoutInMs + startTime, context.negotiateClaimLock, abortSignal ); logger.verbose("claim negotiation succeeded"); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { openCbsSession, withAuth }); //# sourceMappingURL=withAuth.js.map