I'm trying to implement CAS into my login system, but I'm stuck on how to retrieve the "ticket". Basically, the ticket is returned in the URL as a parameter but I can't figure out how to parse it out.
Here is my attempted code:
login: function(req, res) {
if (req.session.authenticated) {
res.redirect('/dashboard');
} else {
var https = require('https');
var url = require('url');
var cas_url = 'https://auth-test.test.edu';
var login_service = '/cas/login';
var validation_service = '/cas/validate';
var service = 'https://localhost:1337';
res.redirect(cas_url + login_service + '?service=' + service);
console.log(req.headers);
}
}
After redirecting to my specified URL, the CAS server redirects back to (with sample ticket): https://localhost:1337/?ticket=ST-10247-Qn0BuiSHob1dxcjODDku-cas-t1
Any ideas on how to optimize my code or retrieve the ticket somehow? Thanks!
EDIT: For validation now, see comment below on selected answer:
index: function(req, res) {
var ticket = req.param('ticket');
if (req.session.authenticated) {
res.redirect('/dashboard');
} else if (ticket) {
var https = require('https');
var options = {
cas_url: 'https://auth-test.berkeley.edu',
login: '/cas/login',
validate: '/cas/validate',
service: 'http://localhost:1337'
};
// redirect to validate URL
res.redirect(options.cas_url + options.validate
+ '?service=' + options.service + '&ticket=' + ticket);
} else {
res.view({
title: 'Home'
});
}
}
If the validation passes through the body will have two lines: yes
username
If the validation does not pass through, the HTML will only display one line: no
How to I parse each line of the displayed HTML page?
You need to create an index route , point it to a controller action, and retrieve the token in that action. For example, in /config/routes.js
, add:
'/': 'HomeController.index'
then in /api/controllers/HomeController
:
index: function (req, res) {
// req.param will contain any route params, body params or
// query string params
var ticket = req.param('ticket');
return res.send("The ticket is: " + ticket);
}
https://localhost:1337/
will then point at that code, assuming you have SSL set up correctly on your server!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.