forked from fedeonline/passport-cas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
174 lines (158 loc) · 5.87 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
/**
* Cas
*/
var url = require('url'),
http = require('http'),
https = require('https'),
parseString = require('xml2js').parseString,
processors = require('xml2js/lib/processors'),
passport = require('passport')
function Strategy(options, verify) {
if (typeof options == 'function') {
verify = options;
options = {};
}
if (!verify) {
throw new Error('cas authentication strategy requires a verify function');
}
this.version = options.version || "CAS1.0"
this.ssoBase = options.ssoBaseURL;
this.serverBaseURL = options.serverBaseURL;
this.validateURL = options.validateURL;
this.serviceURL = options.serviceURL;
this.parsed = url.parse(this.ssoBase);
if (this.parsed.protocol === 'http:') {
this.client = http;
} else {
this.client = https;
}
passport.Strategy.call(this);
this.name = 'cas';
this._verify = verify;
this._passReqToCallback = options.passReqToCallback;
var self = this;
switch (this.version) {
case "CAS1.0":
this._validateUri = "/validate";
this._validate = function (req, body, verified) {
var lines = body.split('\n');
if (lines.length >= 1) {
if (lines[0] === 'no') {
return verified(new Error('Authentication failed'));
} else if (lines[0] === 'yes' && lines.length >= 2) {
if (self._passReqToCallback) {
self._verify(req, lines[1], verified);
} else {
self._verify(lines[1], verified);
}
return;
}
}
return verified(new Error('The response from the server was bad'));
};
break;
case "CAS3.0":
this._validateUri = "/p3/serviceValidate";
this._validate = function (req, body, verified) {
parseString(body, {
trim: true,
normalize: true,
explicitArray: false,
tagNameProcessors: [processors.normalize, processors.stripPrefix]
}, function (err, result) {
if (err)
return verified(new Error('The response from the server was bad'));
try {
if (result.serviceresponse.authenticationfailure)
return verified(new Error('Authentication failed ' + result.serviceresponse.authenticationfailure.$.code));
var success = result.serviceresponse.authenticationsuccess;
if (success) {
if (self._passReqToCallback) {
self._verify(req, success, verified);
} else {
self._verify(success, verified);
}
return;
}
return verified(new Error('Authentication failed'));
} catch (e) {
console.log(e);
return verified(new Error('Authentication failed '));
}
});
};
break;
default:
throw new Error('unsupported version ' + this.version);
}
}
Strategy.prototype.service = function(req) {
var serviceURL = this.serviceURL || req.originalUrl;
var resolvedURL = url.resolve(this.serverBaseURL, serviceURL);
var parsedURL = url.parse(resolvedURL, true);
delete parsedURL.query.ticket;
delete parsedURL.search;
return url.format(parsedURL);
};
Strategy.prototype.authenticate = function (req, options) {
options = options || {};
// CAS Logout flow as described in
// https://wiki.jasig.org/display/CAS/Proposal%3A+Front-Channel+Single+Sign-Out var relayState = req.query.RelayState;
var relayState = req.query.RelayState;
if (relayState) {
// logout locally
req.logout();
//return this.redirect(this.ssoBase + '/logout?_eventId=next&RelayState=' +
// relayState);
return this.redirect(this.ssoBase + '/logout?_eventId=next&RelayState=' +
relayState+'&gateway=true&service='+this.serverBaseURL);
}
var service = this.service(req);
var ticket = req.query.ticket;
if (!ticket) {
var redirectURL = url.parse(this.ssoBase + '/login', true);
redirectURL.query.service = service;
// copy loginParams in login query
for (var property in options.loginParams ) {
var loginParam = options.loginParams[property];
if (loginParam)
redirectURL.query[property] = loginParam;
}
return this.redirect(url.format(redirectURL));
}
var self = this;
var verified = function (err, user, info) {
if (err) {
return self.error(err);
}
if (!user) {
return self.fail(info);
}
self.success(user, info);
};
var _validateUri = this.validateURL || this._validateUri;
var get = this.client.get({
host: this.parsed.hostname,
port: this.parsed.port,
path: url.format({
pathname: this.parsed.pathname + _validateUri,
query: {
ticket: ticket,
service: service
}
})
}, function (response) {
response.setEncoding('utf8');
var body = '';
response.on('data', function (chunk) {
return body += chunk;
});
return response.on('end', function () {
return self._validate(req, body, verified);
});
});
get.on('error', function (e) {
return self.fail(new Error(e));
});
};
exports.Strategy = Strategy;