Skip to content
Browse files

Start work to remove rest-mongo dependency.

  • Loading branch information...
1 parent 047f60a commit f592defff6bed1a26061ee906b27a9aca7ef4e8c @francois2metz francois2metz committed Apr 3, 2011
Showing with 143 additions and 151 deletions.
  1. +58 −54 lib/server.js
  2. +27 −29 tests/server/test_authorize.js
  3. +32 −33 tests/server/test_token_endpoint.js
  4. +26 −35 tests/server/test_valid_grant.js
View
112 lib/server.js
@@ -79,14 +79,14 @@ var PARAMS = exports.PARAMS = {
PARAMS.eua.all = PARAMS.eua.mandatory.concat(PARAMS.eua.optional);
-exports.send_grant = function(res, R, user_id, client_data, additional_info) {
+exports.send_grant = function(res, Grant, user_id, client_data, additional_info) {
/* Create a grant and send it to the user.
* The code sent is of the form: grand.id + '.' + grant.code
*
* Arguments:
* - req
* - res
- * - R: rest-mongo instance
+ * - Grant:
* - user_id: id of the user
* - client_data: hash, data about the client:
* - client_id
@@ -96,84 +96,81 @@ exports.send_grant = function(res, R, user_id, client_data, additional_info) {
* the grant.
*
*/
- var grant = new R.Grant({
+ var grant = new Grant({
client_id: client_data.client_id,
time: Date.now(),
user_id: user_id,
code: randomString(128),
redirect_uri: client_data.redirect_uri
});
if(additional_info) grant.additional_info = additional_info;
- grant.save(function() {
+ grant.save(function(err, result) {
var qs = {code: grant.id + '.' + grant.code};
if(client_data.state) qs.state = client_data.state;
qs = querystring.stringify(qs);
redirect(res, client_data.redirect_uri + '?' + qs);
- }, function(err) {
- unknown_error(res, err);
});
};
-
-SERVER.valid_grant = function(R, data, callback, fallback) {
- /* Valid the grant, call callback(token|null) or fallback(err),
- * token being a JSON object.
- * If valid, the grant is invalidated and cannot be used anymore.
- *
- * To be valid, a grant must exist, not be deprecated and have the right
- * associated client.
- *
- * Arguments:
- * - R: rest-mongo instance
- * - data:
- * - code: grant code given by client.
- * - client_id: the client id giving the grant
- * - redirect_uri: the redirect_uri given with the grant
- * - callback: to be called with a token if the grant was valid,
- * or null otherwise.
- * - fallback: to be called in case of error (an invalid grant is not
- * an error).
- *
- */
+/**
+ * Valid the grant, call callback(err, token|null)
+ * token being a JSON object.
+ * If valid, the grant is invalidated and cannot be used anymore.
+ *
+ * To be valid, a grant must exist, not be deprecated and have the right
+ * associated client.
+ *
+ * Arguments:
+ * - Grant: Grant instance
+ * - data:
+ * - code: grant code given by client.
+ * - client_id: the client id giving the grant
+ * - redirect_uri: the redirect_uri given with the grant
+ * - callback: to be called with a token if the grant was valid,
+ * or null otherwise.
+ *
+ */
+SERVER.valid_grant = function(Grant, data, callback) {
var id_code = data.code.split('.');
if(id_code.length != 2) return callback(null);
- R.Grant.get({ids: id_code[0]}, function(grant) {
+ Grant.getById(id_code[0], function(err, grant) {
+ if (err) return callback(err);
var minute_ago = Date.now() - 60000;
if(!grant || grant.time < minute_ago ||
grant.client_id != data.client_id ||
grant.code != id_code[1] ||
grant.redirect_uri != data.redirect_uri
- ) return callback(null);
+ ) return callback('error in grant');
var additional_info = grant.additional_info;
// Delete the grant so that it cannot be used anymore:
- grant.delete_(function() {
+ grant.del(function(err, result) {
+ if (err) return callback(err);
// Generate and send an access_token to the client:
var token = {
access_token: oauth2.create_access_token(grant.user_id, grant.client_id,
additional_info)
// optional: expires_in, refresh_token, scope
};
- callback(token);
- }, fallback);
- }, fallback);
+ callback(null, token);
+ });
+ });
};
-
+/**
+ * OAuth2 token endpoint.
+ * Check the authorization_code, uri_redirect and client secret, issue a token.
+ *
+ * POST to config.oauth2.token_url
+ *
+ * Arguments:
+ * - req
+ * - res
+ *
+ */
var token_endpoint = exports.token_endpoint = function(req, res) {
- /* OAuth2 token endpoint.
- * Check the authorization_code, uri_redirect and client secret, issue a token.
- *
- * POST to config.oauth2.token_url
- *
- * Arguments:
- * - req
- * - res
- *
- */
if(!req.form) return SERVER.oauth_error(res, 'oat', 'invalid_request');
req.form.complete(function(err, params, files) {
if(err) return SERVER.oauth_error(res, 'oat', 'invalid_request');
- var R = SERVER.RFactory();
// We check there is no invalid_requet error:
var error = false;
@@ -197,21 +194,26 @@ var token_endpoint = exports.token_endpoint = function(req, res) {
else if(!params.client_secret) {
return SERVER.oauth_error(res, 'oat', 'invalid_request');
}
+ var model = SERVER.Model
+ , Client = model.Client
+ , Grant = model.Grant;
// Check the client_id exists and does have correct client_secret:
- R.Client.get({ids: params.client_id}, function(client) {
+ Client.getById(params.client_id, function(err, client) {
+ if (err) return unknown_error(res, err);
if(!client || client.secret != params.client_secret)
return SERVER.oauth_error(res, 'oat', 'invalid_client');
var data = {code: params.code, client_id: client.id, redirect_uri: params.redirect_uri};
- SERVER.valid_grant(R, data, function(token) {
+ SERVER.valid_grant(Grant, data, function(err, token) {
+ if (err) return unknown_error(res, err);
if(!token) return SERVER.oauth_error(res, 'oat', 'invalid_grant');
res.writeHead(200, { 'Content-Type': 'application/json'
, 'Cache-Control': 'no-store'
});
res.end(JSON.stringify(token));
- }, function(err) { unknown_error(res, err) });
- }, function(err) { unknown_error(res, err) });
+ });
+ });
});
};
@@ -247,8 +249,9 @@ SERVER.authorize = function(params, req, res) {
return;
}
- var R = SERVER.RFactory();
- R.Client.get({ids: params.client_id}, function(client) {
+ var Client = SERVER.Model.Client;
+ Client.getById(params.client_id, function(err, client) {
+ if (err) return unknown_error(res, err);
if(!client) return SERVER.oauth_error(res, 'eua', 'invalid_client');
// Check the redirect_uri is the one we know about (if we do):
if(client.redirect_uri && params.redirect_uri
@@ -263,7 +266,7 @@ SERVER.authorize = function(params, req, res) {
redirect_uri: redirect_uri,
state: params.state
});
- }, function(err) { unknown_error(res, err) });
+ });
};
@@ -291,7 +294,7 @@ SERVER.authorize_endpoint = function(req, res) {
};
-exports.connector = function(config, RFactory, authentication) {
+exports.connector = function(config, Model, authentication) {
/* Returns Oauth2 server connect middleware.
*
* This middleware will intercept requests aiming at OAuth2 server
@@ -320,7 +323,8 @@ exports.connector = function(config, RFactory, authentication) {
*/
var sserializer = serializer.createSecureSerializer(config.crypt_key, config.sign_key);
oauth2.set_serializer(sserializer);
- SERVER.RFactory = RFactory;
+
+ SERVER.Model = Model;
SERVER.authentication = authentication;
return router(function(app) {
app.get(config.authorize_url, SERVER.authorize_endpoint);
View
56 tests/server/test_authorize.js
@@ -7,11 +7,11 @@ var assert = require('nodetk/testing/custom_assert')
// To reinit some faked / mocked stuff in the end:
-original_RFactory = server.RFactory;
+original_Model = server.Model;
original_authentication = server.authentication;
original_oauth_error = server.oauth_error;
var reinit = function(callback) {
- server.RFactory = original_RFactory;
+ server.Model = original_Model;
server.authentication = original_authentication;
server.oauth_error = original_oauth_error;
callback();
@@ -57,9 +57,7 @@ exports.tests = [
}],
['Unsupported response_type (token and code_and_token)', 6, function() {
- server.RFactory = function(){ // To be sure the server stop after replying
- assert.ok(false, 'Must not be called');
- }
+ server.Model = {}
var params = {client_id: 1, redirect_uri: 1};
var req = {};
['token', 'code_and_token'].forEach(function(type) {
@@ -70,68 +68,68 @@ exports.tests = [
}],
['No client retrieved from DB', 3, function() {
- server.RFactory = function() {return {
- Client: {get: function(query, callback) {
- callback(null);
+ server.Model = {
+ Client: {getById: function(id, callback) {
+ callback(null, null);
}}
- }};
+ };
var params = {client_id: 1, response_type: 'code', redirect_uri: 1};
var res = "res obj", req = {};
expect_oauth_error(res, 'eua', 'invalid_client');
server.authorize(params, req, res);
}],
['Mismatching redirect_uri', 3, function() {
- server.RFactory = function() {return {
- Client: {get: function(query, callback) {
- callback({redirect_uri: 'other_uri'});
+ server.Model = {
+ Client: {getById: function(id, callback) {
+ callback(null, {redirect_uri: 'other_uri'});
}}
- }};
+ };
var params = {client_id: 1, response_type: 'code', redirect_uri: 'some_uri'};
var res = "res obj", req = {};
expect_oauth_error(res, 'eua', 'redirect_uri_mismatch');
server.authorize(params, req, res);
}],
['Error while retrieving client from DB', 3, function() {
- server.RFactory = function() {return {
- Client: {get: function(query, callback, fallback) {
- fallback('error');
+ server.Model = {
+ Client: {getById: function(id, callback) {
+ callback('error');
}}
- }};
+ };
var params = {client_id: 1, response_type: 'code', redirect_uri: 1};
var req = {};
var res = tools.get_expected_res(500);
server.authorize(params, req, res);
}],
['Client without redirect_uri but with redirect_uri param: OK', 3, function() {
- server.RFactory = function() {return {
- Client: {get: function(query, callback) {
- callback({id: 'cid', name: 'cname', redirect_uri: ''});
+ server.Model = {
+ Client: {getById: function(id, callback) {
+ callback(null, {id: 'cid', name: 'cname', redirect_uri: ''});
}}
- }};
+ };
var params = {client_id: 'cid', response_type: 'code', redirect_uri: 'some_uri', state: 'somestate'};
assert_authorize_ok(params);
}],
['Client with redirect_uri but without redirect_uri param: OK', 3, function() {
- server.RFactory = function() {return {
- Client: {get: function(query, callback) {
- callback({redirect_uri: 'some_uri', name: 'cname', id: 'cid'});
+ server.Model = {
+ Client: {getById: function(id, callback) {
+ callback(null, {redirect_uri: 'some_uri', name: 'cname', id: 'cid'});
}}
- }};
+ };
var params = {client_id: 'cid', response_type: 'code',
state: 'somestate'};
assert_authorize_ok(params);
}],
['Client with redirect_uri and redirect_uri param: OK', 3, function() {
- server.RFactory = function() {return {
- Client: {get: function(query, callback) {
- callback({redirect_uri: 'some_uri', name: 'cname', id: 'cid'});
+ server.Model = {
+ Client: {getById: function(id, callback) {
+ callback(null, {redirect_uri: 'some_uri', name: 'cname', id: 'cid'});
}}
- }};
+ };
var params = {client_id: 'cid', response_type: 'code',
redirect_uri: 'some_uri', state: 'somestate'};
assert_authorize_ok(params);
View
65 tests/server/test_token_endpoint.js
@@ -8,11 +8,11 @@ var assert = require('nodetk/testing/custom_assert')
// To reinit faked/mocked stuff in the end:
var initial_oauth_error = server.oauth_error;
-var initial_RFactory = server.RFactory;
+var initial_Model = server.Model;
var initial_valid_grant = server.valid_grant;
exports.module_close = function(callback) {
server.oauth_error = initial_oauth_error;
- server.RFactory = initial_RFactory;
+ server.Model = initial_Model;
server.valid_grant = initial_valid_grant;
callback();
};
@@ -35,7 +35,7 @@ exports.tests = [
}],
['Missing parameter', 12, function() {
- server.RFactory = function(){return {}};
+ server.Model = {};
var res = 'resobj';
var params = {grant_type: 1, client_id: 1, code: 1, redirect_uri: 1};
Object.keys(params).forEach(function(missing) {
@@ -48,7 +48,7 @@ exports.tests = [
}],
['Unsupported grant_type', 12, function() {
- server.RFactory = function(){return {}};
+ server.Model = {};
var res = 'resobj';
var params = {client_id: 1, code: 1, redirect_uri: 1};
[ "password"
@@ -63,7 +63,7 @@ exports.tests = [
}],
['client_secret given twice', 3, function() {
- server.RFactory = function(){return {}};
+ server.Model = {};
var res = 'resobj';
var params = {
client_id: 1, code: 1, redirect_uri: 1,
@@ -80,10 +80,10 @@ exports.tests = [
['Unexisting client', 4, function() {
- server.RFactory = function(){return {Client: {get: function(query, callback) {
- assert.equal(query.ids, 'cid');
- callback(null);
- }}}};
+ server.Model = {Client: {getById: function(id, callback) {
+ assert.equal(id, 'cid');
+ callback(null, null);
+ }}};
var res = 'resobj';
var params = {
client_id: 'cid', code: 1, redirect_uri: 1,
@@ -98,10 +98,10 @@ exports.tests = [
}],
['Incorrect secret (in headers)', 4, function() {
- server.RFactory = function(){return {Client: {get: function(query, callback) {
- assert.equal(query.ids, 'cid');
- callback({secret: 'someothersecret'});
- }}}};
+ server.Model = {Client: {getById: function(id, callback) {
+ assert.equal(id, 'cid');
+ callback(null, {secret: 'someothersecret'});
+ }}};
var res = 'resobj';
var params = {
client_id: 'cid', code: 1, redirect_uri: 1,
@@ -118,10 +118,10 @@ exports.tests = [
['Incorrect secret (in params)', 4, function() {
- server.RFactory = function(){return {Client: {get: function(query, callback) {
- assert.equal(query.ids, 'cid');
- callback({secret: 'someothersecret'});
- }}}};
+ server.Model = {Client: {getById: function(id, callback) {
+ assert.equal(id, 'cid');
+ callback(null, {secret: 'someothersecret'});
+ }}};
var res = 'resobj';
var params = {
client_id: 'cid', code: 1, redirect_uri: 1,
@@ -137,9 +137,9 @@ exports.tests = [
}],
['Invalid grant', 3, function() {
- server.RFactory = function(){return {Client: {get: function(query, callback) {
- callback({secret: 'somesecret', redirect_uri: 'http://client/process'});
- }}}};
+ server.Model = {Client: {getById: function(id, callback) {
+ callback(null, {secret: 'somesecret', redirect_uri: 'http://client/process'});
+ }}};
server.valid_grant = function(_, _, callback){callback(null)};
var res = 'resobj';
var params = {
@@ -156,9 +156,9 @@ exports.tests = [
}],
['Error retrieving client', 3, function() {
- server.RFactory = function(){return {Client: {get: function(query, _, fallback) {
- fallback('error');
- }}}};
+ server.Model = {Client: {getById: function(id, callback) {
+ callback('error');
+ }}};
var params = {
client_id: 'cid', code: 1, redirect_uri: 'http://client/process',
grant_type: 'authorization_code',
@@ -173,10 +173,10 @@ exports.tests = [
}],
['Error while validating grant', 3, function() {
- server.RFactory = function(){return {Client: {get: function(query, callback) {
- callback({secret: 'somesecret', redirect_uri: 'http://client/process'});
- }}}};
- server.valid_grant = function(_, _, _, fallback){fallback('error')};
+ server.Model = {Client: {getById: function(id, callback) {
+ callback(null, {secret: 'somesecret', redirect_uri: 'http://client/process'});
+ }}};
+ server.valid_grant = function(_, _, callback){callback('error')};
var params = {
client_id: 'cid', code: 1, redirect_uri: 'http://client/process',
grant_type: 'authorization_code',
@@ -191,10 +191,10 @@ exports.tests = [
}],
['Valid grant', 3, function() {
- server.RFactory = function(){return {Client: {get: function(query, callback) {
- callback({secret: 'somesecret', redirect_uri: 'http://client/process'});
- }}}};
- server.valid_grant = function(_, _, callback){callback({'a': 'b'})};
+ server.Model = {Client: {getById: function(id, callback) {
+ callback(null, {secret: 'somesecret', redirect_uri: 'http://client/process'});
+ }}};
+ server.valid_grant = function(_, _, callback){callback(null, {'a': 'b'})};
var params = {
client_id: 'cid', code: 1, redirect_uri: 'http://client/process',
grant_type: 'authorization_code',
@@ -207,7 +207,7 @@ exports.tests = [
var res = {
writeHead: function(status_code, headers) {
assert.equal(status_code, 200);
- assert.deepEqual(headers, {
+ assert.deepEqual(headers, {
'Content-Type': 'application/json'
, 'Cache-Control': 'no-store'
});
@@ -221,4 +221,3 @@ exports.tests = [
}],
];
-
View
61 tests/server/test_valid_grant.js
@@ -26,19 +26,17 @@ exports.tests = [
});
}],
-['Error requesting DB', 2, function() {
- var R = {Grant: {get: function(query, callback, fallback) {
- fallback("error");
- assert.ok(true, 'must be called');
- }}};
- server.valid_grant(R, {code: 'id.code'}, function(token) {
- assert.ok(false, 'should not be called');
- }, function() {
+['Error requesting DB', 2, function() {
+ var Grant = {getById: function(id, callback) {
+ callback("error");
assert.ok(true, 'must be called');
+ }};
+ server.valid_grant(Grant, {code: 'id.code'}, function(err, token) {
+ assert.notEqual(err, null, 'should have an error');
});
}],
-['No grant | bad code | bad client_id | grant expired | invalid redirect_uri', 10, function() {
+['No grant | bad code | bad client_id | grant expired | invalid redirect_uri', 15, function() {
Date.now = function(){return 60000}; // 0 + 1 minute
var data = {code: 'id.CODE', client_id: 'cid', redirect_uri: "redirect_uri"};
[ null // no grant
@@ -47,55 +45,48 @@ exports.tests = [
, {client_id: 'cid', code: 'CODE', time: -1, redirect_uri: "redirect_uri"} // grant expired
, {client_id: 'cid', code: 'CODE', time: 50000, redirect_uri: "bad_redirect_uri"}
].forEach(function(retrieved_token) {
- var R = {Grant: {get: function(query, callback, fallback) {
- callback(retrieved_token);
+ var Grant = {getById: function(id, callback) {
+ callback(null, retrieved_token);
assert.ok(true, 'must be called');
- }}};
- server.valid_grant(R, data, function(token) {
+ }};
+ server.valid_grant(Grant, data, function(err, token) {
+ assert.equal(err, 'error in grant');
assert.equal(token, null);
- }, function() {
- assert.ok(false, 'should not be called');
});
});
}],
-['Error deleting grant from DB', 3, function() {
+['Error deleting grant from DB', 3, function() {
Date.now = function(){return 60000}; // 0 + 1 minute
var data = {code: 'id.CODE', client_id: 'cid'};
- var R = {Grant: {get: function(query, callback, fallback) {
- callback({client_id: 'cid', code: 'CODE', time: 50000,
- delete_: function(callback, fallback) {
- fallback('error');
+ var Grant = {getById: function(id, callback) {
+ callback(null, {client_id: 'cid', code: 'CODE', time: 50000,
+ del: function(callback) {
+ callback('error');
assert.ok(true, 'must be called');
}});
assert.ok(true, 'must be called');
- }}};
- server.valid_grant(R, data, function(token) {
- assert.ok(false, 'should not be called');
- }, function() {
- assert.ok(true, 'must be called');
+ }};
+ server.valid_grant(Grant, data, function(err, token) {
+ assert.notEqual(err, null, 'should have an error');
});
}],
-['OK', 3, function() {
+['OK', 3, function() {
Date.now = function(){return 60000}; // 0 + 1 minute
var data = {code: 'id.CODE', client_id: 'cid'};
- var R = {Grant: {get: function(query, callback, fallback) {
- callback({client_id: 'cid', code: 'CODE', time: 50000, user_id: 'uid',
- delete_: function(callback, fallback) {
+ var Grant = {getById: function(id, callback) {
+ callback(null, {client_id: 'cid', code: 'CODE', time: 50000, user_id: 'uid',
+ del: function(callback) {
callback();
assert.ok(true, 'must be called');
}});
assert.ok(true, 'must be called');
- }}};
- server.valid_grant(R, data, function(token) {
+ }};
+ server.valid_grant(Grant, data, function(err, token) {
assert.deepEqual(token, {
access_token: oauth2.create_access_token('uid', 'cid')
});
- }, function() {
- assert.ok(false, 'should not be called');
});
}],
-
];
-

0 comments on commit f592def

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