Skip to content

Commit d508e2c

Browse files
meotimdihiaEomm
authored andcommitted
feature: add callback uri params (#19)
1 parent 0617f91 commit d508e2c

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ const oauthPlugin = fp(function (fastify, options, next) {
2929
if (typeof options.callbackUri !== 'string') {
3030
return next(new Error('options.callbackUri should be a string'))
3131
}
32+
if (options.callbackUriParams && typeof options.callbackUriParams !== 'object') {
33+
return next(new Error('options.callbackUriParams should be a object'))
34+
}
3235
if (options.generateStateFunction && typeof options.generateStateFunction !== 'function') {
3336
return next(new Error('options.generateStateFunction should be a function'))
3437
}
@@ -45,19 +48,21 @@ const oauthPlugin = fp(function (fastify, options, next) {
4548
const name = options.name
4649
const credentials = options.credentials
4750
const callbackUri = options.callbackUri
51+
const callbackUriParams = options.callbackUriParams || {}
4852
const scope = options.scope
4953
const generateStateFunction = options.generateStateFunction || defaultGenerateStateFunction
5054
const checkStateFunction = options.checkStateFunction || defaultCheckStateFunction
5155
const startRedirectPath = options.startRedirectPath
5256

5357
function startRedirectHandler (request, reply) {
5458
const state = generateStateFunction()
55-
56-
const authorizationUri = this[name].authorizationCode.authorizeURL({
59+
const urlOptions = Object.assign({}, callbackUriParams, {
5760
redirect_uri: callbackUri,
5861
scope: scope,
5962
state: state
6063
})
64+
65+
const authorizationUri = this[name].authorizationCode.authorizeURL(urlOptions)
6166
reply.redirect(authorizationUri)
6267
}
6368

test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,67 @@ t.test('options.callbackUri should be and object', t => {
210210
})
211211
})
212212

213+
t.test('options.callbackUriParams should be and object', t => {
214+
t.plan(1)
215+
216+
const fastify = createFastify({ logger: { level: 'silent' } })
217+
218+
fastify.register(oauthPlugin, {
219+
name: 'the-name',
220+
credentials: {
221+
client: {
222+
id: 'my-client-id',
223+
secret: 'my-secret'
224+
},
225+
auth: oauthPlugin.GITHUB_CONFIGURATION
226+
},
227+
callbackUri: '/callback',
228+
callbackUriParams: 1
229+
})
230+
.ready(err => {
231+
t.strictSame(err.message, 'options.callbackUriParams should be a object')
232+
})
233+
})
234+
235+
t.test('options.callbackUriParams', t => {
236+
const fastify = createFastify({ logger: true })
237+
238+
fastify.register(oauthPlugin, {
239+
name: 'the-name',
240+
credentials: {
241+
client: {
242+
id: 'my-client-id',
243+
secret: 'my-secret'
244+
},
245+
auth: oauthPlugin.GITHUB_CONFIGURATION
246+
},
247+
startRedirectPath: '/login/github',
248+
callbackUri: '/callback',
249+
callbackUriParams: {
250+
access_type: 'offline'
251+
},
252+
scope: ['notifications']
253+
})
254+
255+
t.tearDown(fastify.close.bind(fastify))
256+
257+
fastify.listen(0, function (err) {
258+
t.error(err)
259+
260+
fastify.inject({
261+
method: 'GET',
262+
url: '/login/github'
263+
}, function (err, responseStart) {
264+
t.error(err)
265+
266+
t.equal(responseStart.statusCode, 302)
267+
const matched = responseStart.headers.location.match(/https:\/\/github\.com\/login\/oauth\/authorize\?response_type=code&client_id=my-client-id&access_type=offline&redirect_uri=%2Fcallback&scope=notifications&state=(.*)/)
268+
t.ok(matched)
269+
t.end()
270+
})
271+
})
272+
})
273+
213274
t.test('options.generateStateFunction should be and object', t => {
214275
t.plan(1)
215276

0 commit comments

Comments
 (0)