diff --git a/packages/api-explorer-ui/__tests__/lib/configure-security.test.js b/packages/api-explorer-ui/__tests__/lib/configure-security.test.js index 800a0234b..3ba77da90 100644 --- a/packages/api-explorer-ui/__tests__/lib/configure-security.test.js +++ b/packages/api-explorer-ui/__tests__/lib/configure-security.test.js @@ -16,9 +16,11 @@ describe('configure-security', () => { expect(configureSecurity({ components: { securitySchemes: { test: { type: 'basic' } } }, }, values, { test: {} })).toEqual({ - type: 'header', - name: 'Authorization', - value: `Basic ${new Buffer(`${user}:${password}`).toString('base64')}`, + type: 'headers', + value: { + name: 'Authorization', + value: `Basic ${new Buffer(`${user}:${password}`).toString('base64')}`, + }, }); }); }); @@ -33,9 +35,11 @@ describe('configure-security', () => { expect(configureSecurity({ components: { securitySchemes: { test: { type: 'oauth2' } } }, }, values, { test: {} })).toEqual({ - type: 'header', - name: 'Authorization', - value: `Bearer ${apiKey}`, + type: 'headers', + value: { + name: 'Authorization', + value: `Bearer ${apiKey}`, + }, }); }); }); @@ -49,9 +53,11 @@ describe('configure-security', () => { expect(configureSecurity({ components: { securitySchemes: { test: security } }, }, values, { test: {} })).toEqual({ - type: 'query', - name: security.name, - value: values.auth[security.name], + type: 'queryString', + value: { + name: security.name, + value: values.auth.test, + }, }); }); }); @@ -64,9 +70,11 @@ describe('configure-security', () => { expect(configureSecurity({ components: { securitySchemes: { test: security } }, }, values, { test: {} })).toEqual({ - type: 'header', - name: security.name, - value: values.auth[security.name], + type: 'headers', + value: { + name: security.name, + value: values.auth.test, + }, }); }); @@ -78,9 +86,11 @@ describe('configure-security', () => { expect(configureSecurity({ components: { securitySchemes: { test: security } }, }, values, { test: {} })).toEqual({ - type: 'header', - name: security.name, - value: `Bearer ${values.auth[security.name]}`, + type: 'headers', + value: { + name: security.name, + value: `Bearer ${values.auth.test}`, + }, }); }); @@ -91,9 +101,11 @@ describe('configure-security', () => { expect(configureSecurity({ components: { securitySchemes: { test: security } }, }, values, { test: {} })).toEqual({ - type: 'header', - name: security.name, - value: `Basic ${values.auth[security.name]}`, + type: 'headers', + value: { + name: security.name, + value: `Basic ${values.auth.test}`, + }, }); }); @@ -104,9 +116,11 @@ describe('configure-security', () => { expect(configureSecurity({ components: { securitySchemes: { test: security } }, }, values, { test: {} })).toEqual({ - type: 'header', - name: security.name, - value: `Token ${values.auth[security.name]}`, + type: 'headers', + value: { + name: security.name, + value: `Token ${values.auth.test}`, + }, }); }); }); diff --git a/packages/api-explorer-ui/__tests__/lib/oas-to-har.test.js b/packages/api-explorer-ui/__tests__/lib/oas-to-har.test.js index 0384fa5d7..3f2557ed0 100644 --- a/packages/api-explorer-ui/__tests__/lib/oas-to-har.test.js +++ b/packages/api-explorer-ui/__tests__/lib/oas-to-har.test.js @@ -255,3 +255,61 @@ describe('body values', () => { }); describe('form data values', () => {}); + +describe('auth', () => { + test('should work for header', () => { + expect(oasToHar({ + components: { + securitySchemes: { + 'auth-header': { + type: 'apiKey', + name: 'x-auth-header', + in: 'header', + }, + }, + }, + }, { + path: '/security', + method: 'get', + security: [ + { 'auth-header': [] }, + ], + }, { + auth: { + 'auth-header': 'value', + }, + }).headers).toEqual([{ + name: 'x-auth-header', + value: 'value', + }]); + }); + + test('should work for query', () => { + expect(oasToHar({ + components: { + securitySchemes: { + 'auth-query': { + type: 'apiKey', + name: 'authQuery', + in: 'query', + }, + }, + }, + }, { + path: '/security', + method: 'get', + security: [ + { 'auth-query': [] }, + ], + }, { + auth: { + 'auth-query': 'value', + }, + }).queryString).toEqual([{ + name: 'authQuery', + value: 'value', + }]); + }); + + test('should work for multiple'); +}); diff --git a/packages/api-explorer-ui/src/lib/configure-security.js b/packages/api-explorer-ui/src/lib/configure-security.js index 078206097..c8869c00f 100644 --- a/packages/api-explorer-ui/src/lib/configure-security.js +++ b/packages/api-explorer-ui/src/lib/configure-security.js @@ -1,3 +1,7 @@ +function harValue(type, value) { + return { type, value }; +} + module.exports = function configureSecurity(oas, values, scheme) { const key = Object.keys(scheme)[0]; if (!key) return {}; @@ -6,26 +10,23 @@ module.exports = function configureSecurity(oas, values, scheme) { const security = oas.components.securitySchemes[key]; if (security.type === 'basic') { - return { - type: 'header', + return harValue('headers', { name: 'Authorization', value: `Basic ${new Buffer(`${values.auth.user}:${values.auth.password}`).toString('base64')}`, - }; + }); } if (security.type === 'apiKey') { if (security.in === 'query') { - return { - type: 'query', + return harValue('queryString', { name: security.name, - value: values.auth[security.name], - }; + value: values.auth[key], + }); } if (security.in === 'header') { const header = { - type: 'header', name: security.name, - value: values.auth[security.name], + value: values.auth[key], }; if (security['x-bearer-format']) { @@ -34,16 +35,15 @@ module.exports = function configureSecurity(oas, values, scheme) { header.name = security.name; header.value = `${bearerFormat} ${header.value}`; } - return header; + return harValue('headers', header); } } if (security.type === 'oauth2') { - return { - type: 'header', + return harValue('headers', { name: 'Authorization', value: `Bearer ${values.auth}`, - }; + }); } return undefined; diff --git a/packages/api-explorer-ui/src/lib/oas-to-har.js b/packages/api-explorer-ui/src/lib/oas-to-har.js index cef5fd1e6..df6165eec 100644 --- a/packages/api-explorer-ui/src/lib/oas-to-har.js +++ b/packages/api-explorer-ui/src/lib/oas-to-har.js @@ -1,4 +1,5 @@ const getSchema = require('./get-schema'); +const configureSecurity = require('./configure-security'); // const format = { // value: v => `__START_VALUE__${v}__END__`, @@ -79,5 +80,16 @@ module.exports = (oas, pathOperation = { path: '', method: '' }, values = {}) => har.postData.text = JSON.stringify(formData.body); } + const securityRequirements = pathOperation.security || oas.security; + + if (securityRequirements && securityRequirements.length) { + // TODO pass these values through the formatter? + securityRequirements.forEach((security) => { + const securityValue = configureSecurity(oas, formData, security); + + har[securityValue.type].push(securityValue.value); + }); + } + return har; };