Skip to content

Commit

Permalink
[refactor] Refactor of authorized scope handling
Browse files Browse the repository at this point in the history
  • Loading branch information
stolsma committed Jan 9, 2012
1 parent 9796547 commit 4fb40fd
Showing 1 changed file with 31 additions and 45 deletions.
76 changes: 31 additions & 45 deletions lib/oauth2/plugin/authorization.js
Expand Up @@ -101,64 +101,50 @@ Authorization.prototype.authorizeScope = function(req, res, user_id, client_id,
*
*/
Authorization.prototype.checkAuthorizations = function(user_id, client_id, scopes, next) {
var that = this,
authorized_scopes = [],
var authorized_scopes = [],
unauthorized_scopes = [];

function filterScope(index) {
that.authorizations.get(user_id + ':' + client_id + ':' + scopes[index], function(err, data) {
if (err) unauthorized_scopes.push(scopes[index]);
if (data) authorized_scopes.push(scopes[index]);
if (index === scopes.length-1) {
return next(null, authorized_scopes, unauthorized_scopes);
if (scopes.length === 0) return next(null, [], []);

this.authorizations.get(user_id + ':' + client_id, function(err, data) {
if (err) return next(err);

if (data) {
for (var i=0; i < scopes.length; i++) {
if (data['__' + scopes[i]]) {
authorized_scopes.push(scopes[i]);
} else {
unauthorized_scopes.push(scopes[i]);
}
}
filterScope(index+1);
});
}

if (scopes.length > 0) {
filterScope(0);
} else {
next(null, [], []);
}
}
next(null, authorized_scopes, unauthorized_scopes);
});
};

/**
*
*/
Authorization.prototype.addAuthorizations = function(user_id, client_id, scopes, next) {
Authorization.prototype.addAuthorization = function(user_id, client_id, scopes, next) {
var that = this;

function addScope(index) {
that.addAuthorization(user_id, client_id, scopes[index], function(err, data) {
if (err) return next(err);
if (index === scopes.length-1) return next(null);
addScope(index+1);
});
}
if (typeof scopes === 'string') scopes = [scopes];

if (scopes.length > 0) {
addScope(0);
} else {
next(null);
}
};

/**
*
*/
Authorization.prototype.addAuthorization = function(user_id, client_id, scope, next) {
var data = {
user_id: user_id,
client_id: client_id,
scope: scope
};

this.authorizations.add(user_id + ':' + client_id + ':' + scope, data, function(err, data) {
next(err, data);
this.authorizations.get(user_id + ':' + client_id, function(err, data) {
if (err) return next(err);

if (!data) data = {};
for (var i=0; i < scopes.length; i++) {
data['__' + scopes[i]] = true;
}

that.authorizations.add(user_id + ':' + client_id, data, function(err, data) {
next(err, data);
});
});
};


/**
* Check with the authorization service that the given scopes are authorized for the given client_id. If not all scopes
* are authorized, the resource owner gets a authorization page that returns to `cb_url` to resume the current client
Expand Down Expand Up @@ -204,7 +190,7 @@ Authorization.prototype.authorize = function(req, res, user_id, client_id, scope
if (postTime < 300000) {
if (('allow' in body)) {
// then process the authorized scopes and add them to the authorized scopes list
self.addAuthorizations(user_id, client_id, scopes, function(err) {
self.addAuthorization(user_id, client_id, scopes, function(err) {
// return the authorized scopes and the original url
return next(err, scopes, key[1]);
});
Expand Down

0 comments on commit 4fb40fd

Please sign in to comment.