-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauthenticationService.ts
More file actions
248 lines (209 loc) · 9.33 KB
/
authenticationService.ts
File metadata and controls
248 lines (209 loc) · 9.33 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
import * as http from 'http';
import * as path from 'path';
import * as fs from 'fs';
import { env, ProgressLocation, Uri, window } from 'vscode';
import { v4 as uuid } from 'uuid';
import * as crypto from 'crypto';
import { Token } from '../types/token';
import { UserClaims } from '../types/userClaims';
import { promiseFromEvent } from '../utils';
const CLIENT_ID = `1a5764a8090f136cc9d30f381626d5fa`;
const CALLBACK_HOSTNAME = '127.0.0.1';
const CALLBACK_PORT = 8000;
const CALLBACK_URI = `http://${CALLBACK_HOSTNAME}:${CALLBACK_PORT}/callback`;
export interface IAuthenticationService {
login(environment: string): Promise<Token>;
getClaimsFromToken(token: string): UserClaims;
generateAuthorizeUrl(environment: string): Promise<Uri>;
}
export class AuthenticationService implements IAuthenticationService {
private server: http.Server | null = null;
private _pendingStates: string[] = [];
private _codeVerifiers = new Map<string, string>();
private _scopes = new Map<string, string[]>();
private _environment: string = "";
private _state: string = "";
/**
* Load HTML template from resources folder
*/
private loadHtmlTemplate(filename: string): string {
try {
const resourcesPath = path.join(__dirname, 'resources', filename);
return fs.readFileSync(resourcesPath, 'utf8');
} catch (error) {
console.error(`Failed to load HTML template: ${filename}`, error);
// Fallback to simple HTML
if (filename.includes('success')) {
return `
<html>
<body style="text-align: center; font-family: system-ui; padding: 50px;">
<h1 style="color: #28a745;">Authentication Successful!</h1>
<p>You can now close this tab and return to Visual Studio Code.</p>
</body>
</html>
`;
} else {
return `
<html>
<body style="text-align: center; font-family: system-ui; padding: 50px;">
<h1 style="color: #dc3545;">Authentication Failed</h1>
<p>Please try again.</p>
</body>
</html>
`;
}
}
}
/**
* Generate AuthorizeUrl with scopes, parameters and state etc.
*/
public async generateAuthorizeUrl(environment: string): Promise<Uri> {
const nonceId = uuid();
const scopes = ['openid']
const codeVerifier = this.toBase64UrlEncoding(crypto.randomBytes(32));
const codeChallenge = this.toBase64UrlEncoding(this.sha256(codeVerifier));
const callbackUri = await env.asExternalUri(Uri.parse(CALLBACK_URI));
const callbackQuery = new URLSearchParams(callbackUri.query);
const state = callbackQuery.get('state') || nonceId;
this._environment = environment;
this._state = state;
this._pendingStates.push(state);
this._codeVerifiers.set(state, codeVerifier);
this._scopes.set(state, scopes);
const searchParams = new URLSearchParams([
['response_type', "code"],
['client_id', CLIENT_ID],
['redirect_uri', CALLBACK_URI],
['state', state],
['scope', scopes.join(' ')],
['code_challenge_method', 'S256'],
['code_challenge', codeChallenge],
]);
const uri = Uri.parse(`https://${environment}.superoffice.com/login/common/oauth/authorize?${searchParams.toString()}`);
return Promise.resolve(uri);
}
/**
* Log in to OpenId Connect
*/
public async login(environment: string): Promise<Token> {
return await window.withProgress<Token>({
location: ProgressLocation.Notification,
title: "Signing in to SuperOffice...",
cancellable: true,
}, async (_, token) => {
const uri = await this.generateAuthorizeUrl(environment);
await env.openExternal(uri);
try {
return await Promise.race([
await this.callback(60000),
new Promise<string>((_, reject) => setTimeout(() => reject('Cancelled'), 60000)),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
promiseFromEvent<any, any>(token.onCancellationRequested, (_, __, reject) => { reject('User Cancelled'); }).promise
])
}
finally {
this._pendingStates = this._pendingStates.filter(n => n !== this._state);
}
});
}
private async callback(timeout: number): Promise<Token> {
return new Promise<Token>((resolve, reject) => {
if (this.server) return reject(new Error('Server already started'));
this.server = http.createServer(async (req, res) => {
try {
if (!req.url) {
throw new Error('Request URL not provided during authentication callback.');
}
const url = new URL(req.url, `http://${CALLBACK_HOSTNAME}:${CALLBACK_PORT}`);
if (url.pathname !== '/callback') {
throw new Error('Invalid callback path.');
}
const query = url.searchParams;
const code = query.get('code');
const stateId = query.get('state');
if (!code) {
reject(new Error('Callback does not contain a code.'));
return;
}
if (!stateId) {
reject(new Error('Callback does not contain a state.'));
return;
}
const codeVerifier = this._codeVerifiers.get(stateId);
if (!codeVerifier) {
reject(new Error('No code verifier'));
return;
}
// Check if it is a valid auth request started by the extension
if (!this._pendingStates.some(n => n === stateId)) {
reject(new Error('State not found'));
return;
}
const body = `client_id=${CLIENT_ID}&code=${code}&grant_type=authorization_code&redirect_uri=${CALLBACK_URI}&code_verifier=${codeVerifier}`;
const response = await fetch(`https://${this._environment}.superoffice.com/login/common/oauth/tokens`, {
method: 'POST',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json"
},
body
});
const tokenInformation = await response.json() as Token;
// Send a confirmation page to the browser
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(this.loadHtmlTemplate('auth-success.html'));
// res.end(`
// <html>
// <body>
// <h1>Authentication Successful!</h1>
// <p>You can close this tab and return to Visual Studio Code.</p>
// </body>
// </html>
// `);
resolve(tokenInformation);
} catch (error) {
// Send error feedback to the browser if possible
res.writeHead(500, { 'Content-Type': 'text/html' });
const errorHtml = this.loadHtmlTemplate('auth-error.html')
.replace('{{ERROR_MESSAGE}}', (error as Error).message);
res.end(errorHtml);
reject(error);
} finally {
this.closeServer();
}
});
this.server.on('error', (error) => {
reject(new Error(`Server error: ${error.message}`));
this.closeServer();
});
this.server.listen(CALLBACK_PORT, CALLBACK_HOSTNAME, () => {
console.log(`Server listening on ${CALLBACK_HOSTNAME}:${CALLBACK_PORT}`);
});
// Timeout to reject the promise if no callback is received within the specified time
setTimeout(() => {
reject(new Error('Authorization timed out'));
this.closeServer();
}, timeout);
});
}
private closeServer(): void {
if (this.server) {
this.server.close();
this.server = null;
}
}
private toBase64UrlEncoding(buffer: Buffer): string {
return buffer.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}
private sha256(buffer: string | Uint8Array): Buffer {
return crypto.createHash('sha256').update(buffer).digest();
}
public getClaimsFromToken(token: string): UserClaims {
const arrayToken = token.split('.');
const userClaims = JSON.parse(atob(arrayToken[1])) as UserClaims;
return userClaims;
}
}