Skip to content

Commit

Permalink
Remove Connect dependency since the routing logic is absurdly simple.
Browse files Browse the repository at this point in the history
No need to introduce another dependency :)

Closes ammmir#12
Closes ammmir#14
Closes ammmir#15
  • Loading branch information
ammmir committed Jul 6, 2012
1 parent b3b9049 commit 0efd614
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <http://localhost:8081/login> to gain access to
<http://localhost:8081/secret> or use OAuth to obtain an access token as a code (default) or a token (in the URL hash):

- code: <http://localhost:8081/oauth/authorize?client_id=foo&redirect_uri=http://myapp.foo/>
- token: <http://localhost:8081/oauth/authorize?client_id=foo&redirect_uri=http://myapp.foo/&response_type=token>
18 changes: 9 additions & 9 deletions examples/simple.js
Original file line number Diff line number Diff line change
@@ -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 <client id, client secret> tuples
var myClients = {
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 11 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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',
Expand Down Expand Up @@ -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,
Expand All @@ -166,8 +166,11 @@ OAuth2Provider.prototype.oauth = function() {

self.emit('remove_grant', user_id, client_id, code);
});
});
});

} else {
return next();
}
};
};

exports.OAuth2Provider = OAuth2Provider;
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit 0efd614

Please sign in to comment.