From 0efd6144879cf602c5bfec4fb858433949f77cc7 Mon Sep 17 00:00:00 2001 From: Amir Malik Date: Thu, 5 Jul 2012 23:18:36 -0700 Subject: [PATCH] Remove Connect dependency since the routing logic is absurdly simple. No need to introduce another dependency :) Closes #12 Closes #14 Closes #15 --- README.md | 12 +++++++++++- examples/simple.js | 18 +++++++++--------- index.js | 19 +++++++++++-------- package.json | 5 ++--- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 0feacf4..8e82d75 100644 --- a/README.md +++ b/README.md @@ -37,4 +37,14 @@ support both cookie-authenticated and OAuth access to protected URLs, you could populate `req.session.user` so that individual URLs don't need to care about which type of authentication was used. -See examples/simple.js for how to use it. +## Example + +In the root directory, run `npm install express` and then run: + + node examples/simple.js + +Visit to gain access to + or use OAuth to obtain an access token as a code (default) or a token (in the URL hash): + + - code: + - token: diff --git a/examples/simple.js b/examples/simple.js index 47af5f8..261d5a4 100644 --- a/examples/simple.js +++ b/examples/simple.js @@ -1,8 +1,8 @@ // simple server with a protected resource at /secret secured by OAuth 2 var OAuth2Provider = require('../index').OAuth2Provider, - connect = require('connect'), - MemoryStore = connect.session.MemoryStore; + express = require('express'), + MemoryStore = express.session.MemoryStore; // hardcoded list of tuples var myClients = { @@ -121,15 +121,15 @@ function router(app) { }); } -connect.createServer( - connect.logger(), - connect.bodyParser(), - connect.query(), - connect.cookieParser(), - connect.session({store: new MemoryStore({reapInterval: 5 * 60 * 1000}), secret: 'abracadabra'}), +express.createServer( + express.logger(), + express.bodyParser(), + express.query(), + express.cookieParser(), + express.session({store: new MemoryStore({reapInterval: 5 * 60 * 1000}), secret: 'abracadabra'}), myOAP.oauth(), myOAP.login(), - connect.router(router) + express.router(router) ).listen(8081); function escape_entities(s) { diff --git a/index.js b/index.js index c904edc..fc52674 100644 --- a/index.js +++ b/index.js @@ -62,8 +62,10 @@ OAuth2Provider.prototype.login = function() { OAuth2Provider.prototype.oauth = function() { var self = this; - return connect.router(function(app) { - app.get('/oauth/authorize', function(req, res, next) { + return function(req, res, next) { + var uri = ~req.url.indexOf('?') ? req.url.substr(0, req.url.indexOf('?')) : req.url; + + if(req.method == 'GET' && '/oauth/authorize' == uri) { var client_id = req.query.client_id, redirect_uri = req.query.redirect_uri; @@ -82,9 +84,8 @@ OAuth2Provider.prototype.oauth = function() { // user is logged in, render approval page self.emit('authorize_form', req, res, client_id, authorize_url); }); - }); - app.post('/oauth/authorize', function(req, res, next) { + } else if(req.method == 'POST' && '/oauth/authorize' == uri) { var client_id = req.query.client_id, redirect_uri = req.query.redirect_uri, response_type = req.query.response_type || 'code', @@ -144,9 +145,8 @@ OAuth2Provider.prototype.oauth = function() { res.writeHead(303, {Location: url}); res.end(); } - }); - app.post('/oauth/access_token', function(req, res, next) { + } else if(req.method == 'POST' && '/oauth/access_token' == uri) { var client_id = req.body.client_id, client_secret = req.body.client_secret, redirect_uri = req.body.redirect_uri, @@ -166,8 +166,11 @@ OAuth2Provider.prototype.oauth = function() { self.emit('remove_grant', user_id, client_id, code); }); - }); - }); + + } else { + return next(); + } + }; }; exports.OAuth2Provider = OAuth2Provider; diff --git a/package.json b/package.json index a66c791..b67e104 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,14 @@ { "name": "oauth2-provider", - "version": "1.0.2", + "version": "1.1.0", "description": "A simple customizable OAuth 2.0 provider (server) for node.js.", "homepage": "https://github.com/ammmir/node-oauth2-provider", "author": { "name": "Amir Malik", - "url": "http://amir.unoc.net/" + "url": "http://amirmalik.net/" }, "main": "index", "dependencies": { - "connect" : ">=1.6.0 <2.0.0", "serializer": ">=0.0.2 <0.1.0" }, "licenses": [