-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprotocol1.js
67 lines (59 loc) · 1.98 KB
/
protocol1.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
'use strict'
const { URL } = require('url')
const debug = require('debug')('loopback:component:cas')
const findService = require('./tools.js').findService
/* p1Validate */
module.exports = function (app, config, req, res, next, loginCallback) {
let serviceUrl = req.query['service']
let ticket = req.query['ticket']
let URLserviceUrl
try {
URLserviceUrl = new URL(serviceUrl)
} catch (error) {
if (serviceUrl !== undefined)
debug('Malformed service? ',serviceUrl)
return res.send('no\n')
}
if (!ticket) {
debug('ERROR: Not all of the required request parameters were present.')
return res.send('no\n')
}
// validate ST
app.models.AccessToken.findForRequest(req, { params: ['ticket'], searchDefaultTokenKeys: false},function(err, st_token) {
if (err || !st_token) {
return res.send('no\n')
}
// remove ST
st_token.destroy(function(err) {
if (err) {
debug('ERROR: CAS : error removing ST')
return res.send('no\n')
}
// validate service
findService(app, serviceUrl,function(err, service) {
if (err || !service) {
return res.send('no\n')
}
// check application
if (st_token.appId !== service.id) {
debug('ERROR: CAS /validate ! appId:')
debug('ERROR: service:' + JSON.stringify(req.service) )
debug('ERROR: ST:' + JSON.stringify(st_token))
return res.send('no\n')
}
eval('app.models.' + config.userModel).findOne({ where: { id: st_token.userId } },function(err, user) {
if (err || !user) {
debug('ERROR: Internal Error.')
return res.send(xml.invalidST.renderToString({
code: errorCodes.INTERNAL_ERROR,
message: 'Internal Error'
}))
}
debug('CAS1 validate (email: %s, service: %s)', user.email, service.name)
loginCallback(req, service, user);
return res.send('yes\n')
})
})
})
})
}