From 913d8ac73e3ae96344a221f5ee153965cb74fdc3 Mon Sep 17 00:00:00 2001 From: Casey Banner Date: Sat, 16 Apr 2011 17:08:38 -0400 Subject: [PATCH] * Initial commit --- .npmignore | 3 ++ History.md | 5 +++ Makefile | 5 +++ Readme.md | 29 +++++++++++++ index.js | 2 + lib/node-cas.js | 98 +++++++++++++++++++++++++++++++++++++++++++ package.json | 10 +++++ test/node-cas.test.js | 13 ++++++ 8 files changed, 165 insertions(+) create mode 100644 .npmignore create mode 100644 History.md create mode 100644 Makefile create mode 100644 Readme.md create mode 100644 index.js create mode 100644 lib/node-cas.js create mode 100644 package.json create mode 100644 test/node-cas.test.js diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..39e9864 --- /dev/null +++ b/.npmignore @@ -0,0 +1,3 @@ +support +test +examples diff --git a/History.md b/History.md new file mode 100644 index 0000000..c8aa68f --- /dev/null +++ b/History.md @@ -0,0 +1,5 @@ + +0.0.1 / 2010-01-03 +================== + + * Initial release diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..964bf6a --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ + +test: + @expresso -I lib + +.PHONY: test diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..31fb0c3 --- /dev/null +++ b/Readme.md @@ -0,0 +1,29 @@ + +# node-cas + + Central Authentication Service (CAS) client for Node.js + +## License + +(The MIT License) + +Copyright (c) 2011 Casey Banner <kcbanner@gmail.com> + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..ea227ea --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ + +module.exports = require('./lib/node-cas'); \ No newline at end of file diff --git a/lib/node-cas.js b/lib/node-cas.js new file mode 100644 index 0000000..d35152e --- /dev/null +++ b/lib/node-cas.js @@ -0,0 +1,98 @@ + +/*! + * node-cas + * Copyright(c) 2011 Casey Banner + * MIT Licensed + */ + +/** + * Module dependencies + */ + +var https = require('https'); +var url = require('url'); + +/** + * Initialize CAS with the given `options`. + * + * @param {Object} options + * @api public + */ +var CAS = module.exports = function CAS(options) { + options = options || {}; + + if (!options.base_url) { + throw new Error('Required CAS option `base_url` missing.'); + } + + if (!options.service) { + throw new Error('Required CAS option `service` missing.'); + } + + 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'); + } else { + this.hostname = cas_url.host; + this.port = cas_url.port || 443; + this.base_path = cas_url.pathname; + } + + this.service = options.service; +}; + +/** + * Library version. + */ + +CAS.version = '0.0.1'; + +/** + * Attempt to validate a given ticket with the CAS server. + * `callback` is called with (err, auth_status, username) + * + * @param {String} ticket + * @param {Function} callback + * @api public + */ + +CAS.prototype.validate = function(ticket, callback) { + var req = https.get({ + host: this.hostname, + port: this.port, + path: url.format({ + pathname: this.base_path+'/validate', + query: {ticket: ticket, service: this.service} + }) + }, function(res) { + // Handle server errors + res.on('error', function(e) { + callback(e); + }); + + // Read result + res.setEncoding('utf8'); + var response = ''; + res.on('data', function(chunk) { + response += chunk; + }); + + res.on('end', function() { + var sections = response.split('\n'); + if (sections.length >= 1) { + if (sections[0] == 'no') { + callback(undefined, false); + return; + } else if (sections[0] == 'yes' && sections.length >= 2) { + callback(undefined, true, sections[1]); + return; + } + } + + // Format was not correct, error + callback({message: 'Bad response format.'}); + }); + }); +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..a06e712 --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name": "node-cas", + "version": "0.0.1", + "description": "Central Authentication Service (CAS) client for Node.js", + "keywords": ["cas", "central authentication service", "auth", "authentication", "central", "service"], + "author": "Casey Banner ", + "dependencies": {}, + "main": "index", + "engines": { "node": "0.4.x" } +} \ No newline at end of file diff --git a/test/node-cas.test.js b/test/node-cas.test.js new file mode 100644 index 0000000..2ae3444 --- /dev/null +++ b/test/node-cas.test.js @@ -0,0 +1,13 @@ + +/** + * Module dependencies. + */ + +var cas = require('node-cas') + , should = require('should'); + +module.exports = { + 'test .version': function(){ + cas.version.should.match(/^\d+\.\d+\.\d+$/); + } +}; \ No newline at end of file