Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Add auth to oas-to-har
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Sep 6, 2017
1 parent 0f93181 commit 44f950e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 34 deletions.
56 changes: 35 additions & 21 deletions packages/api-explorer-ui/__tests__/lib/configure-security.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')}`,
},
});
});
});
Expand All @@ -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}`,
},
});
});
});
Expand All @@ -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,
},
});
});
});
Expand All @@ -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,
},
});
});

Expand All @@ -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}`,
},
});
});

Expand All @@ -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}`,
},
});
});

Expand All @@ -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}`,
},
});
});
});
Expand Down
58 changes: 58 additions & 0 deletions packages/api-explorer-ui/__tests__/lib/oas-to-har.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
26 changes: 13 additions & 13 deletions packages/api-explorer-ui/src/lib/configure-security.js
Original file line number Diff line number Diff line change
@@ -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 {};
Expand All @@ -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']) {
Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions packages/api-explorer-ui/src/lib/oas-to-har.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const getSchema = require('./get-schema');
const configureSecurity = require('./configure-security');

// const format = {
// value: v => `__START_VALUE__${v}__END__`,
Expand Down Expand Up @@ -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;
};

0 comments on commit 44f950e

Please sign in to comment.