This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
base.ts
266 lines (234 loc) · 7.03 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// ------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
// See License in the project root for license information.
// ------------------------------------------------------------------------------
import { isAuthenticated } from './authentication/auth';
import { IHarFormat } from './history/har/IHarFormat';
import { Tokens } from './tokens';
export interface IExplorerOptions {
AuthUrl?: string;
ClientId?: string;
Language?: string;
RedirectUrl?: string;
DefaultUserScopes?: string[];
GraphVersions?: string[];
PathToBuildDir: string;
}
export let Methods: RequestType[] = [
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
];
export type GraphApiVersion = 'v1.0' | 'beta';
export let GraphApiVersions: GraphApiVersion[] = ['v1.0', 'beta'];
export type AuthenticationStatus = 'anonymous' | 'authenticating' | 'authenticated';
export type RequestType = 'GET' | 'PUT' | 'POST' | 'GET_BINARY' | 'POST' | 'PATCH' | 'DELETE';
/***
* Represents a scope.
*/
export interface IPermissionScope {
/**
* The scope name.
*/
name: string;
/**
* A short description of the scope.
*/
description: string;
/**
* A long description of the scope.
*/
longDescription: string;
/**
* Specifies whether the scope is currently in preview.
*/
preview: boolean;
/**
* Specifies whether the property is only consent-able via admin consent.
*/
admin: boolean;
/**
* Specifies whether the user has already consented to the scope.
*/
consented?: boolean;
/**
* Specifies whether the user wants to request this scope. Used in the scopes
* dialog for checking/unchecking before scope is actually enabled in the token.
*/
requested?: boolean;
}
export interface IGraphApiCall {
statusCode?: number;
duration?: number;
method?: RequestType;
humanName?: string;
requestUrl?: string;
postBody?: string;
headers?: IGraphRequestHeader[];
requestSentAt?: Date;
relativeDate?: string;
har?: string;
}
export interface ISampleQuery extends IGraphApiCall {
docLink?: string;
AAD?: boolean;
skipTest?: boolean;
MSA?: boolean;
category: string;
tip?: string;
}
export interface ISampleQueryCategory {
title: string;
enabled?: boolean;
queries?: ISampleQuery[];
}
export interface IExplorerValues {
selectedOption?: RequestType;
selectedVersion?: GraphApiVersion;
endpointUrl?: string;
authentication?: {
status?: AuthenticationStatus
user?: {
displayName?: string
emailAddress?: string
profileImageUrl?: string,
},
};
showImage?: boolean;
requestInProgress?: boolean;
headers?: IGraphRequestHeader[];
postBody?: string;
}
export interface IGraphRequestHeader {
name: string;
value: string;
enabled?: boolean;
readonly?: boolean;
}
export interface IAutoCompleteItem {
url: string;
fullUrl: string;
}
export const CommonHeaders = [
'Accept',
'Accept-Charset',
'Accept-Encoding',
'Accept-Language',
'Accept-Datetime',
'Authorization',
'Cache-Control',
'Connection',
'Cookie',
'Content-Length',
'Content-MD5',
'Content-Type',
'Date',
'Expect',
'Forwarded',
'From',
'Host',
'If-Match',
'If-Modified-Since',
'If-None-Match',
'If-Range',
'If-Unmodified-Since',
'Max-Forwards',
'Origin',
'Pragma',
'Proxy-Authorization',
'Range',
'User-Agent',
'Upgrade',
'Via',
'Warning',
];
/**
* Tokens are used as placeholder values in sample queries to cover many scenarios:
* - ID tokens for sample tenant nodes like user IDs, file IDs and other string constants
* - Tokens that must be determined at runtime like the current date
* - Tokens that are determined from the authenticated users session
* - Tokens can be in the POST body or part of the URL
*
* The token fields are split into default, demo and authenticated. If neither the demo or
* auth values are supplied, the token falls back to the default value.
*
* Tokens are maintained in tokens.ts.
*/
export interface IToken {
placeholder: string;
// Base defaults to replace the placeholder with. Not used if any of the below are defined
defaultValue?: string;
defaultValueFn?: Function;
// When the user is not authenticated, use these values for the demo tenant
demoTenantValue?: string;
demoTenantValueFn?: Function;
// When the user is authenticated with MSA or AAD, replace token with these values
authenticatedUserValue?: string;
authenticatedUserValueFn?: Function;
}
/*
* Given a token, go through each of the possible replacement scenarios and find which value to
* replace the token with.
* Order: Authenticated user values, demo tenant replacament values, default replacement values.
*/
function getTokenSubstituteValue(token: IToken) {
const priorityOrder = []; // Desc
if (isAuthenticated()) {
priorityOrder.push(token.authenticatedUserValueFn);
priorityOrder.push(token.authenticatedUserValue);
} else {
priorityOrder.push(token.demoTenantValueFn);
priorityOrder.push(token.demoTenantValue);
}
priorityOrder.push(token.defaultValueFn);
priorityOrder.push(token.defaultValue);
for (const tokenVal of priorityOrder) {
if (!tokenVal) {
continue;
}
if (typeof tokenVal === 'string') {
return tokenVal;
} else if (typeof tokenVal === 'function') {
return tokenVal();
}
}
}
/**
* Given a query, replace all tokens in the request URL and the POST body with thier
* values. When a token is found, use getTokenSubstituteValue() to find the right
* value to substitute based on the session.
*/
export function substituteTokens(query: ISampleQuery) {
type QueryFields = keyof IGraphApiCall;
for (const token of Tokens) {
const queryFieldsToCheck: QueryFields[] = ['requestUrl', 'postBody'];
for (const queryField of queryFieldsToCheck) {
if (!query[queryField]) { // If the sample doesn't have a post body, don't search for tokens in it
continue;
}
if ((query[queryField] as string).indexOf(`{${token.placeholder}}`) !== -1) {
const substitutedValue = getTokenSubstituteValue(token);
if (!substitutedValue) {
continue;
}
query[queryField] = (query[queryField] as string).replace(`{${token.placeholder}}`, substitutedValue);
}
}
}
}
export interface IMessage {
title: string;
body: string;
}
export interface IMessageBarContent {
icon: string;
backgroundClass: string;
text: string;
}
export let AllowedGraphDomains = [
'https://graph.microsoft.com',
'https://canary.graph.microsoft.com',
'https://microsoftgraph.chinacloudapi.cn',
];