Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to define required scopes by the middleware #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ server.exchange(oauth2orizeFacebook(function (client, profile, scope, cb) {
}));
```


## Options

**requiredScopes**

Required scopes allows the exchange middleware to throw an error if the user does not allow a permission required for the application to work.
The requiredScopes are specified within the options (`opts`) object.

```js
// ...

server.exchange(
oauth2orizeFacebook(
{
requiredScopes: ['scope1', 'scope2']
},
function (client, profile, scope, cb) {
// ...
}
)
);
```
## License

MIT licensed.
Expand Down
13 changes: 11 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ module.exports = function (opts, issue) {

var userProperty = opts.userProperty || 'user';
var separators = opts.scopeSeparator || ' ';
var requiredScopes = opts.requiredScopes || [];

if (!Array.isArray(requiredScopes)) {
if (typeof(requiredScopes) === 'string') {
requiredScopes = [ requiredScopes ];
} else {
requiredScopes = [];
}
}

if (!Array.isArray(separators)) {
separators = [ separators ];
Expand All @@ -43,10 +52,10 @@ module.exports = function (opts, issue) {
'Missing Facebook access token!', 'invalid_request'));
}

getFacebookProfile(token, function (err, profile) {
getFacebookProfile(token, requiredScopes, function (err, profile) {
if (err) {
return next(new AuthorizationError(
'Could not get Facebook profile using provided access token.',
err.message || 'Could not get Facebook profile using provided access token.',
'invalid_request'
));
}
Expand Down
51 changes: 48 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var request = require('request');

exports.getFacebookProfile = function (accessToken, cb) {
exports.getFacebookProfile = function (accessToken, requiredScopes, cb) {
request({
url: 'https://graph.facebook.com/me?access_token=' + accessToken,
json: true
Expand All @@ -17,6 +17,51 @@ exports.getFacebookProfile = function (accessToken, cb) {
return cb(new Error(msg));
}

cb(null, body);
if (requiredScopes.length === 0) {
return cb(null, body);
}

getFacebookPermissions(accessToken, function(err, scopes) {
var grantedScopes = [];

for (var index in scopes) {
if (scopes.hasOwnProperty(index)) {
var scope = scopes[index];

if (scope.status === 'granted') {
grantedScopes.push(scope.permission);
}
}
}

for (var index in requiredScopes) {
if (requiredScopes.hasOwnProperty(index)) {
var rScope = requiredScopes[index];
if (grantedScopes.indexOf(rScope) === -1) {
var msg = 'Facebook profile authorization has insufficient scopes';
return cb(new Error(msg));
}
}
}
cb(null, body);
});
};

getFacebookPermissions = function(accessToken, cb) {
request({
url: 'https://graph.facebook.com/me/permissions?access_token=' + accessToken,
json: true
},
function (err, res, body) {
if (err) {
return cb(err);
}

if (body && body.error) {
var msg = body.error.message || 'Could not get Facebook profile permissions.';
return cb(new Error(msg));
}

cb(null, body.data);
});
};
}