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

Support scopes defined both in strategy constructor and authenticate() #99

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,15 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
var params = this.authorizationParams(options);
params.response_type = 'code';
if (callbackURL) { params.redirect_uri = callbackURL; }
var scope = options.scope || this._scope;
if (scope) {

var scope;
if (this._scope && options.scope) {
scope = [].concat(options.scope).concat(this._scope);
} else {
scope = this._scope || options.scope;
}

if (scope && scope.length > 0) {
if (Array.isArray(scope)) { scope = scope.join(this._scopeSeparator); }
params.scope = scope;
}
Expand Down
29 changes: 29 additions & 0 deletions test/oauth2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,35 @@ describe('OAuth2Strategy', function() {
expect(url).to.equal('https://www.example.com/oauth2/authorize?response_type=code&redirect_uri=https%3A%2F%2Fwww.example.net%2Fauth%2Fexample%2Fcallback&scope=permission_1%2Cpermission_2&client_id=ABC123');
});
}); // that redirects to service provider with scope option as array using non-standard separator

describe('that redirects to service provider with scope option from constructor', function() {
var strategy = new OAuth2Strategy({
authorizationURL: 'https://www.example.com/oauth2/authorize',
tokenURL: 'https://www.example.com/oauth2/token',
clientID: 'ABC123',
clientSecret: 'secret',
callbackURL: 'https://www.example.net/auth/example/callback',
scope: 'profile',
},
function(accessToken, refreshToken, profile, done) {});

var url;

before(function(done) {
chai.passport.use(strategy)
.redirect(function(u) {
url = u;
done();
})
.req(function(req) {
})
.authenticate({ scope: 'email' });
});

it('should be redirected', function() {
expect(url).to.equal('https://www.example.com/oauth2/authorize?response_type=code&redirect_uri=https%3A%2F%2Fwww.example.net%2Fauth%2Fexample%2Fcallback&scope=email%20profile&client_id=ABC123');
});
}); // that redirects to service provider with scope option from constructor

describe('that redirects to service provider with state option', function() {
var strategy = new OAuth2Strategy({
Expand Down