Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion History.md
Original file line number Diff line number Diff line change
@@ -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
8 changes: 3 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 {
Expand Down
34 changes: 22 additions & 12 deletions lib/cas.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/*!
* node-cas
* Copyright(c) 2011 Casey Banner <kcbanner@gmail.com>
* Copyright(c) 2011 Casey Banner <kcbanner@gmail.com>, Azrael <azrael@imatlas.com>
* MIT Licensed
*/

Expand All @@ -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`.
Expand All @@ -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;
}
Expand All @@ -47,26 +48,28 @@ 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
* @api public
*/

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);
Expand All @@ -81,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);
Expand All @@ -92,7 +96,13 @@ CAS.prototype.validate = function(ticket, callback) {
}

// Format was not correct, error
callback({message: 'Bad response format.'});
callback({message: 'Bad response format.', response: response});
});
}

var req = (this.protocol === 'https' ? https : http).get(options, reqCb);

req.on('error', function(e) {
callback(e);
});
});
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <kcbanner@gmail.com>",
"author": "Casey Banner <kcbanner@gmail.com>, Azrael <azrael@imatlas.com>",
"dependencies": {},
"main": "index",
"engines": { "node": "0.4 || 0.5 || 0.6" }
"engines": { "node": "*" }
}