-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
314 lines (274 loc) · 11.2 KB
/
index.ts
File metadata and controls
314 lines (274 loc) · 11.2 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
import { NextFunction, Request, Response } from "express";
import url from "url";
import https from "https";
export enum EAssuranceLevel {
NO_ASSURANCE = 0,
LOW = 10,
MEDIUM = 20,
HIGH = 30,
INTERNAL = 40,
TOP = 40
}
export enum ECasVersion {V1 = '1.0', V2 = '2.0', V3 = '3.0'}
enum EAuthType { BOUNCE = 0, BOUNCE_REDIRECT = 1, BLOCK = 2}
export enum EValidateUri {
SERVICEVALIDATE = '/serviceValidate',
STRICTVALIDATE = '/strictValidate',
INTERINSTITUTIONALVALIDATE = '/interinstitutionalValidate',
SPONSORVALIDATE = '/sponsorValidate',
LAXVALIDATE = '/laxValidate',
}
export enum ECasUrls {
ACCEPTANCE = 'https://ecas.acceptance.ec.europa.eu/cas',
PRODUCTION = 'https://ecas.ec.europa.eu/cas'
}
interface IConfig {
casUrl?: string;
validateUri?: EValidateUri;
serviceUrl: string;
logoutRedirectUrl?: string;
casVersion?: string;
sessionName?: string;
sessionInfo?: string;
destroySession?: boolean;
renew?: boolean;
isDevMode?: boolean;
devModeUser?: string;
devModeInfo?: {};
assuranceLevel?: EAssuranceLevel;
}
export class EuLogin {
private static _validateUri: string | undefined;
private static _casHost: string;
private static _casPort: number;
private static _casPath: string;
private static _httpClient: any;
private static _assuranceLevel: number;
private static _devModeInfo: object;
private static _devModeUser: string;
private static _isDevMode: boolean;
private static _renew: boolean;
private static _destroySession: boolean;
private static _sessionInfo: string;
private static _casVersion: string;
private static _sessionName: string;
private static _serviceUrl: string;
private static _casUrl: string;
private static _logoutRedirectUrl: string;
constructor(private config: IConfig
) {
if (config.serviceUrl === undefined || config.serviceUrl.length === 0) {
throw new Error('CAS Authentication requires a serviceUrl parameter.');
}
this.setServiceUrl(config.serviceUrl);
this.setCasUrl((config.casUrl !== undefined && config.casUrl.length > 0) ? config.casUrl : ECasUrls.ACCEPTANCE);
this.setCasVersion((config.casVersion !== undefined && config.casVersion.length > 0) ? config.casVersion : ECasVersion.V3);
this.setSessionName((config.sessionName !== undefined && config.sessionName.length > 0) ? config.sessionName : 'ecas_session');
this.setSessionInfo((config.sessionInfo !== undefined && config.sessionInfo.length > 0) ? config.sessionInfo : 'ecas_session_info');
this.setDestroySession(config.destroySession !== undefined ? config.destroySession : false);
this.setRenew(config.renew !== undefined ? config.renew : false);
this.setIsDevMode(config.isDevMode !== undefined ? config.isDevMode : false);
this.setDevModeUser(config.devModeUser !== undefined ? config.devModeUser : '');
this.setDevModeInfo(config.devModeInfo !== undefined ? config.devModeInfo : {});
this.setAssuranceLevel(config.assuranceLevel !== undefined ? config.assuranceLevel : EAssuranceLevel.TOP);
this.setValidateUri((config.validateUri !== undefined && config.validateUri.length > 0) ? config.validateUri : EValidateUri.SERVICEVALIDATE);
this.setLogoutRedirectUrl((config.logoutRedirectUrl !== undefined && config.logoutRedirectUrl.length > 0) ? config.logoutRedirectUrl : '');
const parsedCasUrl: url.UrlWithStringQuery = url.parse(EuLogin._casUrl);
this.setHttpClient(https);
this.setCasHost(parsedCasUrl.hostname ? parsedCasUrl.hostname : '');
this.setCasPort(parsedCasUrl.protocol ? +parsedCasUrl.protocol : 443);
this.setCasPath(parsedCasUrl.pathname ? parsedCasUrl.pathname : '');
}
private static handle(req: Request, res: Response, next: NextFunction, authType: EAuthType) {
// If the session has been validated with CAS, no action is required.
const reqSession: any = req.session;
if (reqSession[EuLogin._sessionName]) {
// If this is a bounce redirect, redirect the authenticated user.
if (authType === EAuthType.BOUNCE_REDIRECT) {
res.redirect(reqSession.cas_return_to);
}
// Otherwise, allow them through to their request.
else {
next();
}
}
// If dev mode is active, set the CAS user to the specified dev user.
else if (this._isDevMode) {
reqSession[EuLogin._sessionName] = this._devModeUser;
reqSession[EuLogin._sessionInfo] = this._devModeInfo;
next();
}
// If the authentication type is BLOCK, simply send a 401 response.
else if (authType === EAuthType.BLOCK) {
res.sendStatus(401);
}
// If there is a CAS ticket in the query string, validate it with the CAS server.
else if (req.query && req.query.ticket) {
EuLogin.handleTicket(req, res);
}
// Otherwise, redirect the user to the CAS login.
else {
EuLogin.login(req, res);
}
}
private static login(req: Request, res: Response) {
const reqSession: any = req.session;
// Save the return URL in the session. If an explicit return URL is set as a
// query parameter, use that. Otherwise, just use the URL from the request.
reqSession.cas_return_to = req.query.returnTo || url.parse(req.url).path;
// Set up the query parameters.
const queryParams = {
service: this._serviceUrl + url.parse(req.url).pathname,
renew: this._renew
};
// Redirect to the CAS login.
res.redirect(this._casUrl + url.format({
pathname: '/login',
query: queryParams
}));
}
private static handleTicket(req: Request, res: Response) {
const requestOptions: any = {
host: this._casHost,
port: this._casPort
}
const reqQuery: any = req.query;
if (['3.0'].indexOf(this._casVersion) >= 0) {
requestOptions.method = 'GET';
requestOptions.path = url.format({
pathname: this._casPath + this._validateUri,
query: {
service: this._serviceUrl + url.parse(req.url).pathname,
ticket: reqQuery.ticket,
userDetails: true,
format: 'JSON',
assuranceLevel: this._assuranceLevel
}
});
}
const request = this._httpClient.request(requestOptions, (response: any) => {
response.setEncoding('utf8');
let body = '';
response.on('data', (chunk: any) => {
return body += chunk;
});
response.on('end', () => {
const bodyResponse: any = JSON.parse(body);
const failure = bodyResponse.serviceResponse.authenticationFailure;
if (failure) {
return res.status(401).json({ "message": 'CAS authentication failed (' + failure.code + ').' });
}
const success: any = bodyResponse.serviceResponse.authenticationSuccess;
if (success) {
// @ts-ignore
req.session[EuLogin._sessionName] = {
userId: success.user,
domain: success.domain,
departmentNumber: success.departmentNumber ? success.departmentNumber : '',
orgId: success.orgId ? success.orgId : '',
email: success.email,
firstName: success.firstName,
lastName: success.lastName,
loginDate: success.loginDate,
};
// @ts-ignore
res.redirect(req.session.cas_return_to);
}
response.on('error', (error: any) => {
return res.status(401).json({ "message": "Response error from CAS" });
});
});
});
request.on('error', (error: any) => {
res.status(401).json({ "message": "Request error with CAS" });
});
request.end();
}
logout(req: Request, res: Response) {
// Destroy the entire session if the option is set.
if (EuLogin._destroySession) {
req.session.destroy((err) => {
if (err) {
return res.status(401).json({ "message": "Request error with CAS" });
}
});
}
// Otherwise, just destroy the CAS session variables.
else {
// @ts-ignore
delete req.session[EuLogin._sessionName];
if (EuLogin._sessionInfo) {
// @ts-ignore
delete req.session[EuLogin._sessionInfo];
}
}
if (EuLogin._logoutRedirectUrl) {
return res.redirect(EuLogin._logoutRedirectUrl);
}
// Redirect the client to the CAS logout.
res.redirect(EuLogin._casUrl + '/logout');
};
setServiceUrl(serviceUrl: string) {
EuLogin._serviceUrl = serviceUrl;
}
setCasUrl(casUrl: string) {
EuLogin._casUrl = casUrl;
}
setValidateUri(validateUri: string) {
EuLogin._validateUri = validateUri;
}
setCasVersion(casVersion: string) {
EuLogin._casVersion = casVersion;
}
setSessionName(sessionName: string) {
EuLogin._sessionName = sessionName;
}
setSessionInfo(sessionInfo: string) {
EuLogin._sessionInfo = sessionInfo;
}
setDestroySession(destroySession: boolean) {
EuLogin._destroySession = destroySession;
}
setRenew(renew: boolean) {
EuLogin._renew = renew;
}
setIsDevMode(isDevMode: boolean) {
EuLogin._isDevMode = isDevMode;
}
setDevModeUser(devModeUser: string) {
EuLogin._devModeUser = devModeUser;
}
setDevModeInfo(devModeInfo: object) {
EuLogin._devModeInfo = devModeInfo;
}
setAssuranceLevel(assuranceLevel: number) {
EuLogin._assuranceLevel = assuranceLevel;
}
setHttpClient(httpClient: typeof import("https")) {
EuLogin._httpClient = httpClient;
}
setCasHost(casHost: string) {
EuLogin._casHost = casHost;
}
setCasPort(casPort: number) {
EuLogin._casPort = casPort;
}
setCasPath(casPath: string) {
EuLogin._casPath = casPath;
}
setLogoutRedirectUrl(logoutRedirectUrl: string) {
EuLogin._logoutRedirectUrl = logoutRedirectUrl;
}
bounce(req: Request, res: Response, next: NextFunction) {
// Handle the request with the bounce authorization type.
EuLogin.handle(req, res, next, EAuthType.BOUNCE);
}
bounce_redirect(req: Request, res: Response, next: NextFunction) {
// Handle the request with the bounce_redirect authorization type.
EuLogin.handle(req, res, next, EAuthType.BOUNCE_REDIRECT);
}
block(req: Request, res: Response, next: NextFunction) {
// Handle the request with the block authorization type.
EuLogin.handle(req, res, next, EAuthType.BLOCK);
}
}