Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/microsoft-authentication/src/common/scopeData.ts
3320 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Uri } from 'vscode';
7
8
const DEFAULT_CLIENT_ID = 'aebc6443-996d-45c2-90f0-388ff96faa56';
9
const DEFAULT_TENANT = 'organizations';
10
11
const OIDC_SCOPES = ['openid', 'email', 'profile', 'offline_access'];
12
const GRAPH_TACK_ON_SCOPE = 'User.Read';
13
14
export class ScopeData {
15
/**
16
* The full list of scopes including:
17
* * the original scopes passed to the constructor
18
* * internal VS Code scopes (e.g. `VSCODE_CLIENT_ID:...`)
19
* * the default scopes (`openid`, `email`, `profile`, `offline_access`)
20
*/
21
readonly allScopes: string[];
22
23
/**
24
* The full list of scopes as a space-separated string. For logging.
25
*/
26
readonly scopeStr: string;
27
28
/**
29
* The list of scopes to send to the token endpoint. This is the same as `scopes` but without the internal VS Code scopes.
30
*/
31
readonly scopesToSend: string[];
32
33
/**
34
* The client ID to use for the token request. This is the value of the `VSCODE_CLIENT_ID:...` scope if present, otherwise the default client ID.
35
*/
36
readonly clientId: string;
37
38
/**
39
* The tenant ID or `organizations`, `common`, `consumers` to use for the token request. This is the value of the `VSCODE_TENANT:...` scope if present, otherwise it's the default.
40
*/
41
readonly tenant: string;
42
43
/**
44
* The tenant ID to use for the token request. This will only ever be a GUID if one was specified via the `VSCODE_TENANT:...` scope, otherwise undefined.
45
*/
46
readonly tenantId: string | undefined;
47
48
/**
49
* The claims to include in the token request.
50
*/
51
readonly claims?: string;
52
53
constructor(readonly originalScopes: readonly string[] = [], claims?: string, authorizationServer?: Uri) {
54
const modifiedScopes = [...originalScopes];
55
modifiedScopes.sort();
56
this.allScopes = modifiedScopes;
57
this.scopeStr = modifiedScopes.join(' ');
58
this.claims = claims;
59
this.scopesToSend = this.getScopesToSend(modifiedScopes);
60
this.clientId = this.getClientId(this.allScopes);
61
this.tenant = this.getTenant(this.allScopes, authorizationServer);
62
this.tenantId = this.getTenantId(this.tenant);
63
}
64
65
private getClientId(scopes: string[]): string {
66
return scopes.reduce<string | undefined>((prev, current) => {
67
if (current.startsWith('VSCODE_CLIENT_ID:')) {
68
return current.split('VSCODE_CLIENT_ID:')[1];
69
}
70
return prev;
71
}, undefined) ?? DEFAULT_CLIENT_ID;
72
}
73
74
private getTenant(scopes: string[], authorizationServer?: Uri): string {
75
if (authorizationServer?.path) {
76
// Get tenant portion of URL
77
const tenant = authorizationServer.path.split('/')[1];
78
if (tenant) {
79
return tenant;
80
}
81
}
82
return scopes.reduce<string | undefined>((prev, current) => {
83
if (current.startsWith('VSCODE_TENANT:')) {
84
return current.split('VSCODE_TENANT:')[1];
85
}
86
return prev;
87
}, undefined) ?? DEFAULT_TENANT;
88
}
89
90
private getTenantId(tenant: string): string | undefined {
91
switch (tenant) {
92
case 'organizations':
93
case 'common':
94
case 'consumers':
95
// These are not valid tenant IDs, so we return undefined
96
return undefined;
97
default:
98
return this.tenant;
99
}
100
}
101
102
private getScopesToSend(scopes: string[]): string[] {
103
const scopesToSend = scopes.filter(s => !s.startsWith('VSCODE_'));
104
105
const set = new Set(scopesToSend);
106
for (const scope of OIDC_SCOPES) {
107
set.delete(scope);
108
}
109
110
// If we only had OIDC scopes, we need to add a tack-on scope to make the request valid
111
// by forcing Identity into treating this as a Graph token request.
112
if (!set.size) {
113
scopesToSend.push(GRAPH_TACK_ON_SCOPE);
114
}
115
return scopesToSend;
116
}
117
}
118
119