forked from sbechet/loopback-component-cas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol23.js
265 lines (235 loc) · 8.92 KB
/
protocol23.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
'use strict'
const { URL } = require('url')
const Promise = require('bluebird')
const moment = require('moment')
const crypto = require('crypto')
const xml = require('./xml')
const findService = require('./tools.js').findService
const debug = require('debug')('loopback:component:cas')
const errorCodes = {
INVALID_REQUEST: 'INVALID_REQUEST',
INVALID_TICKET_SPEC: 'INVALID_TICKET_SPEC',
UNAUTHORIZED_SERVICE_PROXY: 'UNAUTHORIZED_SERVICE_PROXY',
INVALID_PROXY_CALLBACK: 'INVALID_PROXY_CALLBACK',
INVALID_TICKET: 'INVALID_TICKET',
INVALID_SERVICE: 'INVALID_SERVICE',
INTERNAL_ERROR: 'INTERNAL_ERROR'
}
/* p23Validate */
module.exports = function (app, config, req, res, next, loginCallback, isProtocol3) {
let getRolesNameFromUserId = function(app, userid) {
return new Promise(function(resolve) {
app.models.Role.getRoles({ principalType: app.models.RoleMapping.USER, principalId: userid}).then(function(roles) {
// suppress dynamic roles
let roles2 = []
for (let i in roles) {
if (typeof roles[i] === 'number') {
roles2.push(roles[i])
}
}
app.models.Role.find({ where: { id: { inq: roles2 } }, fields: ['name'] }).then(function(rolenames) {
let rn = []
for (let el of rolenames) {
rn.push(el.name)
}
resolve(rn)
})
})
})
}
let getAttributes = Promise.coroutine(function* (app, config, user, tgt) {
/* uuid always here */
let returnProfile = {
uuid: user.uuid,
CAS3attributes: {}
}
/* Step 1: Standard CAS3 Attributes */
if (config.attributes && config.attributes.length != 0) {
for (let att of config.attributes) {
switch (att) {
case 'authenticationDate':
returnProfile.CAS3attributes.authenticationDate = moment(tgt.created).format("YYYY-MM-DDThh:mm:ss")
break
case 'longTermAuthenticationRequestTokenUsed':
returnProfile.CAS3attributes.longTermAuthenticationRequestTokenUsed = false //TODO: how to know?
break
case 'isFromNewLogin':
returnProfile.CAS3attributes.isFromNewLogin=false //TODO: how to know?
break
case 'memberOf':
returnProfile.CAS3attributes.memberOf = yield getRolesNameFromUserId(app, user.id)
break
}
}
}
/* Step 2: Specific attributes */
// user.profile _is_ JSON
let profile = {}
try {
if (user.profile) {
profile = JSON.parse(user.profile)
}
/* userId is the user's unique number for all applications */
profile.userId = user.uuid;
/* uuid always here */
profile.uuid = user.uuid;
/*
Normalized profile information conforms to the
[contact schema](https://tools.ietf.org/html/draft-smarr-vcarddav-portable-contacts-00)
established by [Joseph Smarr][schema-author].
We use it for specific useful key like firstname and lastname.
*/
if (config.attributes && config.attributes.length != 0) {
returnProfile.attributes = {}
for (let att of config.attributes) {
switch (att.toLowerCase()) {
case "email":
// email is a specific case
returnProfile.attributes.email = user.email
break
case "firstname":
returnProfile.attributes.firstname = profile.name.givenName
break
case "lastname":
returnProfile.attributes.lastname = profile.name.familyName
break
default:
if ( (att != 'authenticationDate') &&
(att != 'longTermAuthenticationRequestTokenUsed') &&
(att != 'isFromNewLogin') &&
(att != 'memberOf') ) {
if (typeof profile[att] == 'object') {
returnProfile.attributes[att] = JSON.stringify(profile[att])
} else {
returnProfile.attributes[att] = profile[att]
}
}
}
}
}
} catch (e) {
debug("ERROR: %s", e)
}
return returnProfile
})
/* main() */
/* 'TARGET' in req.query ? -> SAML */
let xmlinvalid = req.query['TARGET']?xml.invalidSTSaml:xml.invalidST
let xmlvalid = req.query['TARGET']?xml.validSTSaml:xml.validST
let responseID = '_' + crypto.randomBytes(16).toString('hex')
let serviceUrl = req.query['service']
let URLserviceUrl = new URL(serviceUrl)
let URLorigin = URLserviceUrl.origin
let ticket = req.query['ticket']
res.type('text/xml')
if (!ticket || !serviceUrl) {
debug('Not all of the required request parameters were present.')
return res.send(xmlinvalid.renderToString({
code: errorCodes.INVALID_REQUEST,
message: 'Not all of the required request parameters were present',
responseid: responseID,
issueinstant: moment().toISOString(),
audience: URLorigin
}))
}
// TODO one day
const pgtUrl = req.query['pgtUrl']
const renew = req.query['renew']
// validate ST
app.models.AccessToken.findForRequest(req, { params: ['ticket'], searchDefaultTokenKeys: false},function(err, st_token) {
if (err || !st_token) {
debug('invalid service ticket')
if (err) {
debug(err)
}
return res.send(xmlinvalid.renderToString({
code: errorCodes.INVALID_TICKET,
message: `ticket ${ticket} was not recognized`,
responseid: responseID,
issueinstant: moment().toISOString(),
audience: URLorigin
}))
}
// remove ST
st_token.destroy(function(err) {
if (err) {
debug('could not invalidate service ticket')
return res.send(xmlinvalid.renderToString({
code: errorCodes.INTERNAL_ERROR,
message: `service ticket ${ticket} could not be invalidated`,
responseid: responseID,
issueinstant: moment().toISOString(),
audience: URLorigin
}))
}
// validate service
findService(app, serviceUrl, function(err, service) {
if (err || !service) {
debug('service '+ serviceUrl + ' not recognized')
return res.send(xmlinvalid.renderToString({
code: errorCodes.INVALID_SERVICE,
message: `service ${serviceUrl} was not recognized`,
responseid: responseID,
issueinstant: moment().toISOString(),
audience: URLorigin
}))
}
// check application
if (st_token.appId !== service.id) {
debug('CAS /serviceValidate ! (appId)')
debug('service:' + JSON.stringify(service) )
debug('ST:' + JSON.stringify(st_token))
return res.send(xmlinvalid.renderToString({
code: errorCodes.INVALID_SERVICE,
message: `service ${serviceUrl} was not recognized`,
responseid: responseID,
issueinstant: moment().toISOString(),
audience: URLorigin
}))
}
if (isProtocol3) {
// TODO: record in database (TGT, service) for Single Logout (SLO)
// See 2.3.3. The CAS Server MAY support Single Logout (SLO)
}
eval('app.models.' + config.userModel).findOne({ where: { id: st_token.userId } },function(err, user) {
if (err || !user) {
debug('Internal Error.')
return res.send(xmlinvalid.renderToString({
code: errorCodes.INTERNAL_ERROR,
message: 'Internal Error',
responseid: responseID,
issueinstant: moment().toISOString(),
audience: URLorigin
}))
}
app.models.AccessToken.findOne({ where: { and: [ { userId: st_token.userId }, { appId: null} ]}},function(err, tgt) {
if (err || ! tgt) {
debug('Internal Error.')
return res.send(xmlinvalid.renderToString({
code: errorCodes.INTERNAL_ERROR,
message: 'Internal Error',
responseid: responseID,
issueinstant: moment().toISOString(),
audience: URLorigin
}))
}
getAttributes(app, config, user, tgt).then(function(returnProfile){
let casversion = isProtocol3?'3':'2'
debug('CAS%d validate (email:%s, service: %s)', casversion, user.email, service.name)
loginCallback(req, service, user);
/* 'TARGET' in req.query ? -> SAML */
if (req.query['TARGET']) {
returnProfile.issueinstant = moment().toISOString()
returnProfile.audience = URLorigin
returnProfile.responseid = responseID
// debug('returnProfile: ', JSON.stringify(returnData))
}
// debug('rendering: ', xmlvalid.renderToString({profile: returnProfile}))
return res.send(xmlvalid.renderToString(returnProfile))
})
})
})
})
})
})
}