-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
aws-sso-oidc.service.ts
273 lines (228 loc) · 10.3 KB
/
aws-sso-oidc.service.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
267
268
269
270
271
272
273
import {
GenerateSSOTokenResponse,
RegisterClientResponse,
StartDeviceAuthorizationResponse, VerificationResponse
} from './session/aws/methods/aws-sso-role.service';
import SSOOIDC, {
CreateTokenRequest,
RegisterClientRequest,
StartDeviceAuthorizationRequest
} from 'aws-sdk/clients/ssooidc';
import {Injectable} from '@angular/core';
import {Constants} from '../models/constants';
import {WorkspaceService} from './workspace.service';
import {AppService, LoggerLevel} from './app.service';
import {ElectronService} from './electron.service';
import {LeappBaseError} from '../errors/leapp-base-error';
export interface BrowserWindowClosing {
catchClosingBrowserWindow(): void;
}
@Injectable({
providedIn: 'root'
})
export class AwsSsoOidcService {
listeners: BrowserWindowClosing[];
private ssoOidc: SSOOIDC;
private ssoWindow: any;
private currentRegion: string;
private currentPortalUrl: string;
private registerClientResponse: RegisterClientResponse;
private startDeviceAuthorizationResponse: StartDeviceAuthorizationResponse;
private startDeviceAuthorizationResponseExpiresAt: number;
private generateSSOTokenResponse: GenerateSSOTokenResponse;
private setIntervalQueue: Array<any>;
private loginMutex: boolean;
private timeoutOccurred: boolean;
private interruptOccurred: boolean;
private mainIntervalId: any;
private index: number;
constructor(
private appService: AppService,
private electronService: ElectronService,
private workspaceService: WorkspaceService
) {
this.listeners = [];
this.ssoOidc = null;
this.ssoWindow = null;
this.currentRegion = null;
this.currentPortalUrl = null;
this.registerClientResponse = null;
this.startDeviceAuthorizationResponse = null;
this.startDeviceAuthorizationResponseExpiresAt = null;
this.generateSSOTokenResponse = null;
this.setIntervalQueue = [];
this.loginMutex = false;
this.timeoutOccurred = false;
this.interruptOccurred = false;
this.index = 0;
}
getAwsSsoOidcClient(): SSOOIDC {
return this.ssoOidc;
}
interrupt() {
clearInterval(this.mainIntervalId);
this.interruptOccurred = true;
this.loginMutex = false;
}
async login(region: string, portalUrl: string): Promise<GenerateSSOTokenResponse> {
if (!this.loginMutex && this.setIntervalQueue.length === 0) {
this.loginMutex = true;
this.ssoOidc = new SSOOIDC({ region });
this.ssoWindow = null;
this.currentRegion = region;
this.currentPortalUrl = portalUrl;
this.registerClientResponse = null;
this.startDeviceAuthorizationResponse = null;
this.startDeviceAuthorizationResponseExpiresAt = null;
this.generateSSOTokenResponse = null;
this.setIntervalQueue = [];
this.timeoutOccurred = false;
this.interruptOccurred = false;
await this.registerSsoOidcClient();
const startDeviceAuthorizationRequest: StartDeviceAuthorizationRequest = {
clientId: this.registerClientResponse.clientId,
clientSecret: this.registerClientResponse.clientSecret,
startUrl: portalUrl
};
const baseTimeInMilliseconds = Date.now();
this.startDeviceAuthorizationResponse = await this.getAwsSsoOidcClient().startDeviceAuthorization(startDeviceAuthorizationRequest).promise();
this.startDeviceAuthorizationResponseExpiresAt = baseTimeInMilliseconds + this.startDeviceAuthorizationResponse.expiresIn * 1000;
const verificationResponse = await this.openVerificationBrowserWindow(this.registerClientResponse, this.startDeviceAuthorizationResponse);
try {
this.generateSSOTokenResponse = await this.createToken(verificationResponse);
} catch (err) {
this.loginMutex = false;
throw(err);
}
this.loginMutex = false;
return this.generateSSOTokenResponse;
} else if (!this.loginMutex && this.setIntervalQueue.length > 0) {
return this.generateSSOTokenResponse;
} else {
return new Promise((resolve, reject) => {
const repeatEvery = 500; // 0.5 second, we can make these more speedy as they just check a variable, no external calls here
const resolved = setInterval(async () => {
if(this.interruptOccurred) {
clearInterval(resolved);
const resolvedIndex = this.setIntervalQueue.indexOf(resolved);
this.setIntervalQueue.splice(resolvedIndex, 1);
reject(new LeappBaseError('AWS SSO Interrupted', this, LoggerLevel.info, 'AWS SSO Interrupted.'));
} else if (this.generateSSOTokenResponse) {
clearInterval(resolved);
const resolvedIndex = this.setIntervalQueue.indexOf(resolved);
this.setIntervalQueue.splice(resolvedIndex, 1);
resolve(this.generateSSOTokenResponse);
} else if (this.timeoutOccurred) {
clearInterval(resolved);
const resolvedIndex = this.setIntervalQueue.indexOf(resolved);
this.setIntervalQueue.splice(resolvedIndex, 1);
reject(new LeappBaseError('AWS SSO Timeout', this, LoggerLevel.error, 'AWS SSO Timeout occurred. Please redo login procedure.'));
}
}, repeatEvery);
this.setIntervalQueue.push(resolved);
});
}
}
private async registerSsoOidcClient(): Promise<void> {
const registerClientRequest: RegisterClientRequest = { clientName: 'leapp', clientType: 'public' };
this.registerClientResponse = await this.ssoOidc.registerClient(registerClientRequest).promise();
}
private async openVerificationBrowserWindow(registerClientResponse: RegisterClientResponse, startDeviceAuthorizationResponse: StartDeviceAuthorizationResponse): Promise<VerificationResponse> {
if(this.workspaceService.getAwsSsoConfiguration().browserOpening === Constants.inApp.toString()) {
const pos = this.electronService.currentWindow.getPosition();
this.ssoWindow = null;
this.ssoWindow = this.appService.newWindow(startDeviceAuthorizationResponse.verificationUriComplete, true, 'Portal url - Client verification', pos[0] + 200, pos[1] + 50);
this.ssoWindow.loadURL(startDeviceAuthorizationResponse.verificationUriComplete);
this.ssoWindow.on('close', (e) => {
e.preventDefault();
this.loginMutex = false;
this.listeners.forEach(listener => {
listener.catchClosingBrowserWindow();
});
});
return new Promise( (resolve, reject) => {
// When the code is verified and the user has been logged in, the window can be closed
this.ssoWindow.webContents.session.webRequest.onBeforeRequest({ urls: [
'https://*.awsapps.com/start/user-consent/login-success.html',
] }, (details, callback) => {
this.ssoWindow.close();
this.ssoWindow = null;
const verificationResponse: VerificationResponse = {
clientId: registerClientResponse.clientId,
clientSecret: registerClientResponse.clientSecret,
deviceCode: startDeviceAuthorizationResponse.deviceCode
};
resolve(verificationResponse);
callback({
requestHeaders: details.requestHeaders,
url: details.url,
});
});
this.ssoWindow.webContents.session.webRequest.onErrorOccurred((details) => {
if (
details.error.indexOf('net::ERR_ABORTED') < 0 &&
details.error.indexOf('net::ERR_FAILED') < 0 &&
details.error.indexOf('net::ERR_CACHE_MISS') < 0 &&
details.error.indexOf('net::ERR_CONNECTION_REFUSED') < 0
) {
if (this.ssoWindow) {
this.ssoWindow.close();
this.ssoWindow = null;
}
reject(details.error.toString());
}
});
});
} else {
return this.openExternalVerificationBrowserWindow(registerClientResponse, startDeviceAuthorizationResponse);
}
}
private async openExternalVerificationBrowserWindow(registerClientResponse: RegisterClientResponse, startDeviceAuthorizationResponse: StartDeviceAuthorizationResponse): Promise<VerificationResponse> {
const uriComplete = startDeviceAuthorizationResponse.verificationUriComplete;
return new Promise( (resolve, _) => {
// Open external browser window and let authentication begins
this.appService.openExternalUrl(uriComplete);
// Return the code to be used after
const verificationResponse: VerificationResponse = {
clientId: registerClientResponse.clientId,
clientSecret: registerClientResponse.clientSecret,
deviceCode: startDeviceAuthorizationResponse.deviceCode
};
resolve(verificationResponse);
});
}
private async createToken(verificationResponse: VerificationResponse): Promise<GenerateSSOTokenResponse> {
const createTokenRequest: CreateTokenRequest = {
clientId: verificationResponse.clientId,
clientSecret: verificationResponse.clientSecret,
grantType: 'urn:ietf:params:oauth:grant-type:device_code',
deviceCode: verificationResponse.deviceCode
};
let createTokenResponse;
if(this.workspaceService.getAwsSsoConfiguration().browserOpening === Constants.inApp) {
createTokenResponse = await this.getAwsSsoOidcClient().createToken(createTokenRequest).promise();
} else {
createTokenResponse = await this.waitForToken(createTokenRequest);
}
const expirationTime: Date = new Date(Date.now() + createTokenResponse.expiresIn * 1000);
return { accessToken: createTokenResponse.accessToken, expirationTime };
}
private async waitForToken(createTokenRequest: CreateTokenRequest): Promise<any> {
return new Promise((resolve, reject) => {
const intervalInMilliseconds = 5000;
this.mainIntervalId = setInterval(() => {
this.getAwsSsoOidcClient().createToken(createTokenRequest).promise().then(createTokenResponse => {
clearInterval(this.mainIntervalId);
resolve(createTokenResponse);
}).catch(err => {
if(err.toString().indexOf('AuthorizationPendingException') === -1) {
// AWS SSO Timeout occurred
clearInterval(this.mainIntervalId);
this.timeoutOccurred = true;
reject(new LeappBaseError('AWS SSO Timeout', this, LoggerLevel.error, 'AWS SSO Timeout occurred. Please redo login procedure.'));
}
});
}, intervalInMilliseconds);
});
}
}