Permalink
Browse files

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
  • Loading branch information...
1 parent 2f270b8 commit 178d2e2d7cd93810c7cdeac2627e5abd2e6cd9a0 @ammmir committed Nov 21, 2012
Showing with 22 additions and 9 deletions.
  1. +1 −1 examples/simple_express2.js
  2. +1 −1 examples/simple_express3.js
  3. +18 −5 index.js
  4. +2 −2 test/OAuth2Provider_tests.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) {
@@ -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) {
View
@@ -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,
@@ -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);
-};
+ return new module.OAuth2Provider({crypt_key: crypt_key, sign_key: sign_key});
+};

0 comments on commit 178d2e2

Please sign in to comment.