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

Commit

Permalink
Do not add auth headers if there is no value in the inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Sep 8, 2017
1 parent 6c5b88d commit 02debab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/api-explorer-ui/__tests__/lib/configure-security.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ describe('configure-security', () => {
},
});
});

test('should return with no header if user and password are blank', () => {
const values = {
auth: { test: { user: '', password: '' } },
};

expect(configureSecurity({
components: { securitySchemes: { test: { type: 'basic' } } },
}, values, { test: {} })).toEqual(false);
});
});

describe('type=oauth2', () => {
Expand All @@ -60,6 +70,16 @@ describe('configure-security', () => {
},
});
});

test('should return with no header if apiKey is blank', () => {
const values = {
auth: { test: '' },
};

expect(configureSecurity({
components: { securitySchemes: { test: { type: 'oauth2' } } },
}, values, { test: {} })).toEqual(false);
});
});

describe('type=apiKey', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/api-explorer-ui/src/lib/configure-security.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module.exports = function configureSecurity(oas, values, scheme) {
const security = oas.components.securitySchemes[key];

if (security.type === 'basic') {
// Return with no header if user and password are blank
if (!(values.auth[key].user && values.auth[key].password)) return false;

return harValue('headers', {
name: 'Authorization',
value: `Basic ${new Buffer(`${values.auth[key].user}:${values.auth[key].password}`).toString('base64')}`,
Expand Down Expand Up @@ -43,6 +46,8 @@ module.exports = function configureSecurity(oas, values, scheme) {
}

if (security.type === 'oauth2') {
if (!values.auth[key]) return false;

return harValue('headers', {
name: 'Authorization',
value: `Bearer ${values.auth[key]}`,
Expand Down

0 comments on commit 02debab

Please sign in to comment.