Skip to content

Commit fa557a9

Browse files
committed
Allow overriding JWT opts in helper functions
1 parent 4a6ebb1 commit fa557a9

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,23 +336,25 @@ function rejectPreflight (res, origin) {
336336
res.end(JSON.stringify({ data: null, error: 'Origin not allowed' }))
337337
}
338338

339-
function genToken (payload = {}) {
340-
let key = jwtOpts.secret || jwtOpts.privateKey
339+
function genToken (payload = {}, opts) {
340+
opts = opts ? Object.assign({}, jwtOpts, opts) : jwtOpts
341+
let key = opts.secret || opts.privateKey
341342
let noOpts = () => new Error('Missing required jsonwebtoken opts')
342-
if (!jwtOpts || !key) return Promise.reject(noOpts())
343-
let opts = filterObj(jwtOpts, wlSign)
343+
if (!opts || !key) return Promise.reject(noOpts())
344+
let signOpts = filterObj(opts, wlSign)
344345
return new Promise((resolve, reject) => {
345-
jsonwebtoken.sign(payload, key, opts, (err, token) => {
346+
jsonwebtoken.sign(payload, key, signOpts, (err, token) => {
346347
return err ? reject(err) : resolve(token)
347348
})
348349
})
349350
}
350351

351-
function authenticate (jwt) {
352-
let key = (jwtOpts || {}).secret || (jwtOpts || {}).publicKey
353-
if (!jwtOpts || !key) return Promise.resolve()
352+
function authenticate (jwt, opts) {
353+
opts = opts ? Object.assign({}, jwtOpts, opts) : jwtOpts
354+
let key = (opts || {}).secret || (opts || {}).publicKey
355+
if (!opts || !key) return Promise.resolve()
354356
return new Promise((resolve, reject) => {
355-
jsonwebtoken.verify(jwt, key, jwtOpts, (err, d = {}) => {
357+
jsonwebtoken.verify(jwt, key, opts, (err, d = {}) => {
356358
return err ? reject(err) : resolve(d)
357359
})
358360
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scrud",
3-
"version": "4.2.7",
3+
"version": "4.2.8",
44
"description": "SCRUD API server, fast, light, capable",
55
"engines": {
66
"node": ">=6.0.0"

0 commit comments

Comments
 (0)