-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
194 lines (156 loc) · 4.78 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
const cookie = require('cookie');
const qs = require('qs');
const debug = require('debug')('cas');
const merge = require('./src/merge');
const { getRawBody, parseXML, sendRedirect } = require('./src/utils');
const { CasServerAgent } = require('./src/agent');
const { ServiceTicketStore } = require('./src/store');
module.exports = function httpCasClient(...options) {
const clientOptions = merge(...options);
const agent = new CasServerAgent(clientOptions);
const store = new ServiceTicketStore(agent);
const cookieOptions = {
httpOnly: true,
path: '/'
};
return async function (req, res, {
getTicket = function () {
return cookie.parse(req.headers.cookie || '').st;
},
ticketCreated = function (ticketId) {
res.setHeader('Set-Cookie', cookie.serialize('st', ticketId, cookieOptions));
},
ticketDestroyed = function () {
res.setHeader('Set-Cookie', cookie.serialize('st', '', cookieOptions));
},
bodyParser = async function () {
return qs.parse(await getRawBody(req));
}
} = {}) {
/**
* Skip Authentication
* TODO: express session & koa2 session
*/
if (agent.skip(req, res, clientOptions)) {
return true;
}
/**
* Ignore
*/
if (agent.ignoreValdate(req.url)) {
return true;
}
/**
* Use principal adapter for debugging
*/
if (clientOptions.principalAdapter) {
const principal = clientOptions.principalAdapter(req, res);
req.principal = validatePrincipal(agent.cas, principal);
return true;
}
/**
* SLO
*/
if (req.method === 'POST' && agent.slo) {
const { logoutRequest } = await bodyParser();
if (!logoutRequest) {
debug('CAS client detected a POST method but not SLO request.');
} else {
debug('SLO request detected.');
const { SessionIndex: [ticket] } = await parseXML(logoutRequest);
const serviceTicket = store.get(ticket);
if (serviceTicket) {
serviceTicket.invalidate();
debug(`Ticket ST=${ticket} has been invalidated with principal.`);
} else {
debug(`Principal of ticket ST=${ticket} not found when SLO.`);
}
res.end();
return false;
}
}
/**
* PGT callback
*/
if (agent.proxy && req.method === 'GET' && req.url.indexOf(agent.receptorUrl) !== -1) {
if (req.url === agent.receptorUrl) {
debug('PGT 1st callback detected and respond to cas server status 200.');
} else {
const { searchParams } = new URL(req.url, agent.serviceUrl);
agent.pushPgtiou(searchParams.get('pgtIou'), searchParams.get('pgtId'));
debug('PGT 2ed callback detected and set pgt mapping.');
}
res.statusCode = 200;
res.end();
return false;
}
/**
* Try to resolve st mapped principal.
*/
const ticket = await getTicket();
if (ticket) {
debug(`A ticket has been found ST=${ticket}.`);
const serviceTicket = store.get(ticket);
if (serviceTicket && serviceTicket.valid) {
req.ticket = serviceTicket;
req.principal = serviceTicket.principal;
debug(`Principal has been injected to http.request by the ticket ST=${ticket}.`);
return true;
} else {
store.remove(ticket);
debug(`The ticket ST=${ticket} has been destroyed.`);
await ticketDestroyed(ticket);
}
} else {
debug('No ticket found.');
}
/**
* NO valid st in cookie, try to sso.
*/
const requestURL = new URL(req.url, agent.serviceUrl);
const newTicket = requestURL.searchParams.get('ticket');
if (newTicket) {
debug(`A new ticket recieved ST=${newTicket}`);
requestURL.searchParams.delete('ticket');
const serviceTicketOptions = await agent.validateService(newTicket, requestURL);
debug(`Ticket ST=${newTicket} has been validated successfully.`);
store.put(newTicket, serviceTicketOptions);
await ticketCreated(newTicket);
requestURL.searchParams.delete('_g');
sendRedirect(res, requestURL);
} else {
// Access is unauthenticated.
if (requestURL.searchParams.get('_g') === '1') {
res.statusCode = 403;
return false;
}
debug('Access is unauthenticated and redirect to cas server "/login".');
if (agent.gateway) {
requestURL.searchParams.set('_g', 1);
}
const loginUrl = new URL(agent.loginUrl);
loginUrl.searchParams.set('service', requestURL);
sendRedirect(res, loginUrl);
}
return false;
};
};
function validatePrincipal(cas, principal) {
if(!principal.user)
throw new Error('Must provide user for principal adapter!');
fakePrincipal = {user: principal.user};
attributes = {}
if (cas == 3) {
attributes = {};
attrs = principal.attributes;
Object.keys(attrs).forEach(key => {
if(attrs[key] instanceof Array) {
attributes[key] = attrs[key].join(",");
} else {
attributes[key] = typeof attrs[key] === 'string'? attrs[key] : JSON.stringify(attrs[key]);
}
});
fakePrincipal.attributes = attributes;
}
return fakePrincipal;
}