Skip to content

Commit

Permalink
Pass req object to callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
camshaft committed Aug 8, 2013
1 parent 098cb5c commit e3f339b
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 40 deletions.
8 changes: 7 additions & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,13 @@ app.callback = function(name, defaultCb) {
return function() {
debug('calling callback', name);
var cb = self.callbacks[name] || defaultCb;
if (cb) return cb.apply(null, arguments);

// Check the arity to see if it accepts a req object
var args = arguments.length == cb.length
? arguments
: Array.prototype.slice.call(arguments, 1);

if (cb) return cb.apply(null, args);

// Pass an error to the provided callback
var done = arguments[arguments.length - 1];
Expand Down
18 changes: 11 additions & 7 deletions lib/auth/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ var debug = require('simple-debug')('consulate:auth:client')
exports = module.exports = function(callbacks) {
var getClient = callbacks('client');

function verifyClientCredentials(clientID, clientSecret, done) {
function verifyClientCredentials(req, clientID, clientSecret, done) {
debug('getting client', clientID);
getClient(clientID, function(err, client) {
getClient(req, clientID, function(err, client) {
debug('got client', clientID, client);
if (err) return done(err);
if (!client) return done(null, false);
Expand All @@ -35,9 +35,9 @@ exports = module.exports = function(callbacks) {
});
};

function verifyPublicClient(clientID, done) {
function verifyPublicClient(req, clientID, done) {
debug('getting client', clientID);
getClient(clientID, function(err, client) {
getClient(req, clientID, function(err, client) {
debug('got client', clientID, client)
if (err) return done(err);
if (!client) return done(null, false);
Expand All @@ -47,9 +47,13 @@ exports = module.exports = function(callbacks) {
});
};

passport.use(new BasicStrategy(verifyClientCredentials));
passport.use(new ClientPasswordStrategy(verifyClientCredentials));
passport.use(new PublicClientStrategy(verifyPublicClient));
var options = {
passReqToCallback: true
};

passport.use(new BasicStrategy(options, verifyClientCredentials));
passport.use(new ClientPasswordStrategy(options, verifyClientCredentials));
passport.use(new PublicClientStrategy(options, verifyPublicClient));
return exports;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/auth/exchanges/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ var debug = require('simple-debug')('consulate:auth:exchanges:client')
module.exports = function(callbacks) {
var issueToken = callbacks('issueToken');

return oauth2orize.exchange.clientCredentials(function(client, scope, done) {
return oauth2orize.exchange.clientCredentials(function(req, client, scope, done) {
debug('issuing token for client', client);
return issueToken(client, null, scope, function(err, accessToken, refreshToken, params) {
return issueToken(req, client, null, scope, function(err, accessToken, refreshToken, params) {
debug('issued token for client', client, accessToken);
done(err, accessToken, refreshToken, params);
});
Expand Down
10 changes: 5 additions & 5 deletions lib/auth/exchanges/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ module.exports = function(callbacks) {
, issueToken = callbacks('issueToken')
, invalidateAuthorizationCode = callbacks('invalidateAuthorizationCode');

return oauth2orize.exchange.code(function(client, code, redirectURI, done) {
return oauth2orize.exchange.code(function(req, client, code, redirectURI, done) {
// Get auth code info from the code
debug('getting authorization code', code);
authorizationCode(code, function(err, authCode) {
authorizationCode(req, code, function(err, authCode) {
debug('got authorization code', code, authCode);
if (err) return done(err);
if (!authCode) return done(null, false);
Expand All @@ -24,21 +24,21 @@ module.exports = function(callbacks) {

// Find the user from the code
debug('getting user', authCode.user_id);
getUser(authCode.user_id, function(err, user) {
getUser(req, authCode.user_id, function(err, user) {
debug('got user', authCode.user_id, user);
if (err) return done(err);
if (!user) return done(null, false);

// Complete the exchange of the valid code for an access token
debug('issuing token for user', authCode.user_id);
issueToken(client, user, authCode.scope, function(err, token) {
issueToken(req, client, user, authCode.scope, function(err, token) {
debug('issued token for user', authCode.user_id, token);
if (err) return done(err);
if (!token) return done(null, false);

// Invalidate the authorization code now that we've used it
debug('invalidating authorization code', code);
invalidateAuthorizationCode(code, function(err) {
invalidateAuthorizationCode(req, code, function(err) {
debug('invalidated authorization code', code);
if (err) return done(err);
done(null, token);
Expand Down
8 changes: 4 additions & 4 deletions lib/auth/exchanges/password.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ module.exports = function(callbacks) {
, verifyPassword = callbacks('verifyPassword')
, issueToken = callbacks('issueToken');

return oauth2orize.exchange.password(function(client, username, password, scope, done) {
return oauth2orize.exchange.password(function(req, client, username, password, scope, done) {
// TODO verify that this client is allowed to use the password exchange

// Get the user from the username
debug('getting user by username', username);
userByUsername(username, function(err, user) {
userByUsername(req, username, function(err, user) {
debug('got user by username', username, user);
if (err) return done(err);
if (!user) return done(null, false);

// Hash the password and check that it's valid
debug('verifying user password');
verifyPassword(user, password, function(err, isValid) {
verifyPassword(req, user, password, function(err, isValid) {
debug('verified user password', isValid);
if (err) return done(err);
if (!isValid) return done(null, false);

debug('issuing token for user ', username);
return issueToken(client, user, scope, function(err, accessToken, refreshToken, params) {
return issueToken(req, client, user, scope, function(err, accessToken, refreshToken, params) {
debug('issued token for user ', username, accessToken);
done(err, accessToken, refreshToken, params);
});
Expand Down
4 changes: 2 additions & 2 deletions lib/auth/grants/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ var debug = require('simple-debug')('consulate:auth:grants:code')
module.exports = function(callbacks) {
var createAuthorizationCode = callbacks('createAuthorizationCode');

return oauth2orize.grant.code(function(client, redirectURI, user, ares, done) {
return oauth2orize.grant.code(function(req, client, redirectURI, user, ares, done) {
debug('creating auth code for client', client, 'and user', user);
createAuthorizationCode(client, redirectURI, user, ares, function(err, code) {
createAuthorizationCode(req, client, redirectURI, user, ares, function(err, code) {
debug('created auth code', code);
done(err, code);
});
Expand Down
10 changes: 5 additions & 5 deletions lib/auth/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ exports = module.exports = function(callbacks) {
});

// Deserialize the client
server.deserializeClient(function(id, done) {
server.deserializeClient(function(req, id, done) {
debug('deserializing client_id', id);
getClient(id, function(err, client) {
getClient(req, id, function(err, client) {
debug('deserialized client_id', id, client);
if (err) return done(err);
return done(null, client);
Expand All @@ -28,14 +28,14 @@ exports = module.exports = function(callbacks) {

// Setup authorize
server.authorizeClient = function() {
return server.authorization(function(clientID, redirectURI, scope, type, done) {
return server.authorization(function(req, clientID, redirectURI, scope, type, done) {
debug('getting client', clientID, 'with redirect_uri', redirectURI);
getClient(clientID, function(err, client) {
getClient(req, clientID, function(err, client) {
debug('got client', clientID, client);
if (err) return done(err);
if (!client) return done(null, false);

isValidClientRedirectURI(client, redirectURI, function(err, isValid) {
isValidClientRedirectURI(req, client, redirectURI, function(err, isValid) {
if (err) return done(err);
if (!isValid) return done(null, isValid);
return done(null, client, redirectURI)
Expand Down
6 changes: 3 additions & 3 deletions lib/auth/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ module.exports = function(callbacks) {
});
});

passport.use(new LocalStrategy(function(username, password, done) {
passport.use(new LocalStrategy({passReqToCallback: true}, function(req, username, password, done) {
// Get the user from the username
debug('getting user by username', username);
userByUsername(username, function(err, user) {
userByUsername(req, username, function(err, user) {
debug('got user by username', username, user);
if (err) return done(err);
if (!user) return done(null, false);

// Hash the password and check that it's valid
debug('verifying user password');
verifyPassword(user, password, function(err, isValid) {
verifyPassword(req, user, password, function(err, isValid) {
debug('verified user password', isValid);
if (err) return done(err);
if (!isValid) return done(null, false);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "consulate",
"version": "0.1.0",
"dependencies": {
"oauth2orize": "git+https://github.com/timshadel/oauth2orize.git",
"oauth2orize": "git+https://github.com/consulate/oauth2orize.git",
"express": "~3.2.6",
"startup": "~0.1.12",
"connect-ensure-login": "~0.1.1",
"passport-http": "~0.2.2",
"passport-oauth2-client-password": "~0.1.1",
"passport": "~0.1.17",
"passport": "git+https://github.com/consulate/passport.git",
"passport-local": "~0.1.6",
"passport-oauth2-public-client": "~0.1.0",
"simple-debug": "~1.1.0"
Expand Down
8 changes: 4 additions & 4 deletions test/exchange.code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ describe('a code exchange', function() {
var invalidatedCodes;

var callbacks = {
'authorizationCode': function(code, done) {
'authorizationCode': function(req, code, done) {
var fullCode = {client_id: 'validClientId', user_id: 'validUserId', redirect_uri: 'validRedirectUri'};
if (~invalidatedCodes.indexOf(code)) return done(null, false);
if (code === 'validCode') return done(null, fullCode);
done(null, null);
},
'user': function(userId, done) {
'user': function(req, userId, done) {
if (userId === 'validUserId') return done(null, {});
done(null, null);
},
'issueToken': function(client, user, scope, done) {
'issueToken': function(req, client, user, scope, done) {
done(null, 'some-websafe-token-string');
},
'invalidateAuthorizationCode': function(code, done) {
'invalidateAuthorizationCode': function(req, code, done) {
invalidatedCodes.push(code);
done(null);
}
Expand Down
4 changes: 2 additions & 2 deletions test/grant.code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var codeGrant = require('..').grants.code
describe('a code grant', function() {

var callbacks = {
'createAuthorizationCode': function(client, redirectURI, user, ares, done) {
'createAuthorizationCode': function(req, client, redirectURI, user, ares, done) {
if (client.id === 'validClientId') return done(null, 'validCode');
done(null, null);
}
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('a code grant', function() {
res: res,
};
res.allow = true;
code.response(txn, issues_code(res, done), expect_no_error(done));
code.response(txn, req, issues_code(res, done), expect_no_error(done));
});

});
6 changes: 3 additions & 3 deletions test/password.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ var pass = require('..').exchanges.password

describe('a password exchange', function() {
var callbacks = {
'userByUsername': function(username, done) {
'userByUsername': function(req, username, done) {
if (username === 'validuser') return done(null, {});
done(null, null);
},
'verifyPassword': function(user, password, done) {
'verifyPassword': function(req, user, password, done) {
if (password === 'validpass') return done(null, true);
done(null, false);
},
'issueToken': function(client, user, scope, done) {
'issueToken': function(req, client, user, scope, done) {
done(null, 'some-websafe-token-string');
}
}
Expand Down

0 comments on commit e3f339b

Please sign in to comment.