Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Define connection string and related Event Hubs entity name here
const connectionString = "";
const eventHubName = "";
// Define AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET of your AAD application in your environment
const ehConnectionConfig = EventHubConnectionConfig.create(connectionString, eventHubName);
const parameters = {
config: ehConnectionConfig,
connectionProperties: {
product: "MSJSClient",
userAgent: "/js-core-amqp",
version: "0.1.0"
}
};
const connectionContext = ConnectionContextBase.create(parameters);
async function authenticate(
audience: string,
closeConnection: boolean = false
): Promise {
await connectionContext.cbsSession.init();
const credential = new DefaultAzureCredential();
const tokenObject = await credential.getToken(Constants.aadEventHubsScope);
if (!tokenObject) {
throw new Error("Aad token cannot be null");
}
const result = await connectionContext.cbsSession.negotiateClaim(
audience,
tokenObject,
TokenType.CbsTokenTypeJwt
);
options.webSocketOptions && options.webSocketOptions.webSocketConstructorOptions;
const parameters: CreateConnectionContextBaseParameters = {
config: config,
tokenCredential: tokenCredential,
// re-enabling this will be a post-GA discussion.
// dataTransformer: options.dataTransformer,
isEntityPathRequired: true,
connectionProperties: {
product: "MSJSClient",
userAgent: getUserAgent(options),
version: packageJsonInfo.version
}
};
// Let us create the base context and then add EventHub specific ConnectionContext properties.
const connectionContext = ConnectionContextBase.create(parameters) as ConnectionContext;
connectionContext.wasConnectionCloseCalled = false;
connectionContext.senders = {};
connectionContext.receivers = {};
const mOptions: ManagementClientOptions = {
address: options.managementSessionAddress,
audience: options.managementSessionAudience
};
connectionContext.managementSession = new ManagementClient(connectionContext, mOptions);
// Define listeners to be added to the connection object for
// "connection_open" and "connection_error" events.
const onConnectionOpen: OnAmqpEvent = (context: EventContext) => {
connectionContext.wasConnectionCloseCalled = false;
logger.verbose(
"[%s] setting 'wasConnectionCloseCalled' property of connection context to %s.",
connectionContext.connection.id,