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

Commit

Permalink
Merge pull request #153 from mulesoft/i144_auth_url_with_parameters
Browse files Browse the repository at this point in the history
Preserve query string params in authorizationUri
  • Loading branch information
postatum authored Jul 27, 2020
2 parents b2f7174 + b9567df commit 75af020
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/client-oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ function createUri (options, tokenType) {
// Check the required parameters are set.
expects(options, 'clientId', 'authorizationUri')

return options.authorizationUri + '?' + Querystring.stringify(Object.assign({
const sep = options.authorizationUri.includes('?') ? '&' : '?'

return options.authorizationUri + sep + Querystring.stringify(Object.assign({
client_id: options.clientId,
redirect_uri: options.redirectUri,
scope: sanitizeScope(options.scopes),
Expand Down
20 changes: 19 additions & 1 deletion test/code.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global describe, it */
/* global describe, it, context */
var expect = require('chai').expect
var config = require('./support/config')
var ClientOAuth2 = require('../')
Expand All @@ -24,6 +24,24 @@ describe('code', function () {
'scope=notifications&response_type=code&state='
)
})
context('when authorizationUri contains query parameters', function () {
it('should preserve query string parameters', function () {
const authWithParams = new ClientOAuth2({
clientId: config.clientId,
clientSecret: config.clientSecret,
accessTokenUri: config.accessTokenUri,
authorizationUri: config.authorizationUri + '?bar=qux',
authorizationGrants: ['code'],
redirectUri: config.redirectUri,
scopes: 'notifications'
})
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='
)
})
})
})

describe('#getToken', function () {
Expand Down
18 changes: 17 additions & 1 deletion test/token.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global describe, it */
/* global describe, it, context */
var expect = require('chai').expect
var config = require('./support/config')
var ClientOAuth2 = require('../')
Expand All @@ -22,6 +22,22 @@ describe('token', function () {
'scope=notifications&response_type=token&state='
)
})
context('when authorizationUri contains query parameters', function () {
it('should preserve query string parameters', function () {
const authWithParams = new ClientOAuth2({
clientId: config.clientId,
authorizationUri: config.authorizationUri + '?bar=qux',
authorizationGrants: ['token'],
redirectUri: config.redirectUri,
scopes: ['notifications']
})
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='
)
})
})
})

describe('#getToken', function () {
Expand Down

0 comments on commit 75af020

Please sign in to comment.