From a2c6fcf1031b9c0c4bb899321c1554d0b206c102 Mon Sep 17 00:00:00 2001 From: Nicolas Froidure Date: Sun, 3 Mar 2019 17:08:21 +0100 Subject: [PATCH] feat(@whook/swagger-ui): Allow to specify a dev token --- packages/whook-authorization/src/index.js | 2 +- .../src/__snapshots__/index.test.js.snap | 2 +- .../src/config/development/config.js | 2 + .../whook-example/src/handlers/getOpenAPI.js | 27 ++++++++++++- .../services/__snapshots__/API.test.js.snap | 26 ++++++++++++- .../src/__snapshots__/index.test.js.snap | 10 ++++- .../src/handlers/getOpenAPI.js | 5 +-- .../src/handlers/getOpenAPI.test.js | 2 +- packages/whook-swagger-ui/src/index.js | 38 +++++++++++++++++-- packages/whook-swagger-ui/src/index.test.js | 1 + 10 files changed, 100 insertions(+), 15 deletions(-) diff --git a/packages/whook-authorization/src/index.js b/packages/whook-authorization/src/index.js index 9af63fd3..4194a3f4 100644 --- a/packages/whook-authorization/src/index.js +++ b/packages/whook-authorization/src/index.js @@ -67,7 +67,7 @@ async function handleWithAuthorization( 'debug', '🔓 - Public endpoint detected, letting the call pass through!', ); - response = await handler(parameters, operation); + response = await handler({ parameters, authenticated: false }, operation); } else { const authorization = parameters.access_token ? `${DEFAULT_MECHANISM} ${parameters.access_token}` diff --git a/packages/whook-example/src/__snapshots__/index.test.js.snap b/packages/whook-example/src/__snapshots__/index.test.js.snap index 4af2982a..d7e7516d 100644 --- a/packages/whook-example/src/__snapshots__/index.test.js.snap +++ b/packages/whook-example/src/__snapshots__/index.test.js.snap @@ -397,7 +397,7 @@ Object { ], "logInfoCalls": Array [ Array [ - "💁 - Serving the API docs: http://localhost:9999/docs?url=http%3A%2F%2Flocalhost%3A9999%2Fv4%2FopenAPI", + "💁 - Serving the public API docs: http://localhost:9999/docs?url=http%3A%2F%2Flocalhost%3A9999%2Fv4%2FopenAPI", ], Array [ "🎙️ - HTTP Server listening at \\"http://localhost:9999\\".", diff --git a/packages/whook-example/src/config/development/config.js b/packages/whook-example/src/config/development/config.js index e2f8ca92..67dd8301 100644 --- a/packages/whook-example/src/config/development/config.js +++ b/packages/whook-example/src/config/development/config.js @@ -2,6 +2,8 @@ import COMMON_CONFIG from '../common/config'; const CONFIG = { ...COMMON_CONFIG, + DEV_ACCESS_TOKEN: '1-admin', + DEFAULT_MECHANISM: 'fake', }; export default CONFIG; diff --git a/packages/whook-example/src/handlers/getOpenAPI.js b/packages/whook-example/src/handlers/getOpenAPI.js index 7eab8b04..3a75e568 100644 --- a/packages/whook-example/src/handlers/getOpenAPI.js +++ b/packages/whook-example/src/handlers/getOpenAPI.js @@ -1,8 +1,31 @@ import getOpenAPI, { - definition, + definition as baseGetOpenAPIDefinition, } from '@whook/swagger-ui/dist/handlers/getOpenAPI'; // TODO: Use WHOOK_PLUGINS to get handlers from plugins // instead of proxying here -export { definition }; + +export const definition = { + ...baseGetOpenAPIDefinition, + operation: { + ...baseGetOpenAPIDefinition.operation, + parameters: [ + ...(baseGetOpenAPIDefinition.operation.parameters || []), + { + in: 'header', + name: 'authorization', + schema: { + type: 'string', + }, + }, + { + in: 'query', + name: 'access_token', + schema: { + type: 'string', + }, + }, + ], + }, +}; export default getOpenAPI; diff --git a/packages/whook-example/src/services/__snapshots__/API.test.js.snap b/packages/whook-example/src/services/__snapshots__/API.test.js.snap index 2c9b3eb8..fd46ceb2 100644 --- a/packages/whook-example/src/services/__snapshots__/API.test.js.snap +++ b/packages/whook-example/src/services/__snapshots__/API.test.js.snap @@ -238,6 +238,22 @@ Object { "/openAPI": Object { "get": Object { "operationId": "getOpenAPI", + "parameters": Array [ + Object { + "in": "header", + "name": "authorization", + "schema": Object { + "type": "string", + }, + }, + Object { + "in": "query", + "name": "access_token", + "schema": Object { + "type": "string", + }, + }, + ], "responses": Object { "200": Object { "content": Object { @@ -260,7 +276,15 @@ Object { }, "options": Object { "operationId": "optionsWithCORS", - "parameters": Array [], + "parameters": Array [ + Object { + "in": "query", + "name": "access_token", + "schema": Object { + "type": "string", + }, + }, + ], "responses": Object { "200": Object { "description": "CORS sent.", diff --git a/packages/whook-swagger-ui/src/__snapshots__/index.test.js.snap b/packages/whook-swagger-ui/src/__snapshots__/index.test.js.snap index cdb7127d..55db7b8d 100644 --- a/packages/whook-swagger-ui/src/__snapshots__/index.test.js.snap +++ b/packages/whook-swagger-ui/src/__snapshots__/index.test.js.snap @@ -243,7 +243,10 @@ Object { ], "logInfoCalls": Array [ Array [ - "💁 - Serving the API docs: http://localhost:8890/docs?url=http%3A%2F%2Flocalhost%3A8890%2Fv1%2FopenAPI", + "💁 - Serving the public API docs: http://localhost:8890/docs?url=http%3A%2F%2Flocalhost%3A8890%2Fv1%2FopenAPI", + ], + Array [ + "💁 - Serving the private API docs: http://localhost:8890/docs?url=http%3A%2F%2Flocalhost%3A8890%2Fv1%2FopenAPI%3Faccess_token%3Doudelali", ], Array [ "🎙️ - HTTP Server listening at \\"http://localhost:8890\\".", @@ -356,7 +359,10 @@ Object { ], "logInfoCalls": Array [ Array [ - "💁 - Serving the API docs: http://localhost:8888/docs?url=http%3A%2F%2Flocalhost%3A8888%2Fv1%2FopenAPI", + "💁 - Serving the public API docs: http://localhost:8888/docs?url=http%3A%2F%2Flocalhost%3A8888%2Fv1%2FopenAPI", + ], + Array [ + "💁 - Serving the private API docs: http://localhost:8888/docs?url=http%3A%2F%2Flocalhost%3A8888%2Fv1%2FopenAPI%3Faccess_token%3Doudelali", ], Array [ "🎙️ - HTTP Server listening at \\"http://localhost:8888\\".", diff --git a/packages/whook-swagger-ui/src/handlers/getOpenAPI.js b/packages/whook-swagger-ui/src/handlers/getOpenAPI.js index 4e88f32a..744eb111 100644 --- a/packages/whook-swagger-ui/src/handlers/getOpenAPI.js +++ b/packages/whook-swagger-ui/src/handlers/getOpenAPI.js @@ -26,9 +26,8 @@ export const definition = { }, }; -async function getOpenAPI({ API }, { userId }) { - const authenticatedRequest = !!userId; - if (authenticatedRequest) { +async function getOpenAPI({ API }, { authenticated = false }) { + if (authenticated) { return { status: 200, body: API, diff --git a/packages/whook-swagger-ui/src/handlers/getOpenAPI.test.js b/packages/whook-swagger-ui/src/handlers/getOpenAPI.test.js index 09acadb7..af12b948 100644 --- a/packages/whook-swagger-ui/src/handlers/getOpenAPI.test.js +++ b/packages/whook-swagger-ui/src/handlers/getOpenAPI.test.js @@ -42,7 +42,7 @@ describe('getOpenAPI', () => { API, }); const response = await getOpenAPI({ - userId: 1, + authenticated: true, }); expect({ diff --git a/packages/whook-swagger-ui/src/index.js b/packages/whook-swagger-ui/src/index.js index 58a7be7b..f5e1f876 100644 --- a/packages/whook-swagger-ui/src/index.js +++ b/packages/whook-swagger-ui/src/index.js @@ -1,6 +1,7 @@ import { wrapInitializer, alsoInject } from 'knifecycle'; import swaggerDist from 'swagger-ui-dist'; import ecstatic from 'ecstatic'; +import { definition as getOpenAPIDefinition } from './handlers/getOpenAPI'; /** * Wraps the `httpRouter` initializer to also serve the @@ -11,7 +12,15 @@ import ecstatic from 'ecstatic'; export default function wrapHTTPRouterWithSwaggerUI(initHTTPRouter) { return wrapInitializer( async ( - { DEBUG_NODE_ENVS, NODE_ENV, BASE_PATH, HOST, PORT, log = noop }, + { + DEBUG_NODE_ENVS, + NODE_ENV, + DEV_ACCESS_TOKEN, + BASE_PATH, + HOST, + PORT, + log = noop, + }, httpRouter, ) => { if (!DEBUG_NODE_ENVS.includes(NODE_ENV)) { @@ -20,7 +29,9 @@ export default function wrapHTTPRouterWithSwaggerUI(initHTTPRouter) { const localURL = `http://${HOST}:${PORT}`; const swaggerUIURL = `${localURL}/docs`; - const publicSwaggerURL = `${localURL}${BASE_PATH || ''}/openAPI`; + const publicSwaggerURL = `${localURL}${BASE_PATH || ''}${ + getOpenAPIDefinition.path + }`; const staticRouter = ecstatic({ root: swaggerDist.absolutePath(), showdir: false, @@ -29,11 +40,22 @@ export default function wrapHTTPRouterWithSwaggerUI(initHTTPRouter) { log( 'info', - `💁 - Serving the API docs: ${swaggerUIURL}?url=${encodeURIComponent( + `💁 - Serving the public API docs: ${swaggerUIURL}?url=${encodeURIComponent( publicSwaggerURL, )}`, ); + if (DEV_ACCESS_TOKEN) { + log( + 'info', + `💁 - Serving the private API docs: ${swaggerUIURL}?url=${encodeURIComponent( + publicSwaggerURL + + '?access_token=' + + encodeURIComponent(DEV_ACCESS_TOKEN), + )}`, + ); + } + return { ...httpRouter, service: customHTTPRouter, @@ -47,7 +69,15 @@ export default function wrapHTTPRouterWithSwaggerUI(initHTTPRouter) { } }, alsoInject( - ['DEBUG_NODE_ENVS', 'NODE_ENV', 'BASE_PATH', 'HOST', 'PORT', '?log'], + [ + 'DEBUG_NODE_ENVS', + 'NODE_ENV', + '?DEV_ACCESS_TOKEN', + 'BASE_PATH', + 'HOST', + 'PORT', + '?log', + ], initHTTPRouter, ), ); diff --git a/packages/whook-swagger-ui/src/index.test.js b/packages/whook-swagger-ui/src/index.test.js index 71b8fd83..2a0f8bab 100644 --- a/packages/whook-swagger-ui/src/index.test.js +++ b/packages/whook-swagger-ui/src/index.test.js @@ -80,6 +80,7 @@ describe('wrapHTTPRouterWithSwaggerUI', () => { $.register(constant('API', API)); $.register(constant('ENV', {})); $.register(constant('NODE_ENV', 'test')); + $.register(constant('DEV_ACCESS_TOKEN', 'oudelali')); $.register(constant('HOST', HOST)); $.register(constant('WRAPPERS', [])); $.register(