-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
183 lines (157 loc) · 4.73 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
/**
* Cas
*/
var url = require('url'),
http = require('http'),
https = require('https'),
passport = require('passport')
// query parameter used to request a gateway SSO
var gatewayParameter = 'useGateway=true';
/**
* Creates an instance of `Strategy`.
*/
function Strategy(options, verify) {
if (typeof options == 'function') {
verify = options;
options = {};
}
if (!verify) {
throw new Error('cas authentication strategy requires a verify function');
}
this.ssoBase = options.ssoBaseURL;
this.serverBaseURL = options.serverBaseURL;
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;
}
/**
* Authenticate request.
*
* @param req The request to authenticate.
* @param options Strategy-specific options.
*/
Strategy.prototype.authenticate = function(req, options) {
options = options || {};
var origUrl = req.originalUrl;
var ticket = req.param('ticket');
var service = url.resolve(this.serverBaseURL, origUrl);
// check if gateway SSO requested, remove any
// gateway query parameter from URL
var serviceUrl = url.parse(service, true);
delete serviceUrl.search;
service = stripGatewayAuthenticationParameter(serviceUrl);
if (!ticket) {
// Building the redirect url to the login server
var loginServerURL = url.parse(this.ssoBase + '/login', true);
// Adding the gateway parameter if requested
if (useGatewayAuthentication(req)) {
loginServerURL.query.gateway = true;
}
// Adding the service parameter
loginServerURL.query.service = service;
// Redirecting to the login server.
return this.redirect(url.format(loginServerURL));
}
// Formatting the service url and adding the nextUrl parameter after it's done due to double encoding
// Adding the service parameter
// Re-creates the original service URL.
// Remove search and ticket since they are not valid now
var baseServiceUrl = url.resolve(this.serverBaseURL, origUrl);
var tmpUrl = url.parse(baseServiceUrl, true);
delete tmpUrl.search;
delete tmpUrl.query.ticket;
var nextUrl = stripGatewayAuthenticationParameter(tmpUrl);
var validateService = nextUrl;
var self = this;
/*
* Verifies the user login add set error, fail or success depending on the result.
*/
var verified = function (err, user, info) {
if (err) { return self.error(err); }
if (!user) { return self.fail(info); }
self.success(user, info);
};
/**
* Request the login server's /validate with the ticket and service parameters.
* The callback function handles the CAS server response.
* Read more at the "CAS protocol section 2.4.2": http://www.jasig.org/cas/protocol
*
* Response on ticket validation success:
* yes
* u1foobar
*
* Response on ticket validation failure:
* no
*/
var get = this.client.get({
host: this.parsed.hostname,
port: this.parsed.port,
path: url.format({
pathname: '/validate',
query: {
ticket: ticket,
service: validateService
}
})
}, function(response) {
response.setEncoding('utf8');
var body = '';
response.on('data', function(responseData) {
return body += responseData;
});
return response.on('end', function() {
var responseLines = body.split('\n');
if (responseLines.length >= 1) {
if (responseLines[0] === 'no') {
return self.fail(new Error('Authentication failed'));
} else if (responseLines[0] === 'yes' && responseLines.length >= 2) {
self._verify(responseLines[1], verified);
return;
}
}
return self.fail(new Error('The response from the server was bad'));
});
});
get.on('error', function(e) {
return self.fail(new Error(e));
});
};
/**
* Check if we are requested to perform a gateway signon, i.e. a check
*/
function useGatewayAuthentication(req) {
// can be set on request if via application supplied callback
if (req.useGateway == true) { return true; }
// otherwise via query parameter
var origUrl = req.originalUrl;
var useGateway = false;
var idx = origUrl.indexOf(gatewayParameter);
if (idx >= 0) {
useGateway = true;
}
return useGateway;
}
/**
* If a gateway query parameter is added, remove it.
*/
function stripGatewayAuthenticationParameter(aUrl) {
if (aUrl.query && aUrl.query.useGateway) {
delete aUrl.query.useGateway;
}
if (aUrl.query.nextUrl) {
var theNextUrl = decodeURIComponent(aUrl.query.nextUrl);
aUrl.query.nextUrl = decodeURIComponent(theNextUrl);
}
var theUrl = url.format(aUrl);
return theUrl;
}
/**
* Expose `Strategy`.
*/
exports.Strategy = Strategy;