Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Fixes #98: Do not send empty scopes to an auth server #154

Merged
merged 6 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
58 changes: 38 additions & 20 deletions src/client-oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,19 @@ function createUri (options, tokenType) {
// Check the required parameters are set.
expects(options, 'clientId', 'authorizationUri')

const sep = options.authorizationUri.includes('?') ? '&' : '?'

return options.authorizationUri + sep + Querystring.stringify(Object.assign({
const qs = {
client_id: options.clientId,
redirect_uri: options.redirectUri,
scope: sanitizeScope(options.scopes),
response_type: tokenType,
state: options.state
}, options.query))
}
if (options.scopes && options.scopes.length > 0) {
qs.scope = sanitizeScope(options.scopes)
}

const sep = options.authorizationUri.includes('?') ? '&' : '?'
return options.authorizationUri + sep + Querystring.stringify(
Object.assign(qs, options.query))
}

/**
Expand Down Expand Up @@ -417,18 +421,22 @@ OwnerFlow.prototype.getToken = function (username, password, opts) {
var self = this
var options = Object.assign({}, this.client.options, opts)

const body = {
username: username,
password: password,
grant_type: 'password'
}
if (options.scopes && options.scopes.length > 0) {
body.scope = sanitizeScope(options.scopes)
}

return this.client._request(requestOptions({
url: options.accessTokenUri,
method: 'POST',
headers: Object.assign({}, DEFAULT_HEADERS, {
Authorization: auth(options.clientId, options.clientSecret)
}),
body: {
scope: sanitizeScope(options.scopes),
username: username,
password: password,
grant_type: 'password'
}
body: body
}, options))
.then(function (data) {
return self.client.createToken(data)
Expand Down Expand Up @@ -530,16 +538,21 @@ CredentialsFlow.prototype.getToken = function (opts) {

expects(options, 'clientId', 'clientSecret', 'accessTokenUri')

const body = {
grant_type: 'client_credentials'
}

if (options.scopes && options.scopes.length > 0) {
body.scope = sanitizeScope(options.scopes)
}

return this.client._request(requestOptions({
url: options.accessTokenUri,
method: 'POST',
headers: Object.assign({}, DEFAULT_HEADERS, {
Authorization: auth(options.clientId, options.clientSecret)
}),
body: {
scope: sanitizeScope(options.scopes),
grant_type: 'client_credentials'
}
body: body
}, options))
.then(function (data) {
return self.client.createToken(data)
Expand Down Expand Up @@ -671,15 +684,20 @@ JwtBearerFlow.prototype.getToken = function (token, opts) {
headers.Authorization = auth(options.clientId, options.clientSecret)
}

const body = {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: token
}

if (options.scopes && options.scopes.length > 0) {
body.scope = sanitizeScope(options.scopes)
}

return this.client._request(requestOptions({
url: options.accessTokenUri,
method: 'POST',
headers: headers,
body: {
scope: sanitizeScope(options.scopes),
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: token
}
body: body
}, options))
.then(function (data) {
return self.client.createToken(data)
Expand Down
20 changes: 18 additions & 2 deletions test/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@ describe('code', function () {
expect(githubAuth.code.getUri()).to.equal(
config.authorizationUri + '?client_id=abc&' +
'redirect_uri=http%3A%2F%2Fexample.com%2Fauth%2Fcallback&' +
'scope=notifications&response_type=code&state='
'response_type=code&state=&scope=notifications'
)
})
it('should omit empty scopes', function () {
var authWithoutScopes = new ClientOAuth2({
clientId: config.clientId,
clientSecret: config.clientSecret,
accessTokenUri: config.accessTokenUri,
authorizationUri: config.authorizationUri,
authorizationGrants: ['code'],
redirectUri: config.redirectUri,
scopes: ''
})
expect(authWithoutScopes.code.getUri()).to.equal(
config.authorizationUri + '?client_id=abc&' +
'redirect_uri=http%3A%2F%2Fexample.com%2Fauth%2Fcallback&' +
'response_type=code&state='
)
})
context('when authorizationUri contains query parameters', function () {
Expand All @@ -38,7 +54,7 @@ describe('code', function () {
expect(authWithParams.code.getUri()).to.equal(
config.authorizationUri + '?bar=qux&client_id=abc&' +
'redirect_uri=http%3A%2F%2Fexample.com%2Fauth%2Fcallback&' +
'scope=notifications&response_type=code&state='
'response_type=code&state=&scope=notifications'
)
})
})
Expand Down
18 changes: 18 additions & 0 deletions test/credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ describe('credentials', function () {
expect(user).to.an.instanceOf(ClientOAuth2.Token)
expect(user.accessToken).to.equal(config.accessToken)
expect(user.tokenType).to.equal('bearer')
expect(user.data.scope).to.equal('notifications')
})
})

it('should not include empty scopes in auth server request', function () {
var scopelessAuth = new ClientOAuth2({
clientId: config.clientId,
clientSecret: config.clientSecret,
accessTokenUri: config.accessTokenUri,
authorizationGrants: ['credentials'],
scopes: []
})
return scopelessAuth.credentials.getToken()
.then(function (user) {
expect(user).to.an.instanceOf(ClientOAuth2.Token)
expect(user.accessToken).to.equal(config.accessToken)
expect(user.tokenType).to.equal('bearer')
expect(user.data.scope).to.equal(undefined)
})
})

Expand Down
18 changes: 18 additions & 0 deletions test/jwtbearer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ describe('jwt', function () {
expect(user).to.an.instanceOf(ClientOAuth2.Token)
expect(user.accessToken).to.equal(config.accessToken)
expect(user.tokenType).to.equal('bearer')
expect(user.data.scope).to.equal('notifications')
})
})

it('should not include empty scopes in auth server request', function () {
var scopelessAuth = new ClientOAuth2({
clientId: config.clientId,
clientSecret: config.clientSecret,
accessTokenUri: config.accessTokenUri,
authorizationGrants: ['jwt'],
scopes: []
})
return scopelessAuth.jwt.getToken(config.jwt)
.then(function (user) {
expect(user).to.an.instanceOf(ClientOAuth2.Token)
expect(user.accessToken).to.equal(config.accessToken)
expect(user.tokenType).to.equal('bearer')
expect(user.data.scope).to.equal(undefined)
})
})

Expand Down
20 changes: 19 additions & 1 deletion test/owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('owner', function () {
clientSecret: config.clientSecret,
accessTokenUri: config.accessTokenUri,
authorizationGrants: ['owner'],
scope: 'notifications'
scopes: 'notifications'
})

describe('#getToken', function () {
Expand All @@ -19,6 +19,24 @@ describe('owner', function () {
expect(user).to.an.instanceOf(ClientOAuth2.Token)
expect(user.accessToken).to.equal(config.accessToken)
expect(user.tokenType).to.equal('bearer')
expect(user.data.scope).to.equal('notifications')
})
})

it('should not include empty scopes in auth erver request', function () {
postatum marked this conversation as resolved.
Show resolved Hide resolved
var scopelessAuth = new ClientOAuth2({
clientId: config.clientId,
clientSecret: config.clientSecret,
accessTokenUri: config.accessTokenUri,
authorizationGrants: ['owner'],
scope: ''
})
return scopelessAuth.owner.getToken(config.username, config.password)
.then(function (user) {
expect(user).to.an.instanceOf(ClientOAuth2.Token)
expect(user.accessToken).to.equal(config.accessToken)
expect(user.tokenType).to.equal('bearer')
expect(user.data.scope).to.equal(undefined)
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ app.post(
access_token: config.accessToken,
refresh_token: config.refreshToken,
token_type: 'bearer',
scope: 'notifications'
scope: req.body.scope
})
}
)
Expand Down
18 changes: 16 additions & 2 deletions test/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ describe('token', function () {
expect(githubAuth.token.getUri()).to.equal(
config.authorizationUri + '?client_id=abc&' +
'redirect_uri=http%3A%2F%2Fexample.com%2Fauth%2Fcallback&' +
'scope=notifications&response_type=token&state='
'response_type=token&state=&scope=notifications'
)
})
it('should omit empty scopes', function () {
var authWithoutScopes = new ClientOAuth2({
clientId: config.clientId,
authorizationUri: config.authorizationUri,
authorizationGrants: ['token'],
redirectUri: config.redirectUri,
scopes: []
})
expect(authWithoutScopes.token.getUri()).to.equal(
config.authorizationUri + '?client_id=abc&' +
'redirect_uri=http%3A%2F%2Fexample.com%2Fauth%2Fcallback&' +
'response_type=token&state='
)
})
context('when authorizationUri contains query parameters', function () {
Expand All @@ -34,7 +48,7 @@ describe('token', function () {
expect(authWithParams.token.getUri()).to.equal(
config.authorizationUri + '?bar=qux&client_id=abc&' +
'redirect_uri=http%3A%2F%2Fexample.com%2Fauth%2Fcallback&' +
'scope=notifications&response_type=token&state='
'response_type=token&state=&scope=notifications'
)
})
})
Expand Down