-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathclient.ts
More file actions
92 lines (81 loc) · 3.08 KB
/
client.ts
File metadata and controls
92 lines (81 loc) · 3.08 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
import { Disposable } from 'vs/base/common/lifecycle';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { MenuId, MenuRegistry } from "vs/platform/actions/common/actions";
import { localize } from "vs/nls";
import { ILogService } from "vs/platform/log/common/log";
export class SagemakerServerClient extends Disposable {
constructor (
@ILogService private logService: ILogService
) {
super();
this.logService.debug('Initializing SagemakerServerClient...');
this.registerSagemakerCommands();
}
static LOGOUT_COMMAND_ID = 'sagemaker.logout';
static COOKIE_COMMAND_ID = 'sagemaker.parseCookies';
static COOKIE_LOAD_COMMAND_ID = 'sagemaker.loadCookies';
private registerSagemakerCommands() {
const authMode: string | undefined = this.getCookieValue('authMode');
const expiryTime: string | undefined = this.getCookieValue('expiryTime');
const studioUserProfileName: string | undefined = this.getCookieValue('studioUserProfileName')
const ssoExpiryTimestamp: string | undefined = this.getCookieValue('ssoExpiryTimestamp')
const redirectURL: string | undefined = this.getCookieValue('redirectURL')
const cookieEntries = [
'authMode',
'expiryTime',
'studioUserProfileName',
'ssoExpiryTimestamp',
'redirectURL',
'AccessToken',
'StudioSessionToken',
];
this.logService.debug('Registering sagemaker commands...');
CommandsRegistry.registerCommand(SagemakerServerClient.COOKIE_COMMAND_ID, () => {
return {
authMode: authMode,
expiryTime: expiryTime,
ssoExpiryTimestamp: ssoExpiryTimestamp,
studioUserProfileName: studioUserProfileName,
redirectURL: redirectURL
};
});
CommandsRegistry.registerCommand(
SagemakerServerClient.COOKIE_LOAD_COMMAND_ID,
async () => {
try {
const entriesQueryParam = cookieEntries
.map((entry) => `entry=${entry}`)
.join("&");
const urlParams = new URLSearchParams(window.location.search);
const workspaceFolder = urlParams.get('folder');
if (workspaceFolder) {
const response = await fetch(`/load-cookies?workspace=${encodeURIComponent(workspaceFolder)}&${entriesQueryParam}`);
return await response.text();
}
return '';
} catch (error) {
return '';
}
}
);
CommandsRegistry.registerCommand(SagemakerServerClient.LOGOUT_COMMAND_ID, () => {
const currentUrl = new URL(window.location.href);
const hostname = currentUrl.hostname;
const pathComponents = currentUrl.pathname.split('/');
const logoutUrl = `https://${hostname}/${pathComponents[1]}/${pathComponents[2]}/logout`;
window.location.href = logoutUrl;
});
for (const menuId of [MenuId.CommandPalette, MenuId.MenubarHomeMenu]) {
MenuRegistry.appendMenuItem(menuId, {
command: {
id: SagemakerServerClient.LOGOUT_COMMAND_ID,
title: localize('logout', "{0}: Log out", 'Sagemaker'),
},
});
}
}
private getCookieValue(name: string): string | undefined {
const match = document.cookie.match('(^|[^;]+)\\s*' + name + '\\s*=\\s*([^;]+)'); // See https://stackoverflow.com/a/25490531
return match ? match.pop() : undefined;
}
}