-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
aws-sso.ts
80 lines (72 loc) · 2.6 KB
/
aws-sso.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
import extension from '../extension';
import { AppData, ProfileData, UserData } from '../types';
import { waitForElement } from '../utils';
import api, { RateLimiter, Semaphore } from '../utils/api';
/* collect user, app, and profiles from the AWS SSO directory page */
interface AwsEnvironment {
FDLEnabled: boolean
PKCEEnabled: boolean
allowAllCookiesByDefault: boolean
oidcApiEndpoint: string
panoramaEnabled: true
partition: string
region: string
shortbreadEnabled: boolean
stage: string
}
function getEnvironment(): Promise<AwsEnvironment>{
return waitForElement('#env').then((envEl) => {
return JSON.parse(envEl.textContent!);
});
}
async function getUserData(): Promise<UserData> {
await RateLimiter();
await Semaphore.acquire();
return (api('/user') as unknown as Promise<UserData>).finally(() => { Semaphore.release(); });
}
async function getApps(): Promise<AppData[]> {
await RateLimiter();
await Semaphore.acquire();
return (api('/instance/appinstances').then(
(data) => data.result,
) as Promise<AppData[]>).finally(() => { Semaphore.release(); });
}
async function getAppProfiles(app: AppData): Promise<ProfileData[]> {
await RateLimiter();
await Semaphore.acquire();
return (api(`/instance/appinstance/${app.id}/profiles`).then(
(data) => data.result,
) as Promise<ProfileData[]>).finally(() => { Semaphore.release(); });
}
extension.log(window.location.href);
// delay if sso login is still in progress, need to wait on session token
let delay = window.location.href.includes('state=') ? (extension.config.delay * 3) : 0;
extension.log(`aws-sso:delay:${delay}`);
setTimeout(() => {
getEnvironment().then((env) => {
extension.log('aws-sso:env');
extension.log(env);
// getUserData > getApps > getProfiles > resolve promises > saveData
extension.ssoUrl = `https://portal.sso.${env.region}.amazonaws.com`;
getUserData().then((user) => {
const profiles: Array<Promise<ProfileData | void>> = [];
getApps().then((apps) => {
apps.forEach((app) => {
profiles.push(
getAppProfiles(app).then((appProfiles) => {
const appWithProfiles = app;
appWithProfiles.profiles = appProfiles;
extension.apps.push(appWithProfiles);
}),
);
});
Promise.all(profiles).then(() => {
extension.update(user);
extension.loaded = true;
}).catch((err) => {
throw new Error('Something went terribly wrong and it needs to be handled', { cause: err });
});
});
});
});
}, delay);