Skip to content

Commit

Permalink
Merge pull request #31 from DawoonC/master
Browse files Browse the repository at this point in the history
- abstracted redirect uri checking in authorization.js to client model
  • Loading branch information
t1msh committed Mar 6, 2016
2 parents 4ffe594 + b4cca97 commit 7d9a3b2
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 14 deletions.
3 changes: 1 addition & 2 deletions lib/controller/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ module.exports = function(req, res, next) {
function(cb) {
if (!req.oauth2.model.client.getRedirectUri(client))
cb(new error.unsupportedResponseType('RedirectUri is not set for the client'));
else if (redirectUri.indexOf(req.oauth2.model.client.getRedirectUri(client)) !== 0 ||
redirectUri.replace(req.oauth2.model.client.getRedirectUri(client), '').indexOf('#') === 0)
else if (!req.oauth2.model.client.checkRedirectUri(client, redirectUri))
cb(new error.invalidRequest('Wrong RedirectUri provided'));
else {
req.oauth2.logger.debug('RedirectUri check passed: ', redirectUri);
Expand Down
19 changes: 19 additions & 0 deletions lib/model/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ module.exports.getRedirectUri = function(client) {
throw new error.serverError('Client model method "getRedirectUri" is not implemented');
};

/**
* Checks redirect uri for the client
*
* @param client {Object} Client object
* @param client {String} Redirect URI to be checked
*/
module.exports.checkRedirectUri = function(client, redirectUri) {
/**
* For example:
* // for single redirect uri per client
* return (redirectUri.indexOf(getRedirectUri(client)) === 0 &&
* redirectUri.replace(getRedirectUri(client), '').indexOf('#') === -1);
*
* // for multiple redirect uris per client
* return (getRedirectUri(client).indexOf(redirectUri) !== -1);
*/
throw new error.serverError('Client model method "checkRedirectUri" is not implemented');
};

/**
* Fetches client object by primary key
* Should be implemented with server logic
Expand Down
15 changes: 12 additions & 3 deletions test/server/model/memory/oauth2/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module.exports.getId = function(client) {
return client.id;
};

module.exports.getRedirectUri = function(client) {
return client.redirectUri;
};
module.exports.getRedirectUri = getRedirectUri;

module.exports.checkRedirectUri = checkRedirectUri;

module.exports.fetchById = function(clientId, cb) {
for (var i in clients) {
Expand All @@ -17,4 +17,13 @@ module.exports.fetchById = function(clientId, cb) {

module.exports.checkSecret = function(client, secret, cb) {
return cb(null, client.secret == secret);
};

function getRedirectUri(client) {
return client.redirectUri;
};

function checkRedirectUri(client, redirectUri) {
return (redirectUri.indexOf(getRedirectUri(client)) === 0 &&
redirectUri.replace(getRedirectUri(client), '').indexOf('#') === -1);
};
2 changes: 1 addition & 1 deletion test/server/model/redis/oauth2/accessToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports.checkTTL = function(accessToken) {
return true;
};

module.exports.getTTL = function(accessToken, cb) {
module.exports.getTTL = function(token, cb) {
redis.ttl(util.format(KEY.ACCESS_TOKEN, token), cb);
};

Expand Down
17 changes: 13 additions & 4 deletions test/server/model/redis/oauth2/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module.exports.getId = function(client) {
return client.id;
};

module.exports.getRedirectUri = function(client) {
return client.redirectUri;
};
module.exports.getRedirectUri = getRedirectUri;

module.exports.checkRedirectUri = checkRedirectUri;

module.exports.fetchById = function(clientId, cb) {
redis.get(util.format(KEY.CLIENT, clientId), function(err, stringified) {
Expand All @@ -34,4 +34,13 @@ module.exports.fetchById = function(clientId, cb) {
// Add some hashing algorithm for security
module.exports.checkSecret = function(client, secret, cb) {
return cb(null, client.secret == secret);
};
};

function getRedirectUri(client) {
return client.redirectUri;
}

function checkRedirectUri(client, redirectUri) {
return (redirectUri.indexOf(getRedirectUri(client)) === 0 &&
redirectUri.replace(getRedirectUri(client), '').indexOf('#') === -1);
}
17 changes: 13 additions & 4 deletions test/server/model/rethinkdb/oauth2/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports.getId = function(client) {
return client.id;
};

module.exports.getRedirectUri = function(client) {
return client.redirectUri;
};
module.exports.getRedirectUri = getRedirectUri;

module.exports.checkRedirectUri = checkRedirectUri;

module.exports.fetchById = function(clientId, cb) {
connection.acquire(function(err, conn) {
Expand All @@ -20,4 +20,13 @@ module.exports.fetchById = function(clientId, cb) {

module.exports.checkSecret = function(client, secret, cb) {
return cb(null, client.secret == secret);
};
};

function getRedirectUri(client) {
return client.redirectUri;
}

function checkRedirectUri(client, redirectUri) {
return (redirectUri.indexOf(getRedirectUri(client)) === 0 &&
redirectUri.replace(getRedirectUri(client), '').indexOf('#') === -1);
}
1 change: 1 addition & 0 deletions test/server/oauth20.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = function(type) {
// Set client methods
obj.model.client.getId = model.client.getId;
obj.model.client.getRedirectUri = model.client.getRedirectUri;
obj.model.client.checkRedirectUri = model.client.checkRedirectUri;
obj.model.client.fetchById = model.client.fetchById;
obj.model.client.checkSecret = model.client.checkSecret;

Expand Down

0 comments on commit 7d9a3b2

Please sign in to comment.