-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
563 lines (502 loc) · 15.8 KB
/
index.js
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/**
* Central Authentication Service (CAS) client for express
*
* The Central Authentication Service (CAS) is a single-sign-on / single-sign-off protocol for the web.
* It permits a user to access multiple applications while providing their
* credentials (such as userid and password) only once to a central CAS Server application.
*
* @see https://apereo.github.io/cas/5.3.x/protocol/CAS-Protocol-Specification.html
*
* @author Yang,junlong at 2018-11-25 14:00:59 build.
* @version $Id$
*/
var https = require('https');
var http = require('http');
var path = require('path');
var url = require('url');
var parseXML = require('xml2js').parseString;
var XMLprocessors = require('xml2js/lib/processors');
var querystring = require('querystring');
// credential requestor / acceptor
const LOGIN_URI = '/login';
// destroy CAS session (logout)
const LOGOUT_URI = '/logout';
// service ticket validation
const VALIDATE_URI = '/validate';
// service ticket validation [CAS 2.0]
const SERVICE_VALIDATE_URI = '/serviceValidate';
// service/proxy ticket validation [CAS 2.0]
const PROXY_VALIDATE_URI = '/proxyValidate';
// proxy ticket service [CAS 2.0]
const PROXY_URI = '/proxy';
// service ticket validation [CAS 3.0]
const P3_SERVICE_VALIDATE_URI = '/p3/serviceValidate';
// service/proxy ticket validation [CAS 3.0]
const P3_PROXY_VALIDATE_URI = '/p3/proxyValidate'
// Secure Access Markup Language
const SAML_VALIDATE_URI = '/samlValidate';
/**
* Node Cas Client
*
* @see https://github.com/apereo/java-cas-client
*
* @param {Object} options
*/
function CASClient(options) {
/**
* The start of the CAS server URL,
* i.e. https://localhost:8443/cas
*
* @type {String}
*/
this.casServerUrlPrefix = '';
/**
* Defines the location of the CAS server login URL,
* i.e. https://localhost:8443/cas/login.
* This overrides casServerUrlPrefix, if set.
*
* @type {String}
*/
this.casServerLoginUrl = '';
/**
* The name of the server this application is hosted on.
* Service URL will be dynamically constructed using this,
* i.e. https://localhost:8443
* (you must include the protocol, but port is optional if it's a standard port).
*
* @type {String}
*/
this.serverName = '';
/**
* The service URL to send to the CAS server,
* i.e. https://localhost:8443/yourwebapp/index.html
*
* @type {String}
*/
this.service = '';
/**
* specifies whether renew=true should be sent to the CAS server.
* Valid values are either true/false (or no value at all).
*
* @type {Boolean}
*/
this.renew = false;
/**
* specifies whether gateway=true should be sent to the CAS server.
* Valid values are either true/false (or no value at all)
*
* @type {Boolean}
*/
this.gateway = false;
/**
* Whether the client should auto encode the service url.
* Defaults to true
*
* @type {Boolean}
*/
this.encodeServiceUrl = true;
/**
* cas session user name
*
* @type {String}
*/
this.sessionName = 'cas_username';
/**
* cas session info name
*
* @type {String}
*/
this.sessionInfo = 'cas_userinfo';
/**
* If dev mode is active,
* set the CAS user to the specified dev user.
*
* @type {String}
*/
this.devModeUser = '';
/**
* If dev mode is active,
* set the CAS Info to the specified dev info.
*
* @type {String}
*/
this.devModeInfo = {};
/**
* Destroy the entire session if the option is set.
*
* @type {Boolean}
*/
this.isDestroySession = false;
/**
* The CAS Server Supported version
*
* i.e. '1.0', 2.0', '3.0', 'saml1.1'
*
* @type {String}
*/
this.version = '3.0';
/**
* Supported values are XML and JSON
* Default value: JSON
*
* @type {String}
*/
// this.format = 'JSON';
Object.assign(this, options);
if(!(this.casServerUrlPrefix || this.casServerLoginUrl)) {
throw new Error( 'The parameters casServerUrlPrefix and casServerLoginUrl need to be set at least one');
}
if(!this.serverName) {
throw new Error( 'Requires a serverName parameter.');
}
this.casServerUrlPrefix = this.casServerUrlPrefix || path.dirname(this.casServerLoginUrl);
this.casServerLoginUri = this.casServerLoginUrl ? path.basename(this.casServerLoginUrl) : LOGIN_URI;
var casServerParts = url.parse(this.casServerUrlPrefix);
// cas server protocal
this.casServerProtocol = casServerParts.protocol;
this.casServerHost = casServerParts.host;
this.casServerPort = casServerParts.port;
this.casServerPath = casServerParts.path;
// default cas server validate uri
this.casServerValidateUri = P3_SERVICE_VALIDATE_URI;
this.request = this.casServerProtocol == 'http' ? http.request : https.request;
// format 2.0, 3.0 validate response
this._formatValidateResponse = function(body, callback) {
parseXML(body, {
trim: true,
normalize: true,
explicitArray: false,
tagNameProcessors: [XMLprocessors.stripPrefix],
valueProcessors: [function(value) {
return decodeURIComponent(value);
}]
}, function(error, result) {
if (error) {
return callback(new Error('Response from CAS server was bad.'));
}
try {
var failure = result.serviceResponse.authenticationFailure;
if (failure) {
return callback(new Error('CAS authentication failed (' + failure.$.code + ').'));
}
var success = result.serviceResponse.authenticationSuccess;
if (success) {
return callback(null, success.user, success.attributes);
} else {
return callback(new Error( 'CAS authentication failed.'));
}
} catch (err) {
console.log(err);
return callback(new Error('CAS authentication failed.'));
}
});
};
switch(this.version) {
case '1.0' :
this.casServerValidateUri = VALIDATE_URI;
this._formatValidateResponse = function(body, callback) {
var lines = body.split('\n');
if(lines[ 0 ] === 'yes' && lines.length >= 2) {
// success
return callback(null, lines[1]);
} else if (lines[0] === 'no') {
// failed
return callback(new Error('CAS authentication failed.'));
} else {
return callback(new Error('Response from CAS server was bad.'));
}
};
break;
case '2.0' :
this.casServerValidateUri = SERVICE_VALIDATE_URI;
break;
case '3.0' :
// todo nothing
break;
case 'saml1.1' :
this.casServerValidateUri = SAML_VALIDATE_URI;
this._formatValidateResponse = function(body, callback) {
parseXML(body, {
trim: true,
normalize: true,
explicitArray: false,
tagNameProcessors: [XMLprocessors.normalize, XMLprocessors.stripPrefix],
valueProcessors: [function(value) {
return decodeURIComponent(value);
}]
}, function(error, result) {
if (error) {
return callback(new Error('Response from CAS server was bad.'));
}
try {
var samlResponse = result.envelope.body.response;
var success = samlResponse.status.statuscode.$.Value.split(':')[ 1 ];
if (success !== 'Success') {
return callback(new Error('CAS authentication failed (' + success + ').'));
} else {
var attributes = {};
var attributesArray = samlResponse.assertion.attributestatement.attribute;
if (!(attributesArray instanceof Array)) {
attributesArray = [ attributesArray ];
}
attributesArray.forEach(function(attr){
var thisAttrValue;
if (attr.attributevalue instanceof Array){
thisAttrValue = [];
attr.attributevalue.forEach(function(v) {
thisAttrValue.push(v._);
});
} else {
thisAttrValue = attr.attributevalue._;
}
attributes[ attr.$.AttributeName ] = thisAttrValue;
});
return callback(null, samlResponse.assertion.authenticationstatement.subject.nameidentifier, attributes);
}
} catch (err) {
console.log(err);
return callback(new Error('CAS authentication failed.'));
}
});
};
break;
default: // defaults 3.0 version
}
this.check = this.check.bind(this);
this.block = this.block.bind(this);
this.bounce = this.bounce.bind(this);
this.logout = this.logout.bind(this);
this.redirect = this.redirect.bind(this);
}
/**
* Bounces a request with CAS authentication.
* If the user's session is not already validated with CAS,
* their request will be redirected to the CAS login page.
*
* @param {Object} req
* @param {Object} res
* @param {Function} next
* @return {Undefined}
*/
CASClient.prototype.bounce = function(req, res, next) {
this._handle(req, res, next, 1);
};
/**
* Redirect a request with CAS authentication.
* If the user's session is not already validated with CAS,
* their request will be redirected to the CAS login page.
*
* @param {[type]} req
* @param {[type]} res
* @param {Function} next
* @return {[type]}
*/
CASClient.prototype.redirect = function(req, res, next) {
this._handle(req, res, next, 2);
};
/**
* Blocks a request with CAS authentication.
* If the user's session is not already validated with CAS,
* they will receive a 401 response.
*
* @param {[type]} req
* @param {[type]} res
* @param {Function} next
* @return {}
*/
CASClient.prototype.block = function(req, res, next) {
this._handle(req, res, next, 3);
};
/**
* Checks a request with CAS authentication.
* If the user's session is not already validated with CAS,
* return false, otherwise return cas username
*
* @param {[type]} req
* @param {[type]} res
* @param {Function} next
* @return {}
*/
CASClient.prototype.check = function(req, res, next) {
return req.session[ this.sessionName ];
};
/**
* Logout the currently logged in CAS user.
*
* @param {Object} req
* @param {Object} res
* @param {Function} next
* @return {}
*/
CASClient.prototype.logout = function(req, res, next) {
var query = {
service: this.service || url.resolve(this.serverName, url.parse(req.query.service || '/').pathname),
};
if(this.isDestroySession) {
req.session.destroy(function(err) {
if(err) {
console.log(err);
}
});
} else { // Otherwise, just destroy the CAS session variables.
req.session && (delete req.session[ this.sessionName ]);
if (this.sessionInfo && req.session) {
delete req.session[ this.sessionInfo ];
}
}
// Redirect the client to the CAS logout.
res.redirect(url.format({
protocol: this.casServerProtocol,
hostname: this.casServerHost,
pathname: url.resolve(this.casServerPath, LOGOUT_URI),
query: query
}));
}
CASClient.prototype._handle = function(req, res, next, authType) {
// If the session has been validated with CAS, no action is required.
if(this.check(req, res, next)) {
// If this is a bounce redirect, redirect the authenticated user.
if (authType === 2) {
res.redirect(req.session.cas_referer);
} else {
// Otherwise, allow them through to their request.
next();
}
} else if(this.devModeUser) {
req.session[ this.sessionName ] = this.devModeUser;
req.session[ this.sessionInfo ] = this.devModeInfo;
next();
} else if (authType === 3) {
// If the authentication type is BLOCK, simply send a 401 response.
res.sendStatus(401);
} else if (req.query && req.query.ticket) {
// If there is a CAS ticket in the query string, validate it with the CAS server.
this._validate(req, res, next);
} else {
// Otherwise, redirect the user to the CAS login.
this._login(req, res, next);
}
};
/**
* Redirects the client to the CAS login page.
*
* @param {Object} req
* @param {Object} res
* @param {Function} next
* @return {}
*/
CASClient.prototype._login = function(req, res, next) {
// 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.
req.session.cas_referer = req.query.referer || url.parse(req._parsedOriginalUrl || req.url).path;
// Set up the query parameters.
var query = {
service: this.service || url.resolve(this.serverName, url.parse(req._parsedOriginalUrl || req.url).pathname),
renew: this.renew
};
if(this.encodeServiceUrl) {
query.service = encodeURI(query.service);
}
res.redirect(url.format({
protocol: this.casServerProtocol,
hostname: this.casServerHost,
pathname: url.resolve(this.casServerPath, LOGIN_URI),
query: query
}));
}
/**
* Checks the validity of a service ticket
* and returns an XML/JSON-fragment response
*
* @param {Object} req
* @param {Object} res
* @param {Function} next
* @return {[type]}
*/
CASClient.prototype._validate = function(req, res, next) {
var query = {
service: this.service || url.resolve(this.serverName, url.parse(req._parsedOriginalUrl || req.url).pathname),
ticket: req.query.ticket
};
var requestOptions = {
host: this.casServerHost,
port: this.casServerPort
};
requestOptions.path = url.format({
pathname: url.resolve(this.casServerPath, this.casServerValidateUri),
query: query
});
var postData = querystring.stringify(query);
switch(this.version) {
case '1.0' :
break;
case '2.0' :
break;
case 'saml1.1' :
var now = new Date();
postData = [
'<?xml version="1.0" encoding="utf-8"?>',
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">',
' <SOAP-ENV:Header/>',
' <SOAP-ENV:Body>',
' <samlp:Request xmlns:samlp="urn:oasis:names:tc:SAML:1.0:protocol" MajorVersion="1"',
' MinorVersion="1" RequestID="_' + req.host + '.' + now.getTime() + '"',
' IssueInstant="' + now.toISOString() + '">',
' <samlp:AssertionArtifact>',
' ' + req.query.ticket,
' </samlp:AssertionArtifact>',
' </samlp:Request>',
' </SOAP-ENV:Body>',
'</SOAP-ENV:Envelope>'
].join('\n');
requestOptions.method = 'POST';
requestOptions.path = url.format({
pathname: url.resolve(this.casServerPath, this.casServerValidateUri),
query: {
TARGET : query.service,
ticket: ''
}
});
requestOptions.headers = {
'Content-Type': 'text/xml',
'Content-Length': Buffer.byteLength(postData)
};
break;
default: // defaults 3.0 version
}
var request = this.request(requestOptions, response => {
response.setEncoding('utf8');
var body = '';
response.on('data', chunk => {
body += chunk;
});
response.on('end', () => {
// request callback
//req.session[ this.sessionName ] = 'test';
this._formatValidateResponse(body, (error, user, attributes) => {
if(error) {
console.log(error);
res.sendStatus(401);
} else {
req.session[this.sessionName] = user;
if (this.sessionInfo) {
req.session[this.sessionInfo] = attributes || {};
}
res.redirect(req.session.cas_referer);
}
});
});
response.on('error', function(err) {
console.log('Response error from CAS: ', err);
res.sendStatus(401);
});
});
request.on('error', error => {
console.log('Request error with CAS: ', error);
res.sendStatus(401);
});
// write data to request body
request.write(postData);
request.end();
};
module.exports = CASClient;