From 178d2e2d7cd93810c7cdeac2627e5abd2e6cd9a0 Mon Sep 17 00:00:00 2001 From: Amir Malik Date: Tue, 20 Nov 2012 21:02:20 -0800 Subject: [PATCH] Make OAuth endpoints customizable and add new-style constructor. Deprecate OAuth2Provider(crypt_key, sign_key) constructor in favor of one that accepts a single options object, containing the following keys: * crypt_key * sign_key * authorize_uri (optional, default: /oauth/authorize) * access_token_uri (optional, default: /oauth/access_token) Closes #17 --- examples/simple_express2.js | 2 +- examples/simple_express3.js | 2 +- index.js | 23 ++++++++++++++++++----- test/OAuth2Provider_tests.js | 4 ++-- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/examples/simple_express2.js b/examples/simple_express2.js index cfdb111..e03b85c 100644 --- a/examples/simple_express2.js +++ b/examples/simple_express2.js @@ -12,7 +12,7 @@ var myClients = { // temporary grant storage var myGrants = {}; -var myOAP = new OAuth2Provider('encryption secret', 'signing secret'); +var myOAP = new OAuth2Provider({crypt_key: 'encryption secret', sign_key: 'signing secret'}); // before showing authorization page, make sure the user is logged in myOAP.on('enforce_login', function(req, res, authorize_url, next) { diff --git a/examples/simple_express3.js b/examples/simple_express3.js index 4291e0f..0cfcf38 100644 --- a/examples/simple_express3.js +++ b/examples/simple_express3.js @@ -14,7 +14,7 @@ var app = express(); // temporary grant storage var myGrants = {}; -var myOAP = new OAuth2Provider('encryption secret', 'signing secret'); +var myOAP = new OAuth2Provider({crypt_key: 'encryption secret', sign_key: 'signing secret'}); // before showing authorization page, make sure the user is logged in myOAP.on('enforce_login', function(req, res, authorize_url, next) { diff --git a/index.js b/index.js index f5ef767..9be672d 100644 --- a/index.js +++ b/index.js @@ -9,8 +9,21 @@ var EventEmitter = require('events').EventEmitter, querystring = require('querystring'), serializer = require('serializer'); -function OAuth2Provider(crypt_key, sign_key) { - this.serializer = serializer.createSecureSerializer(crypt_key, sign_key); +function OAuth2Provider(options) { + if(arguments.length != 1) { + console.warn('OAuth2Provider(crypt_key, sign_key) constructor has been deprecated, yo.'); + + options = { + crypt_key: arguments[0], + sign_key: arguments[1], + }; + } + + options['authorize_uri'] = options['authorize_uri'] || '/oauth/authorize'; + options['access_token_uri'] = options['access_token_uri'] || '/oauth/access_token'; + + this.options = options; + this.serializer = serializer.createSecureSerializer(this.options.crypt_key, this.options.sign_key); } OAuth2Provider.prototype = new EventEmitter(); @@ -64,7 +77,7 @@ OAuth2Provider.prototype.oauth = function() { 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) { + if(req.method == 'GET' && self.options.authorize_uri == uri) { var client_id = req.query.client_id, redirect_uri = req.query.redirect_uri; @@ -84,7 +97,7 @@ OAuth2Provider.prototype.oauth = function() { self.emit('authorize_form', req, res, client_id, authorize_url); }); - } else if(req.method == 'POST' && '/oauth/authorize' == uri) { + } else if(req.method == 'POST' && self.options.authorize_uri == uri) { var client_id = req.query.client_id, redirect_uri = req.query.redirect_uri, response_type = req.query.response_type || 'code', @@ -150,7 +163,7 @@ OAuth2Provider.prototype.oauth = function() { res.end(); } - } else if(req.method == 'POST' && '/oauth/access_token' == uri) { + } else if(req.method == 'POST' && self.options.access_token_uri == uri) { var client_id = req.body.client_id, client_secret = req.body.client_secret, redirect_uri = req.body.redirect_uri, diff --git a/test/OAuth2Provider_tests.js b/test/OAuth2Provider_tests.js index 96887d6..abab633 100644 --- a/test/OAuth2Provider_tests.js +++ b/test/OAuth2Provider_tests.js @@ -106,5 +106,5 @@ var createOauth2Provider = function(crypt_key, sign_key){ var crypt_key = crypt_key || '123131', sign_key = sign_key || 'asdfasdfas'; - return new module.OAuth2Provider(crypt_key, sign_key); -}; \ No newline at end of file + return new module.OAuth2Provider({crypt_key: crypt_key, sign_key: sign_key}); +};