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

Commit

Permalink
Make oas.prepareSecurity() work for && and || cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Oct 25, 2017
1 parent 878712b commit 22f813e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@
}]
}
},
"/and-security": {
"post": {
"security": [{
"oauth": ["write:things"],
"apiKey": []
}]
}
},
"/and-or-security": {
"post": {
"security": [{
"oauth": ["write:things"],
"apiKey": []
},
{
"oauthDiff": ["write:things"]
}]
}
},
"/single-auth": {
"post": {
"security": [{
Expand Down
15 changes: 14 additions & 1 deletion packages/api-explorer-ui/__tests__/lib/Oas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,25 @@ describe('operation.prepareSecurity()', () => {
expect(operation.prepareSecurity()).toMatchSnapshot();
});

test('should work for multiple securities', () => {
test('should work for multiple securities (||)', () => {
const operation = new Oas(multipleSecurities).operation('/things', 'post');

expect(Object.keys(operation.prepareSecurity()).length).toBe(2);
});

test('should work for multiple securities (&&)', () => {
const operation = new Oas(multipleSecurities).operation('/and-security', 'post');

expect(Object.keys(operation.prepareSecurity()).length).toBe(2);
});

test('should work for multiple securities (&& and ||)', () => {
const operation = new Oas(multipleSecurities).operation('/and-or-security', 'post');

expect(operation.prepareSecurity().OAuth2.length).toBe(2);
expect(operation.prepareSecurity().Header.length).toBe(1);
});

test('should set a `key` property');

// TODO We dont currently support cookies?
Expand Down
50 changes: 30 additions & 20 deletions packages/api-explorer-ui/src/lib/Oas.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,44 @@ class Operation {

return securityRequirements
.map(requirement => {
let security;
let key;
let keys;
try {
key = Object.keys(requirement)[0];
security = this.oas.components.securitySchemes[key];
keys = Object.keys(requirement);
} catch (e) {
return false;
}

if (!security) return false;
let type = security.type;
if (security.type === 'http' && security.scheme === 'basic') {
type = 'Basic';
} else if (security.type === 'oauth2') {
type = 'OAuth2';
} else if (security.type === 'apiKey' && security.in === 'query') {
type = 'Query';
} else if (security.type === 'apiKey' && security.in === 'header') {
type = 'Header';
}
return keys.map((key) => {
let security;
try {
security = this.oas.components.securitySchemes[key];
} catch (e) {
return false;
}

if (!security) return false;
let type = security.type;
if (security.type === 'http' && security.scheme === 'basic') {
type = 'Basic';
} else if (security.type === 'oauth2') {
type = 'OAuth2';
} else if (security.type === 'apiKey' && security.in === 'query') {
type = 'Query';
} else if (security.type === 'apiKey' && security.in === 'header') {
type = 'Header';
}

security._key = key;

security._key = key;
return { type, security };
return { type, security };
});
})
.filter(Boolean)
.reduce((prev, next) => {
if (!prev[next.type]) prev[next.type] = [];
prev[next.type].push(next.security);
.reduce((prev, securities) => {
securities.forEach((security) => {
if (!prev[security.type]) prev[security.type] = [];
prev[security.type].push(security.security);
});
return prev;
}, {});
}
Expand Down

0 comments on commit 22f813e

Please sign in to comment.