From 664c5f4f96c350ff22e6ac858fb08664535f86c8 Mon Sep 17 00:00:00 2001 From: azrael Date: Mon, 20 Jan 2014 11:53:23 +0800 Subject: [PATCH 1/2] 1. add the latest node engine supported 2. add HTTP supported 3. fix validate bugs Signed-off-by: azrael --- History.md | 8 +++++++- Readme.md | 8 +++----- lib/cas.js | 31 ++++++++++++++++++++----------- package.json | 6 +++--- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/History.md b/History.md index c8aa68f..08aee41 100644 --- a/History.md +++ b/History.md @@ -1,5 +1,11 @@ +0.0.4 / 2014-01-20 (by Azrael) +================== + + 1. add the latest node engine supported + 2. add HTTP supported + 3. fix validate bugs 0.0.1 / 2010-01-03 ================== - * Initial release + * Initial release diff --git a/Readme.md b/Readme.md index ce3321c..80dd4e4 100644 --- a/Readme.md +++ b/Readme.md @@ -5,8 +5,6 @@ This module only handles the ticket validation step of the CAS login process. Planned features include functions to generate the login/logout URLs. - Generally, to start the login process, send your users to: `https://cas_base_url/login?service=url_to_handle_ticket_validation`. In the University of Waterloo example below, this url would be: `https://cas.uwaterloo.ca/cas/login?service='my_service'`. - ## Installation via npm: @@ -18,20 +16,20 @@ via npm: Setup: var CAS = require('cas'); - var cas = new CAS({base_url: 'https://cas.uwaterloo.ca/cas', service: 'my_service'}); + var cas = new CAS({base_url: 'https://cas.uwaterloo.ca/cas', service: 'http://localhost:8087/result/'}); Using it in a login route: exports.cas_login = function(req, res) { var ticket = req.param('ticket'); if (ticket) { - cas.validate(ticket, function(err, status, username) { + cas.validate(ticket, function(err, status, response) { if (err) { // Handle the error res.send({error: err}); } else { // Log the user in - res.send({status: status, username: username}); + res.send({status: status, response: response}); } }); } else { diff --git a/lib/cas.js b/lib/cas.js index d35152e..40738dc 100644 --- a/lib/cas.js +++ b/lib/cas.js @@ -1,7 +1,7 @@ /*! * node-cas - * Copyright(c) 2011 Casey Banner + * Copyright(c) 2011 Casey Banner , Azrael * MIT Licensed */ @@ -10,7 +10,9 @@ */ var https = require('https'); +var http = require('http'); var url = require('url'); +var pkg = require('../package.json'); /** * Initialize CAS with the given `options`. @@ -30,12 +32,11 @@ var CAS = module.exports = function CAS(options) { } var cas_url = url.parse(options.base_url); - if (cas_url.protocol != 'https:') { - throw new Error('Only https CAS servers are supported.'); - } else if (!cas_url.hostname) { - throw new Error('Option `base_url` must be a valid url like: https://example.com/cas'); + if (!cas_url.hostname) { + throw new Error('Option `base_url` must be a valid url like: https://example.com/cas'); } else { - this.hostname = cas_url.host; + this.protocol = cas_url.protocol; + this.hostname = cas_url.hostname; this.port = cas_url.port || 443; this.base_path = cas_url.pathname; } @@ -47,11 +48,11 @@ var CAS = module.exports = function CAS(options) { * Library version. */ -CAS.version = '0.0.1'; +CAS.version = pkg.version; /** * Attempt to validate a given ticket with the CAS server. - * `callback` is called with (err, auth_status, username) + * `callback` is called with (err, auth_status, response) * * @param {String} ticket * @param {Function} callback @@ -59,14 +60,16 @@ CAS.version = '0.0.1'; */ CAS.prototype.validate = function(ticket, callback) { - var req = https.get({ + var options = { host: this.hostname, port: this.port, path: url.format({ pathname: this.base_path+'/validate', query: {ticket: ticket, service: this.service} }) - }, function(res) { + }; + + var reqCb = function(res) { // Handle server errors res.on('error', function(e) { callback(e); @@ -94,5 +97,11 @@ CAS.prototype.validate = function(ticket, callback) { // Format was not correct, error callback({message: 'Bad response format.'}); }); - }); + } + + var req = (this.protocol === 'https' ? https : http).get(options, reqCb); + + req.on('error', function(e) { + callback(e); + }); }; \ No newline at end of file diff --git a/package.json b/package.json index 7ae9ea4..0af4660 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "cas", - "version": "0.0.3", + "version": "0.0.4", "description": "Central Authentication Service (CAS) client for Node.js", "keywords": ["cas", "central authentication service", "auth", "authentication", "central", "service"], - "author": "Casey Banner ", + "author": "Casey Banner , Azrael ", "dependencies": {}, "main": "index", - "engines": { "node": "0.4 || 0.5 || 0.6" } + "engines": { "node": "*" } } From be58f50c037d6bcf786d334cd66b5fdcee2a26ac Mon Sep 17 00:00:00 2001 From: azrael Date: Mon, 20 Jan 2014 23:00:18 +0800 Subject: [PATCH 2/2] output the error response --- lib/cas.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/cas.js b/lib/cas.js index 40738dc..8f3f7e0 100644 --- a/lib/cas.js +++ b/lib/cas.js @@ -84,6 +84,7 @@ CAS.prototype.validate = function(ticket, callback) { res.on('end', function() { var sections = response.split('\n'); + if (sections.length >= 1) { if (sections[0] == 'no') { callback(undefined, false); @@ -95,7 +96,7 @@ CAS.prototype.validate = function(ticket, callback) { } // Format was not correct, error - callback({message: 'Bad response format.'}); + callback({message: 'Bad response format.', response: response}); }); }