Skip to content

Commit

Permalink
Make OAuth endpoints customizable and add new-style constructor.
Browse files Browse the repository at this point in the history
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 ammmir#17
  • Loading branch information
ammmir committed Nov 21, 2012
1 parent 2f270b8 commit 178d2e2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/simple_express2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_express3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand All @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions test/OAuth2Provider_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.