|
1 | | -const { InteractiveBrowserCredential } = require('@azure/identity'); |
2 | | - |
3 | | -/** |
4 | | - * Gets authentication ARM token using interactive browser authentication |
5 | | - * @param {Object} options - Configuration options |
6 | | - * @param {string} [options.tenantId] - Optional tenant ID to override default tenant |
7 | | - * @param {string} [options.clientId] - Optional client ID for the application |
8 | | - * @returns {Promise<string>} - Bearer token for portal.azure.com |
9 | | - */ |
10 | | -async function getArmToken(options = {}) { |
11 | | - try { |
12 | | - const credential = new InteractiveBrowserCredential({ |
13 | | - tenantId: options.tenantId, |
14 | | - clientId: options.clientId |
15 | | - }); |
16 | | - console.log("Please sign in via the browser window that will open..."); |
17 | | - |
18 | | - // Get the token - this will open a browser window for authentication |
19 | | - const scope = "https://management.azure.com/user_impersonation"; |
20 | | - const response = await credential.getToken(scope); |
21 | | - |
22 | | - if (response && response.token) { |
23 | | - console.log("Successfully acquired token with expiration at:", (new Date(response.expiresOnTimestamp)).toLocaleString()); |
24 | | - return `${response.tokenType} ${response.token}`; |
25 | | - } else { |
26 | | - throw new Error("Failed to acquire token: Empty response"); |
27 | | - } |
28 | | - } catch (error) { |
29 | | - console.error("Error acquiring portal token:", error.message); |
30 | | - throw error; |
31 | | - } |
32 | | -} |
33 | | - |
34 | | -module.exports = { |
35 | | - getArmToken |
36 | | -}; |
| 1 | +const { InteractiveBrowserCredential } = require('@azure/identity'); |
| 2 | + |
| 3 | +/** |
| 4 | + * Gets authentication ARM token using interactive browser authentication |
| 5 | + * @param {Object} options - Configuration options |
| 6 | + * @param {string} [options.tenantId] - Optional tenant ID to override default tenant |
| 7 | + * @param {string} [options.clientId] - Optional client ID for the application |
| 8 | + * @returns {Promise<string>} - Bearer token for portal.azure.com |
| 9 | + */ |
| 10 | +async function getArmToken(options = {}) { |
| 11 | + try { |
| 12 | + const credential = new InteractiveBrowserCredential({ |
| 13 | + tenantId: options.tenantId, |
| 14 | + clientId: options.clientId, |
| 15 | + loginStyle: "popup" |
| 16 | + }); |
| 17 | + |
| 18 | + console.log("Please sign in via the browser window that will open..."); |
| 19 | + |
| 20 | + // Get the token - this will open a browser window for authentication |
| 21 | + const scope = "https://management.azure.com/user_impersonation"; |
| 22 | + const response = await credential.getToken(scope); |
| 23 | + |
| 24 | + if (response && response.token) { |
| 25 | + console.log("Successfully acquired token with expiration at:", (new Date(response.expiresOnTimestamp)).toLocaleString()); |
| 26 | + |
| 27 | + return `${response.tokenType} ${response.token}`; |
| 28 | + } |
| 29 | + else { |
| 30 | + throw new Error("Failed to acquire token: Empty response"); |
| 31 | + } |
| 32 | + } |
| 33 | + catch (error) { |
| 34 | + console.error("Error acquiring portal token:", error.message); |
| 35 | + throw error; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +module.exports = { |
| 40 | + getArmToken |
| 41 | +}; |
0 commit comments