Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 41 additions & 36 deletions auth/arm-auth.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
const { InteractiveBrowserCredential } = require('@azure/identity');

/**
* Gets authentication ARM token using interactive browser authentication
* @param {Object} options - Configuration options
* @param {string} [options.tenantId] - Optional tenant ID to override default tenant
* @param {string} [options.clientId] - Optional client ID for the application
* @returns {Promise<string>} - Bearer token for portal.azure.com
*/
async function getArmToken(options = {}) {
try {
const credential = new InteractiveBrowserCredential({
tenantId: options.tenantId,
clientId: options.clientId
});
console.log("Please sign in via the browser window that will open...");

// Get the token - this will open a browser window for authentication
const scope = "https://management.azure.com/user_impersonation";
const response = await credential.getToken(scope);

if (response && response.token) {
console.log("Successfully acquired token with expiration at:", (new Date(response.expiresOnTimestamp)).toLocaleString());
return `${response.tokenType} ${response.token}`;
} else {
throw new Error("Failed to acquire token: Empty response");
}
} catch (error) {
console.error("Error acquiring portal token:", error.message);
throw error;
}
}

module.exports = {
getArmToken
};
const { InteractiveBrowserCredential } = require('@azure/identity');

/**
* Gets authentication ARM token using interactive browser authentication
* @param {Object} options - Configuration options
* @param {string} [options.tenantId] - Optional tenant ID to override default tenant
* @param {string} [options.clientId] - Optional client ID for the application
* @returns {Promise<string>} - Bearer token for portal.azure.com
*/
async function getArmToken(options = {}) {
try {
const credential = new InteractiveBrowserCredential({
tenantId: options.tenantId,
clientId: options.clientId,
loginStyle: "popup"
});

console.log("Please sign in via the browser window that will open...");

// Get the token - this will open a browser window for authentication
const scope = "https://management.azure.com/user_impersonation";
const response = await credential.getToken(scope);

if (response && response.token) {
console.log("Successfully acquired token with expiration at:", (new Date(response.expiresOnTimestamp)).toLocaleString());

return `${response.tokenType} ${response.token}`;
}
else {
throw new Error("Failed to acquire token: Empty response");
}
}
catch (error) {
console.error("Error acquiring portal token:", error.message);
throw error;
}
}

module.exports = {
getArmToken
};
Loading
Loading