From 2ada0535965e4385e0e734e7d90c74c8c3275c78 Mon Sep 17 00:00:00 2001 From: dennis Date: Fri, 19 Oct 2018 15:17:44 +0900 Subject: [PATCH 1/3] Fix displaying auth scope --- lib/index.js | 13 +++++++++++-- test/index.js | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 651f943..fc10e38 100644 --- a/lib/index.js +++ b/lib/index.js @@ -121,8 +121,17 @@ internals.connectionInfo = function (server, routes, options, connectionInfo) { const defaultStrategy = Hoek.reach(server, 'auth.settings.default.strategies'); let authStrategy = route.settings.auth ? route.settings.auth.strategies.toString() : false; - const authScope = (route.settings.auth && route.settings.auth.access) ? - route.settings.auth.access[0].scope.selection.toString() : false; + let authScope = false; + if (route.settings.auth && route.settings.auth.access) { + const required = route.settings.auth.access[0].scope.required ? + `required: ${route.settings.auth.access[0].scope.required.toString()} ` : ''; + const forbidden = route.settings.auth.access[0].scope.forbidden ? + `forbidden: ${route.settings.auth.access[0].scope.forbidden.toString()} ` : ''; + const selection = route.settings.auth.access[0].scope.selection ? + `selection: ${route.settings.auth.access[0].scope.selection.toString()}` : ''; + + authScope = `${required}${forbidden}${selection}`; + } if (route.settings.auth === undefined) { authStrategy = defaultStrategy ? String(defaultStrategy) : false; diff --git a/test/index.js b/test/index.js index e547e9b..e11e336 100644 --- a/test/index.js +++ b/test/index.js @@ -46,7 +46,7 @@ const internals = { { method: 'GET', path: '/all', description: 'a route on all connections', auth: false, scope: false }, { method: 'GET', path: '/api', description: 'api routes', auth: false, scope: false }, { method: 'POST', path: '/apost/{foo}/comment/{another}', description: '', auth: false, scope: false }, - { method: 'GET', path: '/hi', description: '', auth: 'findme', scope: 'tester' }, + { method: 'GET', path: '/hi', description: '', auth: 'findme', scope: 'required: tester1 forbidden: tester3 selection: tester2' }, { method: 'DELETE', path: '/post/{id}', description: '', auth: false, scope: false } ] }], @@ -122,7 +122,7 @@ internals.prepareServer = async function (options) { const authOptions = options.authType ? { strategy: 'findme', - scope: ['tester'] + scope: ['+tester1', 'tester2', '!tester3'] } : false; plugin.route({ From 38ddb6d7ed1d68f072f27263dbf1edf90d9ccb68 Mon Sep 17 00:00:00 2001 From: dennis Date: Mon, 19 Nov 2018 00:26:39 +0900 Subject: [PATCH 2/3] modify printing scope information format and add test cases --- lib/index.js | 32 +++++++++----- test/index.js | 119 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 135 insertions(+), 16 deletions(-) diff --git a/lib/index.js b/lib/index.js index fc10e38..b3226b8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -121,16 +121,28 @@ internals.connectionInfo = function (server, routes, options, connectionInfo) { const defaultStrategy = Hoek.reach(server, 'auth.settings.default.strategies'); let authStrategy = route.settings.auth ? route.settings.auth.strategies.toString() : false; - let authScope = false; - if (route.settings.auth && route.settings.auth.access) { - const required = route.settings.auth.access[0].scope.required ? - `required: ${route.settings.auth.access[0].scope.required.toString()} ` : ''; - const forbidden = route.settings.auth.access[0].scope.forbidden ? - `forbidden: ${route.settings.auth.access[0].scope.forbidden.toString()} ` : ''; - const selection = route.settings.auth.access[0].scope.selection ? - `selection: ${route.settings.auth.access[0].scope.selection.toString()}` : ''; - - authScope = `${required}${forbidden}${selection}`; + let authScope = []; + + const required = Hoek.reach(route, 'settings.auth.access.0.scope.required', { default: '' }); + if (required !== '' ) { + authScope.push(required.map((item) => `+${item}`).join(', ')); + } + + const forbidden = Hoek.reach(route, 'settings.auth.access.0.scope.forbidden', { default: '' }); + if (forbidden !== '' ) { + authScope.push(forbidden.map((item) => `!${item}`).join(', ')); + } + + const selection = Hoek.reach(route, 'settings.auth.access.0.scope.selection', { default: '' }); + if (selection !== '') { + authScope.push(selection.join(', ')); + } + + if (authScope.length > 0) { + authScope = authScope.join(', '); + } + else { + authScope = false; } if (route.settings.auth === undefined) { diff --git a/test/index.js b/test/index.js index e11e336..1af032c 100644 --- a/test/index.js +++ b/test/index.js @@ -46,7 +46,37 @@ const internals = { { method: 'GET', path: '/all', description: 'a route on all connections', auth: false, scope: false }, { method: 'GET', path: '/api', description: 'api routes', auth: false, scope: false }, { method: 'POST', path: '/apost/{foo}/comment/{another}', description: '', auth: false, scope: false }, - { method: 'GET', path: '/hi', description: '', auth: 'findme', scope: 'required: tester1 forbidden: tester3 selection: tester2' }, + { method: 'GET', path: '/hi', description: '', auth: 'findme', scope: '+tester1, !tester3, tester2' }, + { method: 'DELETE', path: '/post/{id}', description: '', auth: false, scope: false } + ] + }], + requiredScopeResult: [{ + routes: [ + { method: 'GET', path: '/', description: 'main index', auth: false, scope: false }, + { method: 'GET', path: '/all', description: 'a route on all connections', auth: false, scope: false }, + { method: 'GET', path: '/api', description: 'api routes', auth: false, scope: false }, + { method: 'POST', path: '/apost/{foo}/comment/{another}', description: '', auth: false, scope: false }, + { method: 'GET', path: '/hi', description: '', auth: 'findme', scope: '+tester1' }, + { method: 'DELETE', path: '/post/{id}', description: '', auth: false, scope: false } + ] + }], + forbiddenScopeResult: [{ + routes: [ + { method: 'GET', path: '/', description: 'main index', auth: false, scope: false }, + { method: 'GET', path: '/all', description: 'a route on all connections', auth: false, scope: false }, + { method: 'GET', path: '/api', description: 'api routes', auth: false, scope: false }, + { method: 'POST', path: '/apost/{foo}/comment/{another}', description: '', auth: false, scope: false }, + { method: 'GET', path: '/hi', description: '', auth: 'findme', scope: '!tester3' }, + { method: 'DELETE', path: '/post/{id}', description: '', auth: false, scope: false } + ] + }], + selectionScopeResult: [{ + routes: [ + { method: 'GET', path: '/', description: 'main index', auth: false, scope: false }, + { method: 'GET', path: '/all', description: 'a route on all connections', auth: false, scope: false }, + { method: 'GET', path: '/api', description: 'api routes', auth: false, scope: false }, + { method: 'POST', path: '/apost/{foo}/comment/{another}', description: '', auth: false, scope: false }, + { method: 'GET', path: '/hi', description: '', auth: 'findme', scope: 'tester2' }, { method: 'DELETE', path: '/post/{id}', description: '', auth: false, scope: false } ] }], @@ -120,10 +150,26 @@ internals.prepareServer = async function (options) { } }); - const authOptions = options.authType ? { - strategy: 'findme', - scope: ['+tester1', 'tester2', '!tester3'] - } : false; + let authOptions = false; + if (options.authType) { + authOptions = { + strategy: 'findme' + }; + switch (options.scopeType) { + case 'required': + authOptions.scope = ['+tester1']; + break; + case 'selection': + authOptions.scope = ['tester2']; + break; + case 'forbidden': + authOptions.scope = ['!tester3']; + break; + default: + authOptions.scope = ['+tester1', 'tester2', '!tester3']; + break; + } + } plugin.route({ method: 'GET', @@ -245,7 +291,7 @@ describe('routes', () => { expect(text).to.match(/hi.*findme/); }); - it('gets route information with scope', async () => { + it('gets route information with all scope', async () => { const blippOptions = { showAuth: true, @@ -265,6 +311,67 @@ describe('routes', () => { expect(text).to.match(/hi.*findme/); }); + it('gets route information with required scope', async () => { + + const blippOptions = { + showAuth: true, + showScope: true, + showStart: false + }; + + const server = await internals.prepareServer({ blippOptions, authType: 'findme', scopeType: 'required' }); + + const info = server.plugins[Pkg.name].info(); + delete info[0].uri; + expect(info).to.equal(internals.requiredScopeResult); + + const text = server.plugins[Pkg.name].text(); + expect(text).to.match(/none.*main index/); + expect(text).to.match(/none.*api routes/); + expect(text).to.match(/hi.*findme/); + }); + + it('gets route information with selection scope', async () => { + + const blippOptions = { + showAuth: true, + showScope: true, + showStart: false + }; + + const server = await internals.prepareServer({ blippOptions, authType: 'findme', scopeType: 'selection' }); + + const info = server.plugins[Pkg.name].info(); + delete info[0].uri; + expect(info).to.equal(internals.selectionScopeResult); + + const text = server.plugins[Pkg.name].text(); + expect(text).to.match(/none.*main index/); + expect(text).to.match(/none.*api routes/); + expect(text).to.match(/hi.*findme/); + }); + + it('gets route information with forbidden scope', async () => { + + const blippOptions = { + showAuth: true, + showScope: true, + showStart: false + }; + + const server = await internals.prepareServer({ blippOptions, authType: 'findme', scopeType: 'forbidden' }); + + const info = server.plugins[Pkg.name].info(); + delete info[0].uri; + expect(info).to.equal(internals.forbiddenScopeResult); + + const text = server.plugins[Pkg.name].text(); + expect(text).to.match(/none.*main index/); + expect(text).to.match(/none.*api routes/); + expect(text).to.match(/hi.*findme/); + }); + + it('gets route information with default', async () => { const blippOptions = { From 60fa7e434228267cee7eafafc8173731b7729f16 Mon Sep 17 00:00:00 2001 From: dennis Date: Mon, 19 Nov 2018 00:40:14 +0900 Subject: [PATCH 3/3] ignore queueMicrotask leak --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c3f4843..34eb6af 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "blipp is a simple hapi plugin to display the routes table at startup", "main": "lib/index.js", "scripts": { - "test": "lab -a code -v -t 100 -L" + "test": "lab -a code -v -t 100 -L -I queueMicrotask" }, "repository": { "type": "git",