@microsoft/vscode-azext-azureauth 1.1.3 → 1.2.1

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,13 +99,16 @@ 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;
102
+ private getToken;
98
103
  /**
99
- * Gets a normalized list of scopes
104
+ * Gets a normalized list of scopes. If no scopes are provided, the return value of {@link getDefaultScope} is used.
105
+ *
106
+ * Only supports top-level resource scopes (e.g. http://management.azure.com, http://storage.azure.com) or .default scopes.
107
+ *
108
+ * All resources/scopes will be normalized to the `.default` scope for each resource.
100
109
  *
101
110
  * @param scopes An input scope string, list, or undefined
102
111
  * @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
112
  */
106
113
  private getScopes;
107
114
  /**
@@ -110,5 +117,5 @@ export declare class VSCodeAzureSubscriptionProvider extends vscode.Disposable i
110
117
  *
111
118
  * @returns The default Azure scopes required
112
119
  */
113
- private getDefaultScopes;
120
+ private getDefaultScope;
114
121
  }
@@ -21,6 +21,7 @@ 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");
26
27
  const configuredAzureEnv_1 = require("./utils/configuredAzureEnv");
@@ -66,6 +67,22 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
66
67
  */
67
68
  this.onDidSignOut = this.onDidSignOutEmitter.event;
68
69
  }
70
+ /**
71
+ * Gets a list of tenants available to the user.
72
+ * Use {@link isSignedIn} to check if the user is signed in to a particular tenant.
73
+ *
74
+ * @returns A list of tenants.
75
+ */
76
+ getTenants() {
77
+ return __awaiter(this, void 0, void 0, function* () {
78
+ const listTenantsResponse = yield (0, cross_fetch_1.default)('https://management.azure.com/tenants?api-version=2022-12-01', {
79
+ headers: {
80
+ Authorization: `Bearer ${yield this.getToken()}`,
81
+ }
82
+ });
83
+ return (yield listTenantsResponse.json()).value;
84
+ });
85
+ }
69
86
  /**
70
87
  * Gets a list of Azure subscriptions available to the user.
71
88
  *
@@ -131,11 +148,13 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
131
148
  /**
132
149
  * Asks the user to sign in or pick an account to use.
133
150
  *
151
+ * @param tenantId (Optional) Provide to sign in to a specific tenant.
152
+ *
134
153
  * @returns True if the user is signed in, false otherwise.
135
154
  */
136
- signIn() {
155
+ signIn(tenantId) {
137
156
  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 });
157
+ const session = yield vscode.authentication.getSession((0, configuredAzureEnv_1.getConfiguredAuthProviderId)(), this.getScopes([], tenantId), { createIfNone: true, clearSessionPreference: true });
139
158
  return !!session;
140
159
  });
141
160
  }
@@ -179,39 +198,6 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
179
198
  return fullSubscriptionIds.map(id => id.split('/')[1]);
180
199
  });
181
200
  }
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
201
  /**
216
202
  * Gets the subscriptions for a given tenant.
217
203
  *
@@ -220,7 +206,7 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
220
206
  * @returns The list of subscriptions for the tenant.
221
207
  */
222
208
  getSubscriptionsForTenant(tenantId) {
223
- var _a, e_2, _b, _c;
209
+ var _a, e_1, _b, _c;
224
210
  return __awaiter(this, void 0, void 0, function* () {
225
211
  const { client, credential, authentication } = yield this.getSubscriptionClient(tenantId);
226
212
  const environment = (0, configuredAzureEnv_1.getConfiguredAzureEnv)();
@@ -248,12 +234,12 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
248
234
  }
249
235
  }
250
236
  }
251
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
237
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
252
238
  finally {
253
239
  try {
254
240
  if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
255
241
  }
256
- finally { if (e_2) throw e_2.error; }
242
+ finally { if (e_1) throw e_1.error; }
257
243
  }
258
244
  return subscriptions;
259
245
  });
@@ -268,17 +254,17 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
268
254
  getSubscriptionClient(tenantId) {
269
255
  return __awaiter(this, void 0, void 0, function* () {
270
256
  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;
257
+ const getSession = (scopes) => __awaiter(this, void 0, void 0, function* () {
258
+ const session = yield vscode.authentication.getSession((0, configuredAzureEnv_1.getConfiguredAuthProviderId)(), this.getScopes(scopes, tenantId), { createIfNone: false, silent: true });
259
+ if (!session) {
260
+ throw new NotSignedInError_1.NotSignedInError();
261
+ }
262
+ return session;
263
+ });
273
264
  const credential = {
274
265
  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
266
  return {
281
- token: session.accessToken,
267
+ token: (yield getSession(this.getScopes(scopes, tenantId))).accessToken,
282
268
  expiresOnTimestamp: 0
283
269
  };
284
270
  })
@@ -287,28 +273,44 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
287
273
  client: new armSubs.SubscriptionClient(credential),
288
274
  credential: credential,
289
275
  authentication: {
290
- getSession: () => session // Rewrapped to make TS not confused about the weird initialization pattern
276
+ getSession
291
277
  }
292
278
  };
293
279
  });
294
280
  }
281
+ getToken(tenantId) {
282
+ return __awaiter(this, void 0, void 0, function* () {
283
+ const session = yield vscode.authentication.getSession((0, configuredAzureEnv_1.getConfiguredAuthProviderId)(), this.getScopes([], tenantId), { createIfNone: false, silent: true });
284
+ if (!session) {
285
+ throw new NotSignedInError_1.NotSignedInError();
286
+ }
287
+ return session.accessToken;
288
+ });
289
+ }
295
290
  /**
296
- * Gets a normalized list of scopes
291
+ * Gets a normalized list of scopes. If no scopes are provided, the return value of {@link getDefaultScope} is used.
292
+ *
293
+ * Only supports top-level resource scopes (e.g. http://management.azure.com, http://storage.azure.com) or .default scopes.
294
+ *
295
+ * All resources/scopes will be normalized to the `.default` scope for each resource.
297
296
  *
298
297
  * @param scopes An input scope string, list, or undefined
299
298
  * @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
299
  */
303
300
  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));
301
+ if (scopes === undefined || scopes === "" || scopes.length === 0) {
302
+ scopes = this.getDefaultScope();
311
303
  }
304
+ const arrScopes = (Array.isArray(scopes) ? scopes : [scopes])
305
+ .map((scope) => {
306
+ if (scope.endsWith('.default')) {
307
+ return scope;
308
+ }
309
+ else {
310
+ return `${scope}.default`;
311
+ }
312
+ });
313
+ const scopeSet = new Set(arrScopes);
312
314
  if (tenantId) {
313
315
  scopeSet.add(`VSCODE_TENANT:${tenantId}`);
314
316
  }
@@ -320,8 +322,8 @@ class VSCodeAzureSubscriptionProvider extends vscode.Disposable {
320
322
  *
321
323
  * @returns The default Azure scopes required
322
324
  */
323
- getDefaultScopes() {
324
- return [`${(0, configuredAzureEnv_1.getConfiguredAzureEnv)().resourceManagerEndpointUrl}.default`];
325
+ getDefaultScope() {
326
+ return `${(0, configuredAzureEnv_1.getConfiguredAzureEnv)().resourceManagerEndpointUrl}.default`;
325
327
  }
326
328
  }
327
329
  exports.VSCodeAzureSubscriptionProvider = VSCodeAzureSubscriptionProvider;
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.1",
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
  }