Skip to content
This repository
Browse code

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...
commit 178d2e2d7cd93810c7cdeac2627e5abd2e6cd9a0 1 parent 2f270b8
Amir Malik authored November 20, 2012
2  examples/simple_express2.js
@@ -12,7 +12,7 @@ var myClients = {
12 12
 // temporary grant storage
13 13
 var myGrants = {};
14 14
 
15  
-var myOAP = new OAuth2Provider('encryption secret', 'signing secret');
  15
+var myOAP = new OAuth2Provider({crypt_key: 'encryption secret', sign_key: 'signing secret'});
16 16
 
17 17
 // before showing authorization page, make sure the user is logged in
18 18
 myOAP.on('enforce_login', function(req, res, authorize_url, next) {
2  examples/simple_express3.js
@@ -14,7 +14,7 @@ var app = express();
14 14
 // temporary grant storage
15 15
 var myGrants = {};
16 16
 
17  
-var myOAP = new OAuth2Provider('encryption secret', 'signing secret');
  17
+var myOAP = new OAuth2Provider({crypt_key: 'encryption secret', sign_key: 'signing secret'});
18 18
 
19 19
 // before showing authorization page, make sure the user is logged in
20 20
 myOAP.on('enforce_login', function(req, res, authorize_url, next) {
23  index.js
@@ -9,8 +9,21 @@ var EventEmitter = require('events').EventEmitter,
9 9
      querystring = require('querystring'),
10 10
       serializer = require('serializer');
11 11
 
12  
-function OAuth2Provider(crypt_key, sign_key) {
13  
-  this.serializer = serializer.createSecureSerializer(crypt_key, sign_key);
  12
+function OAuth2Provider(options) {
  13
+  if(arguments.length != 1) {
  14
+    console.warn('OAuth2Provider(crypt_key, sign_key) constructor has been deprecated, yo.');
  15
+
  16
+    options = {
  17
+      crypt_key: arguments[0],
  18
+      sign_key: arguments[1],
  19
+    };
  20
+  }
  21
+
  22
+  options['authorize_uri'] = options['authorize_uri'] || '/oauth/authorize';
  23
+  options['access_token_uri'] = options['access_token_uri'] || '/oauth/access_token';
  24
+
  25
+  this.options = options;
  26
+  this.serializer = serializer.createSecureSerializer(this.options.crypt_key, this.options.sign_key);
14 27
 }
15 28
 
16 29
 OAuth2Provider.prototype = new EventEmitter();
@@ -64,7 +77,7 @@ OAuth2Provider.prototype.oauth = function() {
64 77
   return function(req, res, next) {
65 78
     var uri = ~req.url.indexOf('?') ? req.url.substr(0, req.url.indexOf('?')) : req.url;
66 79
 
67  
-    if(req.method == 'GET' && '/oauth/authorize' == uri) {
  80
+    if(req.method == 'GET' && self.options.authorize_uri == uri) {
68 81
       var    client_id = req.query.client_id,
69 82
           redirect_uri = req.query.redirect_uri;
70 83
 
@@ -84,7 +97,7 @@ OAuth2Provider.prototype.oauth = function() {
84 97
         self.emit('authorize_form', req, res, client_id, authorize_url);
85 98
       });
86 99
 
87  
-    } else if(req.method == 'POST' && '/oauth/authorize' == uri) {
  100
+    } else if(req.method == 'POST' && self.options.authorize_uri == uri) {
88 101
       var     client_id = req.query.client_id,
89 102
            redirect_uri = req.query.redirect_uri,
90 103
           response_type = req.query.response_type || 'code',
@@ -150,7 +163,7 @@ OAuth2Provider.prototype.oauth = function() {
150 163
         res.end();
151 164
       }
152 165
 
153  
-    } else if(req.method == 'POST' && '/oauth/access_token' == uri) {
  166
+    } else if(req.method == 'POST' && self.options.access_token_uri == uri) {
154 167
       var     client_id = req.body.client_id,
155 168
           client_secret = req.body.client_secret,
156 169
            redirect_uri = req.body.redirect_uri,
4  test/OAuth2Provider_tests.js
@@ -106,5 +106,5 @@ var createOauth2Provider = function(crypt_key, sign_key){
106 106
   var crypt_key = crypt_key || '123131',
107 107
     sign_key = sign_key || 'asdfasdfas';
108 108
 
109  
-  return new module.OAuth2Provider(crypt_key, sign_key);
110  
-};
  109
+  return new module.OAuth2Provider({crypt_key: crypt_key, sign_key: sign_key});
  110
+};

0 notes on commit 178d2e2

Please sign in to comment.
Something went wrong with that request. Please try again.