Skip to content

Commit a05eb7d

Browse files
committed
Move the redirect_uri logic in the config module
1 parent 129ee72 commit a05eb7d

File tree

7 files changed

+66
-71
lines changed

7 files changed

+66
-71
lines changed

lib/config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ var merge = ({provider, options = {}, server = {}, name}) => {
7171
}
7272
}
7373

74+
// redirect_uri
75+
if (!provider.redirect_uri && provider.protocol && provider.host && provider.name) {
76+
provider.redirect_uri = [
77+
provider.protocol,
78+
'://',
79+
provider.host,
80+
provider.path,
81+
'/connect/',
82+
provider.name,
83+
'/callback'
84+
].join('')
85+
}
86+
7487
// custom_parameters
7588
;(() => {
7689
if (provider.custom_parameters) {

lib/flow/oauth1.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
var qs = require('qs')
33
var request = require('../client')
4-
var redirect = require('../redirect')
54
var response = require('../response')
65

76

@@ -10,7 +9,7 @@ exports.request = (provider) => new Promise((resolve, reject) => {
109
method: 'POST',
1110
url: provider.request_url,
1211
oauth: {
13-
callback: redirect(provider),
12+
callback: provider.redirect_uri,
1413
consumer_key: provider.key,
1514
consumer_secret: provider.secret
1615
}
@@ -25,7 +24,7 @@ exports.request = (provider) => new Promise((resolve, reject) => {
2524
}
2625
options.form = {
2726
consumer_key: provider.key,
28-
redirect_uri: redirect(provider),
27+
redirect_uri: provider.redirect_uri,
2928
state: provider.state
3029
}
3130
}
@@ -64,14 +63,14 @@ exports.authorize = (provider, req) => new Promise((resolve, reject) => {
6463
if (provider.getpocket) {
6564
params = {
6665
request_token: req.code,
67-
redirect_uri: redirect(provider)
66+
redirect_uri: provider.redirect_uri
6867
}
6968
}
7069
if (provider.ravelry || provider.trello) {
7170
params.scope = provider.scope
7271
}
7372
if (provider.tripit) {
74-
params.oauth_callback = redirect(provider)
73+
params.oauth_callback = provider.redirect_uri
7574
}
7675
if (provider.subdomain) {
7776
url = url.replace('[subdomain]', provider.subdomain)

lib/flow/oauth2.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
var crypto = require('crypto')
33
var qs = require('qs')
44
var request = require('../client')
5-
var redirect = require('../redirect')
65
var response = require('../response')
76

87

@@ -11,7 +10,7 @@ exports.authorize = (provider) => new Promise((resolve) => {
1110
var params = {
1211
client_id: provider.key,
1312
response_type: 'code',
14-
redirect_uri: redirect(provider),
13+
redirect_uri: provider.redirect_uri,
1514
scope: provider.scope,
1615
state: provider.state,
1716
nonce: provider.nonce
@@ -61,7 +60,7 @@ exports.access = (provider, authorize, session) => new Promise((resolve, reject)
6160
code: authorize.code,
6261
client_id: provider.key,
6362
client_secret: provider.secret,
64-
redirect_uri: redirect(provider)
63+
redirect_uri: provider.redirect_uri
6564
}
6665
}
6766
if (provider.basecamp) {
@@ -95,7 +94,7 @@ exports.access = (provider, authorize, session) => new Promise((resolve, reject)
9594
client_assertion: provider.secret,
9695
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
9796
assertion: authorize.code,
98-
redirect_uri: redirect(provider)
97+
redirect_uri: provider.redirect_uri
9998
}
10099
}
101100
if (provider.subdomain) {

lib/redirect.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/config.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,42 @@ describe('config', () => {
7171
})
7272
})
7373

74+
describe('redirect_uri', () => {
75+
it('default', () => {
76+
var provider = {}
77+
var options = {}
78+
var server = {protocol: 'http', host: 'localhost:5000', callback: '/'}
79+
var name = 'grant'
80+
var result = config.merge({provider, options, server, name})
81+
t.equal(
82+
result.redirect_uri,
83+
'http://localhost:5000/connect/grant/callback'
84+
)
85+
})
86+
it('path prefix', () => {
87+
var provider = {path: '/path/prefix'}
88+
var options = {}
89+
var server = {protocol: 'http', host: 'localhost:5000', callback: '/'}
90+
var name = 'grant'
91+
var result = config.merge({provider, options, server, name})
92+
t.equal(
93+
result.redirect_uri,
94+
'http://localhost:5000/path/prefix/connect/grant/callback'
95+
)
96+
})
97+
it('override', () => {
98+
var provider = {redirect_uri: 'http://localhost:5000'}
99+
var options = {}
100+
var server = {protocol: 'http', host: 'localhost:5000', callback: '/'}
101+
var name = 'grant'
102+
var result = config.merge({provider, options, server, name})
103+
t.equal(
104+
result.redirect_uri,
105+
'http://localhost:5000'
106+
)
107+
})
108+
})
109+
74110
describe('custom_params', () => {
75111
it('empty keys in options.custom_params are excluded', () => {
76112
var provider = {custom_params: {name: 'grant'}}
@@ -193,11 +229,13 @@ describe('config', () => {
193229
authorize_url: 'https://www.facebook.com/dialog/oauth',
194230
access_url: 'https://graph.facebook.com/oauth/access_token',
195231
oauth: 2, facebook: true, name: 'facebook',
196-
protocol: 'http', host: 'localhost:3000'
232+
protocol: 'http', host: 'localhost:3000',
233+
redirect_uri: 'http://localhost:3000/connect/facebook/callback'
197234
},
198235
custom: {
199236
custom: true, name: 'custom',
200-
protocol: 'http', host: 'localhost:3000'
237+
protocol: 'http', host: 'localhost:3000',
238+
redirect_uri: 'http://localhost:3000/connect/custom/callback'
201239
}
202240
})
203241
})
@@ -212,11 +250,13 @@ describe('config', () => {
212250
authorize_url: 'https://www.facebook.com/dialog/oauth',
213251
access_url: 'https://graph.facebook.com/oauth/access_token',
214252
oauth: 2, facebook: true, name: 'facebook',
215-
protocol: 'http', host: 'localhost:3000'
253+
protocol: 'http', host: 'localhost:3000',
254+
redirect_uri: 'http://localhost:3000/connect/facebook/callback'
216255
},
217256
custom: {
218257
custom: true, name: 'custom',
219-
protocol: 'http', host: 'localhost:3000'
258+
protocol: 'http', host: 'localhost:3000',
259+
redirect_uri: 'http://localhost:3000/connect/custom/callback'
220260
}
221261
})
222262
})

test/flow/oauth1.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,7 @@ describe('oauth1', () => {
246246
var {body} = await oauth1.request(grant.config.getpocket)
247247
t.deepEqual(body, {
248248
accept: 'application/x-www-form-urlencoded',
249-
form: {
250-
consumer_key: 'key',
251-
redirect_uri: ':///connect/getpocket/callback',
252-
state: 'state'
253-
}
249+
form: {consumer_key: 'key', state: 'state'}
254250
})
255251
})
256252
})
@@ -341,10 +337,7 @@ describe('oauth1', () => {
341337
it('authorize', async () => {
342338
var url = await oauth1.authorize(grant.config.getpocket, {code: 'code'})
343339
var query = qs.parse(url.split('?')[1])
344-
t.deepEqual(query, {
345-
redirect_uri: ':///connect/getpocket/callback',
346-
request_token: 'code'
347-
})
340+
t.deepEqual(query, {request_token: 'code'})
348341
})
349342
})
350343

test/redirect.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)