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

Preserve query string params in authorizationUri #153

Merged
merged 1 commit into from
Jul 27, 2020
Merged
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
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