Skip to content

Commit ffef2ce

Browse files
committed
allow buffer for publicKey
1 parent 67ce529 commit ffef2ce

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

src/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const scheme = joi.object({
1111
realmUrl: joi.string().uri().required(),
1212
clientId: joi.string().min(1).required(),
1313
secret: joi.string().min(1),
14-
publicKey: joi.string().regex(/^-----BEGIN(?: RSA)? PUBLIC KEY-----[\s\S]*-----END(?: RSA)? PUBLIC KEY-----$/ig, 'PEM'),
14+
publicKey: joi.alternatives().try(
15+
joi.string().regex(/^-----BEGIN(?: RSA)? PUBLIC KEY-----[\s\S]*-----END(?: RSA)? PUBLIC KEY-----\s?$/ig, 'PEM'),
16+
joi.object().type(Buffer)
17+
),
1518
verifyOpts: joi.object({
1619
ignoreExpiration: joi.any().forbidden(),
1720
ignoreNotBefore: joi.any().forbidden()

test/fixtures/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const introspectPath = '/protocol/openid-connect/token/introspect'
88
const realmUrl = `${baseUrl}${realmPath}`
99
const clientId = 'foobar'
1010
const secret = '1234-bar-4321-foo'
11+
const publicKeyBuffer = fs.readFileSync('./test/fixtures/public.pem')
1112
const publicKey = `-----BEGIN RSA PUBLIC KEY-----
1213
MIICCgKCAgEAur96MoQa/blg5eJFqmN//V4oQjKaBJl6KEvWSGAVgRm2PsnFKzCJ
1314
K9aJrUjETS473/x6fAHXyF5QKun6avNxpWs2VzwlO4t8Bi2EpSW2w0led2OzR/MF
@@ -42,6 +43,7 @@ const clientConfig = {
4243
*/
4344
const common = Object.assign({}, clientConfig, {
4445
publicKey,
46+
publicKeyBuffer,
4547
baseUrl,
4648
token,
4749
realmPath,

test/utils.spec.js

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ test('throw error if options are invalid – userInfo', (t) => {
235235
})
236236
})
237237

238-
test('throw no error if options are valid', (t) => {
238+
test('throw no error if options are valid – secret', (t) => {
239239
const valids = [
240240
{},
241241
{ cache: {} },
@@ -251,11 +251,11 @@ test('throw no error if options are valid', (t) => {
251251
valids.forEach((valid) => {
252252
t.notThrows(
253253
() => utils.verify(helpers.getOptions(valid)),
254-
Error, helpers.log('valid', valid))
254+
Error, helpers.log('valid.secret', valid))
255255
})
256256
})
257257

258-
test('throw no error if options are valid', (t) => {
258+
test('throw no error if options are valid – publicKey/string', (t) => {
259259
const valids = [
260260
{},
261261
{ verifyOpts: undefined },
@@ -277,6 +277,58 @@ test('throw no error if options are valid', (t) => {
277277
secret: undefined,
278278
publicKey: fixtures.common.publicKey
279279
}, valid))),
280-
Error, helpers.log('valid with publicKey', valid))
280+
Error, helpers.log('valid.publicKey.string', valid))
281+
})
282+
})
283+
284+
test('throw no error if options are valid – publicKey/Buffer', (t) => {
285+
const valids = [
286+
{},
287+
{ verifyOpts: undefined },
288+
{ verifyOpts: {} },
289+
{ verifyOpts: { audience: 'foobar' } },
290+
{ cache: {} },
291+
{ cache: { segment: 'foobar' } },
292+
{ cache: true },
293+
{ cache: false },
294+
{ userInfo: [] },
295+
{ userInfo: ['string'] }
296+
]
297+
298+
t.plan(valids.length)
299+
300+
valids.forEach((valid) => {
301+
t.notThrows(
302+
() => utils.verify(helpers.getOptions(Object.assign({
303+
secret: undefined,
304+
publicKey: fixtures.common.publicKeyBuffer
305+
}, valid))),
306+
Error, helpers.log('valid.publicKey.Buffer', valid))
307+
})
308+
})
309+
310+
test('throw no error if options are valid – publicKey/Buffer/string', (t) => {
311+
const valids = [
312+
{},
313+
{ verifyOpts: undefined },
314+
{ verifyOpts: {} },
315+
{ verifyOpts: { audience: 'foobar' } },
316+
{ cache: {} },
317+
{ cache: { segment: 'foobar' } },
318+
{ cache: true },
319+
{ cache: false },
320+
{ userInfo: [] },
321+
{ userInfo: ['string'] }
322+
]
323+
324+
t.plan(valids.length)
325+
326+
valids.forEach((valid) => {
327+
t.notThrows(
328+
() => utils.verify(helpers.getOptions(Object.assign({
329+
secret: undefined,
330+
publicKey: fixtures.common.publicKeyBuffer.toString()
331+
}, valid))),
332+
Error, helpers.log('valid.publicKey.Buffer.string', valid))
281333
})
282334
})

0 commit comments

Comments
 (0)