forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
628 lines (538 loc) · 23.9 KB
/
Copy pathauth.ts
File metadata and controls
628 lines (538 loc) · 23.9 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
namespace pxt.auth {
const EMPTY_USERNAME = "??";
const AUTH_CONTAINER = "auth"; // local storage "namespace".
const CSRF_TOKEN_KEY = "csrf-token"; // stored in local storage.
const AUTH_LOGIN_STATE_KEY = "login-state"; // stored in local storage.
const AUTH_USER_STATE_KEY = "user-state"; // stored in local storage.
const X_PXT_TARGET = "x-pxt-target"; // header passed in auth rest calls.
export type ApiResult<T> = {
resp: T;
statusCode: number;
success: boolean;
err: any;
};
const DEV_BACKEND_DEFAULT = "";
const DEV_BACKEND_PROD = "https://www.makecode.com";
const DEV_BACKEND_STAGING = "https://staging.pxt.io";
// Localhost endpoints. Ensure matching port number in pxt-backend/node/.vscode/launch.json
const DEV_BACKEND_LOCALHOST_5500 = "http://localhost:5500"; // if running in Docker container
const DEV_BACKEND_LOCALHOST_8080 = "http://localhost:8080"; // if not running in Docker
const DEV_BACKEND = DEV_BACKEND_STAGING;
let authDisabled = false;
export type UserProfile = {
id?: string;
idp?: {
provider?: pxt.IdentityProviderId;
username?: string;
displayName?: string;
picture?: {
mimeType?: string;
width?: number;
height?: number;
encoded?: string;
dataUrl?: string;
};
};
};
export type UserSkillmapState = {
mapProgress: any;
completedTags: any;
};
/**
* User preference state that should be synced with the cloud.
*/
export type UserPreferences = {
language?: string;
highContrast?: boolean;
reader?: string;
skillmap?: UserSkillmapState;
};
export const DEFAULT_USER_PREFERENCES: () => UserPreferences = () => ({
highContrast: false,
language: pxt.appTarget.appTheme.defaultLocale,
reader: "",
skillmap: {mapProgress: {}, completedTags: {}}
});
/**
* Cloud-synced user state.
*/
export type State = {
profile?: UserProfile;
preferences?: UserPreferences;
};
let _client: AuthClient;
export function client(): AuthClient { return _client; }
const PREFERENCES_DEBOUNCE_MS = 5 * 1000;
const PREFERENCES_DEBOUNCE_MAX_MS = 30 * 1000;
let debouncePreferencesChangedTimeout = 0;
let debouncePreferencesChangedStarted = 0;
export abstract class AuthClient {
/**
* In-memory cache of user profile state. This is persisted in local storage.
* DO NOT ACCESS DIRECTLY UNLESS YOU KNOW IT IS OK. Functions allowed direct
* access are marked as such below.
*/
private state$: Readonly<State>;
constructor() {
pxt.Util.assert(!_client);
// Set global instance.
_client = this;
}
public async initAsync() {
// Load state from local storage
try {
this.state$ = await pxt.storage.shared.getAsync<State>(AUTH_CONTAINER, AUTH_USER_STATE_KEY);
} catch { }
if (!this.state$) {
this.state$ = {};
}
this.setUserProfileAsync(this.state$.profile);
this.setUserPreferencesAsync(this.state$.preferences);
}
protected abstract onSignedIn(): Promise<void>;
protected abstract onSignedOut(): Promise<void>;
protected abstract onSignInFailed(): Promise<void>;
protected abstract onUserProfileChanged(): Promise<void>;
protected abstract onUserPreferencesChanged(diff: ts.pxtc.jsonPatch.PatchOperation[]): Promise<void>;
protected abstract onProfileDeleted(userId: string): Promise<void>;
protected abstract onApiError(err: any): Promise<void>;
protected abstract onStateCleared(): Promise<void>
/**
* Starts the process of authenticating the user against the given identity
* provider. Upon success the backend will write an http-only session cookie
* to the response, containing the authorization token. This cookie is not
* accessible in code, but will be included in all subsequent http requests.
* @param idp The id of the identity provider.
* @param persistent Whether or not to remember this login across sessions.
* @param callbackState The URL hash and params to return to after the auth
* flow completes.
*/
public async loginAsync(idp: pxt.IdentityProviderId, persistent: boolean, callbackState: CallbackState = undefined) {
if (!hasIdentity() || !idpEnabled(idp)) { return; }
callbackState = callbackState ?? NilCallbackState;
const state = this.getState();
// See if we have a valid access token already.
if (!state.profile) { await this.authCheckAsync(); }
const currIdp = state.profile?.idp;
// Check if we're already signed into this identity provider.
if (currIdp === idp) {
pxt.debug(`loginAsync: Already signed into ${idp}.`);
return;
}
this.clearState();
// Store some continuation state in local storage so we can return to what
// the user was doing before signing in.
const genId = () => (Math.PI * Math.random()).toString(36).slice(2);
const loginState: LoginState = {
key: genId(),
callbackState,
callbackPathname: window.location.pathname,
idp,
};
await pxt.storage.shared.setAsync(AUTH_CONTAINER, AUTH_LOGIN_STATE_KEY, loginState);
// Redirect to the login endpoint.
const loginUrl = pxt.Util.stringifyQueryString('/api/auth/login', {
response_type: "token",
provider: idp,
persistent,
redirect_uri: `${window.location.origin}${window.location.pathname}?authcallback=1&state=${loginState.key}`
})
const apiResult = await this.apiAsync<LoginResponse>(loginUrl);
if (apiResult.success) {
pxt.tickEvent('auth.login.start', { 'provider': idp });
window.location.href = apiResult.resp.loginUrl;
} else {
await this.onSignInFailed();
}
}
/**
* Sign out the user and clear the auth token cookie.
*/
public async logoutAsync(continuationHash?: string) {
if (!hasIdentity()) { return; }
pxt.tickEvent('auth.logout');
// backend will clear the cookie token and pass back the provider logout endpoint.
await this.apiAsync('/api/auth/logout');
// Clear csrf token so we can no longer make authenticated requests.
await pxt.storage.shared.delAsync(AUTH_CONTAINER, CSRF_TOKEN_KEY);
// Update state and UI to reflect logged out state.
this.clearState();
const hash = continuationHash ? continuationHash.startsWith('#') ? continuationHash : `#${continuationHash}` : "";
// Redirect to home screen, or skillmap home screen
if (pxt.BrowserUtils.hasWindow()) {
window.location.href = `${window.location.origin}${window.location.pathname}${hash}`;
location.reload();
}
}
public async deleteProfileAsync() {
// only if we're logged in
if (!await this.loggedInAsync()) { return; }
const userId = this.getState().profile?.id;
const res = await this.apiAsync('/api/user', null, 'DELETE');
if (res.err) {
await this.onApiError((res.err));
} else {
try {
// Clear csrf token so we can no longer make authenticated requests.
await pxt.storage.shared.delAsync(AUTH_CONTAINER, CSRF_TOKEN_KEY);
try {
await this.onProfileDeleted(userId);
} catch {
pxt.tickEvent('auth.profile.cloudToLocalFailed');
}
// Update state and UI to reflect logged out state.
this.clearState();
}
finally {
pxt.tickEvent('auth.profile.deleted');
}
}
}
private initialUserPreferences_: Promise<UserPreferences | undefined> = undefined;
public async initialUserPreferencesAsync(): Promise<UserPreferences | undefined> {
// only if we're logged in
if (!await this.loggedInAsync()) { return undefined; }
if (!this.initialUserPreferences_) {
this.initialUserPreferences_ = this.fetchUserPreferencesAsync();
}
return this.initialUserPreferences_;
}
public async userProfileAsync(): Promise<UserProfile | undefined> {
if (!await this.loggedInAsync()) { return undefined; }
const state = this.getState();
return { ...state.profile };
}
public async userPreferencesAsync(): Promise<UserPreferences | undefined> {
//if (!await this.loggedInAsync()) { return undefined; } // allow even when not signed in.
const state = this.getState();
return { ...state.preferences };
}
private initialAuthCheck_: Promise<UserProfile | undefined> = undefined;
/**
* Checks to see if we're already logged in by trying to fetch user info from
* the backend. If we have a valid auth token cookie, it will succeed.
*/
public async authCheckAsync(): Promise<UserProfile | undefined> {
if (!hasIdentity()) { return undefined; }
// Fail fast if we don't have csrf token.
if (!(await pxt.storage.shared.getAsync(AUTH_CONTAINER, CSRF_TOKEN_KEY))) { return undefined; }
const state = this.getState();
if (state.profile?.id) {
if (!this.initialAuthCheck_) {
this.initialAuthCheck_ = Promise.resolve(state.profile);
}
} else if (!this.initialAuthCheck_) {
// Optimistically try to fetch user profile. It will succeed if we have a valid
// session cookie. Upon success, virtual api state will be updated, and the UI
// will update accordingly.
this.initialAuthCheck_ = this.fetchUserAsync();
}
return this.initialAuthCheck_;
}
public async loggedInAsync(): Promise<boolean> {
await this.authCheckAsync();
return this.hasUserId();
}
public async updateUserProfileAsync(opts: {
username?: string,
avatarUrl?: string
}): Promise<boolean> {
if (!await this.loggedInAsync()) { return false; }
const state = this.getState();
const result = await this.apiAsync<UserProfile>('/api/user/profile', {
id: state.profile.id,
username: opts.username,
avatarUrl: opts.avatarUrl
} as UserProfile);
if (result.success) {
// Set user profile from returned value
await this.setUserProfileAsync(result.resp);
}
return result.success;
}
public async patchUserPreferencesAsync(ops: ts.pxtc.jsonPatch.PatchOperation | ts.pxtc.jsonPatch.PatchOperation[]) {
ops = Array.isArray(ops) ? ops : [ops];
if (!ops.length) { return; }
const curPref = await this.userPreferencesAsync();
ts.pxtc.jsonPatch.patchInPlace(curPref, ops);
await this.setUserPreferencesAsync(curPref);
// If we're not logged in, non-persistent local state is all we'll use
if (!await this.loggedInAsync()) { return; }
// If the user is logged in, save to cloud, but debounce the api call as this can be called frequently from skillmaps
clearTimeout(debouncePreferencesChangedTimeout);
const savePrefs = async () => {
debouncePreferencesChangedStarted = 0;
const result = await this.apiAsync<UserPreferences>('/api/user/preferences', ops, 'PATCH');
if (result.success) {
pxt.debug("Updating local user preferences w/ cloud data after result of POST")
// Set user profile from returned value so we stay in-sync
this.setUserPreferencesAsync(result.resp)
} else {
pxt.reportError("identity", "update preferences failed", result as any);
}
}
if (!debouncePreferencesChangedStarted) {
debouncePreferencesChangedStarted = U.now();
}
if (PREFERENCES_DEBOUNCE_MAX_MS < U.now() - debouncePreferencesChangedStarted) {
await savePrefs();
} else {
debouncePreferencesChangedTimeout = setTimeout(savePrefs, PREFERENCES_DEBOUNCE_MS);
}
}
/*protected*/ hasUserId(): boolean {
if (!hasIdentity()) { return false; }
const state = this.getState();
return !!state.profile?.id;
}
private async fetchUserAsync(): Promise<UserProfile | undefined> {
if (!hasIdentity()) { return undefined; }
const state = this.getState();
// We already have a user, no need to get it again.
if (state.profile?.id) {
return state.profile;
}
const result = await this.apiAsync('/api/user/profile');
if (result.success) {
const profile: UserProfile = result.resp;
await this.setUserProfileAsync(profile);
return profile;
}
return undefined
}
private async setUserProfileAsync(profile: UserProfile) {
const wasLoggedIn = this.hasUserId();
this.transformUserProfile(profile);
const isLoggedIn = this.hasUserId();
this.onUserProfileChanged();
//pxt.data.invalidate(USER_PROFILE);
if (isLoggedIn && !wasLoggedIn) {
await this.onSignedIn();
//pxt.data.invalidate(LOGGED_IN);
} else if (!isLoggedIn && wasLoggedIn) {
await this.onSignedOut();
//pxt.data.invalidate(LOGGED_IN);
}
}
private async setUserPreferencesAsync(newPref: Partial<UserPreferences>) {
const oldPref = this.getState().preferences ?? DEFAULT_USER_PREFERENCES()
const diff = ts.pxtc.jsonPatch.diff(oldPref, newPref);
// update
this.transformUserPreferences({
...oldPref,
...newPref
});
await this.onUserPreferencesChanged(diff);
}
private async fetchUserPreferencesAsync(): Promise<UserPreferences | undefined> {
// Wait for the initial auth
if (!await this.loggedInAsync()) { return undefined; }
const state = this.getState();
const result = await this.apiAsync<Partial<UserPreferences>>('/api/user/preferences');
if (result.success) {
// Set user profile from returned value
if (result.resp) {
// Note the cloud should send partial information back if it is missing
// a field. So e.g. if the language has never been set in the cloud, it won't
// overwrite the local state.
this.setUserPreferencesAsync(result.resp);
// update our one-time promise for the initial load
return state.preferences;
}
}
return undefined;
}
/**
* Updates user profile state and writes it to local storage.
* Direct access to state$ allowed.
*/
private transformUserProfile(profile: UserProfile) {
this.state$ = {
...this.state$,
profile: {
...profile
}
};
this.saveState();
}
/**
* Updates user preference state and writes it to local storage.
* Direct access to state$ allowed.
*/
protected transformUserPreferences(preferences: UserPreferences) {
this.state$ = {
...this.state$,
preferences: {
...preferences
}
};
this.saveState();
}
/**
* Read-only access to current state.
* Direct access to state$ allowed.
*/
/*private*/ getState(): Readonly<State> {
return this.state$;
};
/**
* Write auth state to local storage.
* Direct access to state$ allowed.
*/
private saveState() {
pxt.storage.shared.setAsync(AUTH_CONTAINER, AUTH_USER_STATE_KEY, this.state$).then(() => { });
}
/**
* Clear all auth state.
* Direct access to state$ allowed.
*/
private clearState() {
this.state$ = {};
pxt.storage.shared.delAsync(AUTH_CONTAINER, AUTH_USER_STATE_KEY)
.then(() => this.onStateCleared());
}
/*protected*/ async apiAsync<T = any>(url: string, data?: any, method?: string): Promise<ApiResult<T>> {
const headers: pxt.Map<string> = {};
const csrfToken = await pxt.storage.shared.getAsync<string>(AUTH_CONTAINER, CSRF_TOKEN_KEY);
if (csrfToken) {
headers["authorization"] = `mkcd ${csrfToken}`;
}
headers[X_PXT_TARGET] = pxt.appTarget?.id;
url = pxt.BrowserUtils.isLocalHostDev() ? `${DEV_BACKEND}${url}` : url;
return pxt.Util.requestAsync({
url,
headers,
data,
method: method ? method : data ? "POST" : "GET",
withCredentials: true, // Include cookies and authorization header in request, subject to CORS policy.
}).then(r => {
return {
statusCode: r.statusCode,
resp: r.json,
success: Math.floor(r.statusCode / 100) === 2,
err: null
}
}).catch(async (e) => {
if (!/logout/.test(url) && e.statusCode == 401) {
// 401/Unauthorized. logout now.
await this.logoutAsync();
}
return {
statusCode: e.statusCode,
err: e,
resp: null,
success: false
}
});
}
}
export type CallbackState = {
hash?: string;
params?: pxt.Map<string>;
};
const NilCallbackState = {
hash: '',
params: {}
};
/**
* During login, we store some state in local storage so we know where to
* redirect after login completes. This is the shape of that state.
*/
type LoginState = {
key: string;
callbackState: CallbackState;
callbackPathname: string;
idp: pxt.IdentityProviderId;
};
type LoginResponse = {
loginUrl: string;
};
export async function loginCallbackAsync(qs: pxt.Map<string>): Promise<void> {
let loginState: LoginState;
let callbackState: CallbackState = { ...NilCallbackState };
do {
// Read and remove auth state from local storage
loginState = await pxt.storage.shared.getAsync<LoginState>(AUTH_CONTAINER, AUTH_LOGIN_STATE_KEY);
if (!loginState) {
pxt.debug("Auth state not found in storge.");
return;
}
await pxt.storage.shared.delAsync(AUTH_CONTAINER, AUTH_LOGIN_STATE_KEY)
const stateKey = qs['state'];
if (!stateKey || loginState.key !== stateKey) {
pxt.debug("Failed to get auth state for key");
return;
}
callbackState = {
...NilCallbackState,
...loginState.callbackState
};
const error = qs['error'];
if (error) {
// Possible values for 'error':
// 'invalid_request' -- Something is wrong with the request itself.
// 'access_denied' -- The identity provider denied the request, or user canceled it.
const error_description = qs['error_description'];
pxt.tickEvent('auth.login.error', { 'error': error, 'provider': loginState.idp });
pxt.log(`Auth failed: ${error}:${error_description}`);
// TODO: Is it correct to clear continuation hash?
callbackState = { ...NilCallbackState };
// TODO: Show a message to the user (via rewritten continuation path)?
break;
}
const authToken = qs['token'];
if (!authToken) {
pxt.debug("Missing authToken in auth callback.")
break;
}
// Store csrf token in local storage. It is ok to do this even when
// "Remember me" wasn't selected because this token is not usable
// without its cookie-based counterpart. When "Remember me" is false,
// the cookie is not persisted.
await pxt.storage.shared.setAsync(AUTH_CONTAINER, CSRF_TOKEN_KEY, authToken);
pxt.tickEvent('auth.login.success', { 'provider': loginState.idp });
} while (false);
// Clear url parameters and redirect to the callback location.
const hash = callbackState.hash.startsWith('#') ? callbackState.hash : `#${callbackState.hash}`;
const params = pxt.Util.stringifyQueryString('', callbackState.params);
const pathname = loginState.callbackPathname.startsWith('/') ? loginState.callbackPathname : `/${loginState.callbackPathname}`;
const redirect = `${pathname}${hash}${params}`;
window.location.href = redirect;
}
export function identityProviders(): pxt.AppCloudProvider[] {
return Object.keys(pxt.appTarget?.cloud?.cloudProviders || {})
.map(id => pxt.appTarget.cloud.cloudProviders[id])
.filter(prov => prov.identity)
.sort((a, b) => a.order - b.order);
}
export function identityProvider(id: pxt.IdentityProviderId): pxt.AppCloudProvider {
return identityProviders().filter(prov => prov.id === id).shift();
}
export function hasIdentity(): boolean {
return !authDisabled && !pxt.BrowserUtils.isPxtElectron() && identityProviders().length > 0;
}
function idpEnabled(idp: pxt.IdentityProviderId): boolean {
return identityProviders().filter(prov => prov.id === idp).length > 0;
}
export function enableAuth(enabled = true) {
authDisabled = !enabled;
}
export function userName(user: pxt.auth.UserProfile): string {
return user?.idp?.displayName ?? user?.idp?.username ?? EMPTY_USERNAME;
}
export function userInitials(user: pxt.auth.UserProfile): string {
const username = pxt.auth.userName(user);
return ts.pxtc.Util.initials(username);
}
export function generateUserProfilePicDataUrl(profile: pxt.auth.UserProfile) {
if (profile?.idp?.picture?.encoded) {
const url = window.URL || window.webkitURL;
try {
// Decode the base64 image to a data URL.
const decoded = pxt.Util.stringToUint8Array(atob(profile.idp.picture.encoded));
const blob = new Blob([decoded], { type: profile.idp.picture.mimeType });
profile.idp.picture.dataUrl = url.createObjectURL(blob);
} catch { }
}
}
}