@microsoft/vscode-azext-azureauth 1.1.3 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,6 +13,14 @@ The `AzureSubscriptionProvider` interface describes the functions of this packag
13
13
  * An interface for obtaining Azure subscription information
14
14
  */
15
15
  export interface AzureSubscriptionProvider {
16
+ /**
17
+ * Gets a list of tenants available to the user.
18
+ * Use {@link isSignedIn} to check if the user is signed in to a particular tenant.
19
+ *
20
+ * @returns A list of tenants.
21
+ */
22
+ getTenants(): Promise<TenantIdDescription[]>;
23
+
16
24
  /**
17
25
  * Gets a list of Azure subscriptions available to the user.
18
26
  *
@@ -30,16 +38,20 @@ export interface AzureSubscriptionProvider {
30
38
  /**
31
39
  * Checks to see if a user is signed in.
32
40
  *
41
+ * @param tenantId (Optional) Provide to check if a user is signed in to a specific tenant.
42
+ *
33
43
  * @returns True if the user is signed in, false otherwise.
34
44
  */
35
- isSignedIn(): Promise<boolean>;
45
+ isSignedIn(tenantId?: string): Promise<boolean>;
36
46
 
37
47
  /**
38
48
  * Asks the user to sign in or pick an account to use.
39
49
  *
50
+ * @param tenantId (Optional) Provide to sign in to a specific tenant.
51
+ *
40
52
  * @returns True if the user is signed in, false otherwise.
41
53
  */
42
- signIn(): Promise<boolean>;
54
+ signIn(tenantId?: string): Promise<boolean>;
43
55
 
44
56
  /**
45
57
  * An event that is fired when the user signs in. Debounced to fire at most once every 5 seconds.
@@ -88,6 +100,12 @@ export declare function getConfiguredAzureEnv(): azureEnv.Environment & {
88
100
  export declare function setConfiguredAzureEnv(cloud: string | azureEnv.EnvironmentParameters, target?: vscode.ConfigurationTarget): Promise<void>;
89
101
  ```
90
102
 
103
+ ## Logs
104
+
105
+ View the Microsoft Authentication extension logs by running the `Developer: Show Logs...` command from the VS Code command palette.
106
+
107
+ Change the log level by running the `Developer: Set Log Level...` command from the VS Code command palette. Select `Microsoft Authentication` from the list of loggers and then select the desired log level.
108
+
91
109
  ## License
92
110
 
93
111
  [MIT](LICENSE.md)
@@ -1,9 +1,17 @@
1
1
  import type * as vscode from 'vscode';
2
2
  import type { AzureSubscription } from './AzureSubscription';
3
+ import { TenantIdDescription } from '@azure/arm-subscriptions';
3
4
  /**
4
5
  * An interface for obtaining Azure subscription information
5
6
  */
6
7
  export interface AzureSubscriptionProvider {
8
+ /**
9
+ * Gets a list of tenants available to the user.
10
+ * Use {@link isSignedIn} to check if the user is signed in to a particular tenant.
11
+ *
12
+ * @returns A list of tenants.
13
+ */
14
+ getTenants(): Promise<TenantIdDescription[]>;
7
15
  /**
8
16
  * Gets a list of Azure subscriptions available to the user.
9
17
  *
@@ -20,15 +28,19 @@ export interface AzureSubscriptionProvider {
20
28
  /**
21
29
  * Checks to see if a user is signed in.
22
30
  *
31
+ * @param tenantId (Optional) Provide to check if a user is signed in to a specific tenant.
32
+ *
23
33
  * @returns True if the user is signed in, false otherwise.
24
34
  */
25
- isSignedIn(): Promise<boolean>;
35
+ isSignedIn(tenantId?: string): Promise<boolean>;
26
36
  /**
27
37
  * Asks the user to sign in or pick an account to use.
28
38
  *
39
+ * @param tenantId (Optional) Provide to sign in to a specific tenant.
40
+ *
29
41
  * @returns True if the user is signed in, false otherwise.
30
42
  */
31
- signIn(): Promise<boolean>;
43
+ signIn(tenantId?: string): Promise<boolean>;
32
44
  /**
33
45
  * An event that is fired when the user signs in. Debounced to fire at most once every 5 seconds.
34
46
  */
@@ -1,3 +1,4 @@
1
+ import type { TenantIdDescription } from '@azure/arm-subscriptions';
1
2
  import * as vscode from 'vscode';
2
3
  import type { AzureSubscription, SubscriptionId, TenantId } from './AzureSubscription';
3
4
  import type { AzureSubscriptionProvider } from './AzureSubscriptionProvider';
@@ -12,6 +13,13 @@ export declare class VSCodeAzureSubscriptionProvider extends vscode.Disposable i
12
13
  private readonly onDidSignOutEmitter;
13
14
  private lastSignOutEventFired;
14
15
  constructor();
16
+ /**
17
+ * Gets a list of tenants available to the user.
18
+ * Use {@link isSignedIn} to check if the user is signed in to a particular tenant.
19
+ *
20
+ * @returns A list of tenants.
21
+ */
22
+ getTenants(): Promise<TenantIdDescription[]>;
15
23
  /**
16
24
  * Gets a list of Azure subscriptions available to the user.
17
25
  *
@@ -36,9 +44,11 @@ export declare class VSCodeAzureSubscriptionProvider extends vscode.Disposable i
36
44
  /**
37
45
  * Asks the user to sign in or pick an account to use.
38
46
  *
47
+ * @param tenantId (Optional) Provide to sign in to a specific tenant.
48
+ *
39
49
  * @returns True if the user is signed in, false otherwise.
40
50
  */
41
- signIn(): Promise<boolean>;
51
+ signIn(tenantId?: string): Promise<boolean>;
42
52
  /**
43
53
  * An event that is fired when the user signs in. Debounced to fire at most once every 5 seconds.
44
54
  */
@@ -73,12 +83,6 @@ export declare class VSCodeAzureSubscriptionProvider extends vscode.Disposable i
73
83
  * @returns A list of subscription IDs that are configured in `azureResourceGroups.selectedSubscriptions`.
74
84
  */
75
85
  protected getSubscriptionFilters(): Promise<SubscriptionId[]>;
76
- /**
77
- * Gets the tenants available to a user.
78
- *
79
- * @returns The list of tenants visible to the user.
80
- */
81
- private getTenants;
82
86
  /**
83
87
  * Gets the subscriptions for a given tenant.
84
88
  *
@@ -95,20 +99,5 @@ export declare class VSCodeAzureSubscriptionProvider extends vscode.Disposable i
95
99
  * @returns A client, the credential used by the client, and the authentication function
96
100
  */
97
101
  private getSubscriptionClient;
98
- /**
99
- * Gets a normalized list of scopes
100
- *
101
- * @param scopes An input scope string, list, or undefined
102
- * @param tenantId (Optional) The tenant ID, will be added to the scopes
103
- *
104
- * @returns A list of scopes, with the default scope and (optionally) the tenant scope added
105
- */
106
- private getScopes;
107
- /**
108
- * Gets the default Azure scopes required for resource management,
109
- * depending on the configured endpoint
110
- *
111
- * @returns The default Azure scopes required
112
- */
113
- private getDefaultScopes;
102
+ private getToken;
114
103
  }
@@ -21,8 +21,10 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.VSCodeAzureSubscriptionProvider = void 0;
24
+ const cross_fetch_1 = require("cross-fetch");
24
25
  const vscode = require("vscode");
25
26
  const NotSignedInError_1 = require("./NotSignedInError");
27
+ const getSessionFromVSCode_1 = require("./getSessionFromVSCode");
26
28
  const configuredAzureEnv_1 = require("./utils/configuredAzureEnv");
27
29
  const EventDebounce = 5 * 1000; // 5 seconds
28
30
  /**
@@ -66,6 +68,22 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
66
68
  */
67
69
  this.onDidSignOut = this.onDidSignOutEmitter.event;
68
70
  }
71
+ /**
72
+ * Gets a list of tenants available to the user.
73
+ * Use {@link isSignedIn} to check if the user is signed in to a particular tenant.
74
+ *
75
+ * @returns A list of tenants.
76
+ */
77
+ getTenants() {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ const listTenantsResponse = yield (0, cross_fetch_1.default)('https://management.azure.com/tenants?api-version=2022-12-01', {
80
+ headers: {
81
+ Authorization: `Bearer ${yield this.getToken()}`,
82
+ }
83
+ });
84
+ return (yield listTenantsResponse.json()).value;
85
+ });
86
+ }
69
87
  /**
70
88
  * Gets a list of Azure subscriptions available to the user.
71
89
  *
@@ -124,18 +142,20 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
124
142
  */
125
143
  isSignedIn(tenantId) {
126
144
  return __awaiter(this, void 0, void 0, function* () {
127
- const session = yield vscode.authentication.getSession((0, configuredAzureEnv_1.getConfiguredAuthProviderId)(), this.getScopes([], tenantId), { createIfNone: false, silent: true });
145
+ const session = yield (0, getSessionFromVSCode_1.getSessionFromVSCode)([], tenantId, { createIfNone: false, silent: true });
128
146
  return !!session;
129
147
  });
130
148
  }
131
149
  /**
132
150
  * Asks the user to sign in or pick an account to use.
133
151
  *
152
+ * @param tenantId (Optional) Provide to sign in to a specific tenant.
153
+ *
134
154
  * @returns True if the user is signed in, false otherwise.
135
155
  */
136
- signIn() {
156
+ signIn(tenantId) {
137
157
  return __awaiter(this, void 0, void 0, function* () {
138
- const session = yield vscode.authentication.getSession((0, configuredAzureEnv_1.getConfiguredAuthProviderId)(), this.getDefaultScopes(), { createIfNone: true, clearSessionPreference: true });
158
+ const session = yield (0, getSessionFromVSCode_1.getSessionFromVSCode)([], tenantId, { createIfNone: true, clearSessionPreference: true });
139
159
  return !!session;
140
160
  });
141
161
  }
@@ -179,39 +199,6 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
179
199
  return fullSubscriptionIds.map(id => id.split('/')[1]);
180
200
  });
181
201
  }
182
- /**
183
- * Gets the tenants available to a user.
184
- *
185
- * @returns The list of tenants visible to the user.
186
- */
187
- getTenants() {
188
- var _a, e_1, _b, _c;
189
- return __awaiter(this, void 0, void 0, function* () {
190
- const { client } = yield this.getSubscriptionClient();
191
- const tenants = [];
192
- try {
193
- for (var _d = true, _e = __asyncValues(client.tenants.list()), _f; _f = yield _e.next(), _a = _f.done, !_a;) {
194
- _c = _f.value;
195
- _d = false;
196
- try {
197
- const tenant = _c;
198
- tenants.push(tenant);
199
- }
200
- finally {
201
- _d = true;
202
- }
203
- }
204
- }
205
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
206
- finally {
207
- try {
208
- if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
209
- }
210
- finally { if (e_1) throw e_1.error; }
211
- }
212
- return tenants;
213
- });
214
- }
215
202
  /**
216
203
  * Gets the subscriptions for a given tenant.
217
204
  *
@@ -220,7 +207,7 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
220
207
  * @returns The list of subscriptions for the tenant.
221
208
  */
222
209
  getSubscriptionsForTenant(tenantId) {
223
- var _a, e_2, _b, _c;
210
+ var _a, e_1, _b, _c;
224
211
  return __awaiter(this, void 0, void 0, function* () {
225
212
  const { client, credential, authentication } = yield this.getSubscriptionClient(tenantId);
226
213
  const environment = (0, configuredAzureEnv_1.getConfiguredAzureEnv)();
@@ -248,12 +235,12 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
248
235
  }
249
236
  }
250
237
  }
251
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
238
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
252
239
  finally {
253
240
  try {
254
241
  if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
255
242
  }
256
- finally { if (e_2) throw e_2.error; }
243
+ finally { if (e_1) throw e_1.error; }
257
244
  }
258
245
  return subscriptions;
259
246
  });
@@ -268,17 +255,17 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
268
255
  getSubscriptionClient(tenantId) {
269
256
  return __awaiter(this, void 0, void 0, function* () {
270
257
  const armSubs = yield Promise.resolve().then(() => require('@azure/arm-subscriptions'));
271
- // This gets filled in when the client calls `getToken`, and then it can be returned in the `authentication` property of `AzureSubscription`
272
- let session;
258
+ const getSession = (scopes) => __awaiter(this, void 0, void 0, function* () {
259
+ const session = yield (0, getSessionFromVSCode_1.getSessionFromVSCode)(scopes, tenantId, { createIfNone: false, silent: true });
260
+ if (!session) {
261
+ throw new NotSignedInError_1.NotSignedInError();
262
+ }
263
+ return session;
264
+ });
273
265
  const credential = {
274
266
  getToken: (scopes) => __awaiter(this, void 0, void 0, function* () {
275
- // TODO: if possible, change to `getSessions` when that API is available: https://github.com/microsoft/vscode/issues/152399
276
- session = yield vscode.authentication.getSession((0, configuredAzureEnv_1.getConfiguredAuthProviderId)(), this.getScopes(scopes, tenantId), { createIfNone: false, silent: true });
277
- if (!session) {
278
- throw new NotSignedInError_1.NotSignedInError();
279
- }
280
267
  return {
281
- token: session.accessToken,
268
+ token: (yield getSession(scopes)).accessToken,
282
269
  expiresOnTimestamp: 0
283
270
  };
284
271
  })
@@ -287,41 +274,19 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
287
274
  client: new armSubs.SubscriptionClient(credential),
288
275
  credential: credential,
289
276
  authentication: {
290
- getSession: () => session // Rewrapped to make TS not confused about the weird initialization pattern
277
+ getSession,
291
278
  }
292
279
  };
293
280
  });
294
281
  }
295
- /**
296
- * Gets a normalized list of scopes
297
- *
298
- * @param scopes An input scope string, list, or undefined
299
- * @param tenantId (Optional) The tenant ID, will be added to the scopes
300
- *
301
- * @returns A list of scopes, with the default scope and (optionally) the tenant scope added
302
- */
303
- getScopes(scopes, tenantId) {
304
- const scopeSet = new Set(this.getDefaultScopes());
305
- // If `.default` is passed in, it will be ignored, in favor of the correct default added by `getDefaultScopes`
306
- if (typeof scopes === 'string' && scopes !== '.default') {
307
- scopeSet.add(scopes);
308
- }
309
- else if (Array.isArray(scopes)) {
310
- scopes.filter(scope => scope !== '.default').forEach(scope => scopeSet.add(scope));
311
- }
312
- if (tenantId) {
313
- scopeSet.add(`VSCODE_TENANT:${tenantId}`);
314
- }
315
- return Array.from(scopeSet);
316
- }
317
- /**
318
- * Gets the default Azure scopes required for resource management,
319
- * depending on the configured endpoint
320
- *
321
- * @returns The default Azure scopes required
322
- */
323
- getDefaultScopes() {
324
- return [`${(0, configuredAzureEnv_1.getConfiguredAzureEnv)().resourceManagerEndpointUrl}.default`];
282
+ getToken(tenantId) {
283
+ return __awaiter(this, void 0, void 0, function* () {
284
+ const session = yield (0, getSessionFromVSCode_1.getSessionFromVSCode)([], tenantId, { createIfNone: false, silent: true });
285
+ if (!session) {
286
+ throw new NotSignedInError_1.NotSignedInError();
287
+ }
288
+ return session.accessToken;
289
+ });
325
290
  }
326
291
  }
327
292
  exports.VSCodeAzureSubscriptionProvider = VSCodeAzureSubscriptionProvider;
@@ -0,0 +1,12 @@
1
+ import * as vscode from "vscode";
2
+ /**
3
+ * Wraps {@link vscode.authentication.getSession} and handles:
4
+ * * Passing the configured auth provider id
5
+ * * Getting the list of scopes, adding the tenant id to the scope list if needed
6
+ *
7
+ * @param scopes - top-level resource scopes (e.g. http://management.azure.com, http://storage.azure.com) or .default scopes. All resources/scopes will be normalized to the `.default` scope for each resource.
8
+ * @param tenantId - (Optional) The tenant ID, will be added to the scopes
9
+ * @param options - see {@link vscode.AuthenticationGetSessionOptions}
10
+ * @returns An authentication session if available, or undefined if there are no sessions
11
+ */
12
+ export declare function getSessionFromVSCode(scopes?: string | string[], tenantId?: string, options?: vscode.AuthenticationGetSessionOptions): Promise<vscode.AuthenticationSession | undefined>;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Microsoft Corporation. All rights reserved.
4
+ * Licensed under the MIT License. See License.txt in the project root for license information.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
7
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
8
+ return new (P || (P = Promise))(function (resolve, reject) {
9
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
10
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
11
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
12
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
13
+ });
14
+ };
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.getSessionFromVSCode = void 0;
17
+ const configuredAzureEnv_1 = require("./utils/configuredAzureEnv");
18
+ const vscode = require("vscode");
19
+ function getResourceScopes(scopes) {
20
+ if (scopes === undefined || scopes === "" || scopes.length === 0) {
21
+ scopes = `${(0, configuredAzureEnv_1.getConfiguredAzureEnv)().resourceManagerEndpointUrl}.default`;
22
+ }
23
+ const arrScopes = (Array.isArray(scopes) ? scopes : [scopes])
24
+ .map((scope) => {
25
+ if (scope.endsWith('.default')) {
26
+ return scope;
27
+ }
28
+ else {
29
+ return `${scope}.default`;
30
+ }
31
+ });
32
+ return Array.from(new Set(arrScopes));
33
+ }
34
+ function addTenantIdScope(scopes, tenantId) {
35
+ const scopeSet = new Set(scopes);
36
+ scopeSet.add(`VSCODE_TENANT:${tenantId}`);
37
+ return Array.from(scopeSet);
38
+ }
39
+ function getScopes(scopes, tenantId) {
40
+ let scopeArr = getResourceScopes(scopes);
41
+ if (tenantId) {
42
+ scopeArr = addTenantIdScope(scopeArr, tenantId);
43
+ }
44
+ return scopeArr;
45
+ }
46
+ /**
47
+ * Wraps {@link vscode.authentication.getSession} and handles:
48
+ * * Passing the configured auth provider id
49
+ * * Getting the list of scopes, adding the tenant id to the scope list if needed
50
+ *
51
+ * @param scopes - top-level resource scopes (e.g. http://management.azure.com, http://storage.azure.com) or .default scopes. All resources/scopes will be normalized to the `.default` scope for each resource.
52
+ * @param tenantId - (Optional) The tenant ID, will be added to the scopes
53
+ * @param options - see {@link vscode.AuthenticationGetSessionOptions}
54
+ * @returns An authentication session if available, or undefined if there are no sessions
55
+ */
56
+ function getSessionFromVSCode(scopes, tenantId, options) {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ return yield vscode.authentication.getSession((0, configuredAzureEnv_1.getConfiguredAuthProviderId)(), getScopes(scopes, tenantId), options);
59
+ });
60
+ }
61
+ exports.getSessionFromVSCode = getSessionFromVSCode;
62
+ //# sourceMappingURL=getSessionFromVSCode.js.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@microsoft/vscode-azext-azureauth",
3
3
  "author": "Microsoft Corporation",
4
- "version": "1.1.3",
4
+ "version": "1.2.2",
5
5
  "description": "Azure authentication helpers for Visual Studio Code",
6
6
  "tags": [
7
7
  "azure",
@@ -52,6 +52,7 @@
52
52
  },
53
53
  "dependencies": {
54
54
  "@azure/arm-subscriptions": "^5.1.0",
55
- "@azure/ms-rest-azure-env": "^2.0.0"
55
+ "@azure/ms-rest-azure-env": "^2.0.0",
56
+ "cross-fetch": "^4.0.0"
56
57
  }
57
58
  }