Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit b5ee894

Browse files
committed
feat(oas3): expand oauth scheme requirement into it's flows
1 parent 767ecf8 commit b5ee894

File tree

3 files changed

+66
-18
lines changed

3 files changed

+66
-18
lines changed

packages/fury-adapter-oas3-parser/lib/parser/oas/parseSecurityRequirementObject.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,30 @@ function parseSecurityRequirementObject(context, object) {
4747
let e;
4848
const schemeName = key.toValue();
4949

50-
if (!context.hasScheme(schemeName)) {
51-
parseResult.push(createWarning(namespace, `'${schemeName}' security scheme not found`, key));
50+
const scopes = value.map(scope => scope.toValue());
51+
52+
if (scopes.length) {
53+
e = new namespace.elements.AuthScheme({ scopes });
5254
} else {
53-
const scopes = value.map(scope => scope.toValue());
55+
e = new namespace.elements.AuthScheme({});
56+
}
57+
58+
// Expand oauth2 flows
59+
const hasFlows = context.state.oauthFlows[schemeName] || [];
5460

55-
if (scopes.length) {
56-
e = new namespace.elements.AuthScheme({ scopes });
57-
} else {
58-
e = new namespace.elements.AuthScheme({});
59-
}
61+
if (hasFlows.length !== 0) {
62+
hasFlows.forEach((flow) => {
63+
const element = e.clone();
64+
element.element = flow;
65+
array.push(element);
66+
});
6067

68+
return;
69+
}
70+
71+
if (!context.hasScheme(schemeName)) {
72+
parseResult.push(createWarning(namespace, `'${schemeName}' security scheme not found`, key));
73+
} else {
6174
e.element = schemeName;
6275
array.push(e);
6376
}

packages/fury-adapter-oas3-parser/lib/state.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class State {
1616
}
1717

1818
oauthFlow(id, flow) {
19-
this.oauthFlows[id] = this.oauthFlows[id] || [];
20-
this.oauthFlows[id].push(flow);
19+
this.oauthFlows[id] = this.oauthFlows[id] || new Set();
20+
this.oauthFlows[id].add(flow);
2121

2222
return this.registerScheme(flow);
2323
}

packages/fury-adapter-oas3-parser/test/unit/parser/oas/parseSecurityRequirementObject-test.js

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ describe('Security Requirement Object', () => {
1010
beforeEach(() => {
1111
context = new Context(namespace);
1212
context.registerScheme('customApiKey');
13-
context.registerScheme('customOauth2');
13+
context.registerScheme('customOauth');
14+
context.oauthFlow('oauthWithFlow', 'oauthWithFlow implicit');
15+
context.oauthFlow('oauthWithFlow', 'oauthWithFlow authorization code');
1416
});
1517

1618
it('provides warning when security requirement is not an object', () => {
@@ -52,7 +54,7 @@ describe('Security Requirement Object', () => {
5254

5355
it('parses correctly a single scheme reference with scopes', () => {
5456
const securityRequirement = new namespace.elements.Object({
55-
customOauth2: [
57+
customOauth: [
5658
'scope1',
5759
'scope2',
5860
],
@@ -66,7 +68,7 @@ describe('Security Requirement Object', () => {
6668
const arr = parseResult.get(0);
6769

6870
expect(arr.length).to.equal(1);
69-
expect(arr.get(0).element).to.equal('customOauth2');
71+
expect(arr.get(0).element).to.equal('customOauth');
7072
expect(arr.get(0).length).to.equal(1);
7173

7274
const scopes = arr.get(0).get(0).value;
@@ -79,7 +81,7 @@ describe('Security Requirement Object', () => {
7981

8082
it('provides warning when scope is not a string', () => {
8183
const securityRequirement = new namespace.elements.Object({
82-
customOauth2: [
84+
customOauth: [
8385
'scope1',
8486
2,
8587
],
@@ -93,7 +95,7 @@ describe('Security Requirement Object', () => {
9395
const arr = parseResult.get(0);
9496

9597
expect(arr.length).to.equal(1);
96-
expect(arr.get(0).element).to.equal('customOauth2');
98+
expect(arr.get(0).element).to.equal('customOauth');
9799
expect(arr.get(0).length).to.equal(1);
98100

99101
const scopes = arr.get(0).get(0).value;
@@ -102,13 +104,13 @@ describe('Security Requirement Object', () => {
102104
expect(scopes.length).to.equal(1);
103105
expect(scopes.get(0).toValue()).to.equal('scope1');
104106

105-
expect(parseResult).to.contain.warning("'Security Requirement Object' 'customOauth2' array value is not a string");
107+
expect(parseResult).to.contain.warning("'Security Requirement Object' 'customOauth' array value is not a string");
106108
});
107109

108110
it('parses correctly multi scheme references', () => {
109111
const securityRequirement = new namespace.elements.Object({
110112
customApiKey: [],
111-
customOauth2: [],
113+
customOauth: [],
112114
});
113115

114116
const parseResult = parse(context, securityRequirement);
@@ -121,7 +123,40 @@ describe('Security Requirement Object', () => {
121123
expect(arr.length).to.equal(2);
122124
expect(arr.get(0).element).to.equal('customApiKey');
123125
expect(arr.get(0).length).to.equal(0);
124-
expect(arr.get(1).element).to.equal('customOauth2');
126+
expect(arr.get(1).element).to.equal('customOauth');
125127
expect(arr.get(1).length).to.equal(0);
126128
});
129+
130+
it('parses correctly oauth2 scheme with flows', () => {
131+
let scopes;
132+
const securityRequirement = new namespace.elements.Object({
133+
oauthWithFlow: [
134+
'scope',
135+
],
136+
});
137+
138+
const parseResult = parse(context, securityRequirement);
139+
140+
expect(parseResult.length).to.equal(1);
141+
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Array);
142+
143+
const arr = parseResult.get(0);
144+
145+
expect(arr.length).to.equal(2);
146+
expect(arr.get(0).element).to.equal('oauthWithFlow implicit');
147+
expect(arr.get(0).length).to.equal(1);
148+
149+
scopes = arr.get(0).get(0).value;
150+
expect(scopes).to.be.instanceof(namespace.elements.Array);
151+
expect(scopes.length).to.equal(1);
152+
expect(scopes.get(0).toValue()).to.equal('scope');
153+
154+
expect(arr.get(1).element).to.equal('oauthWithFlow authorization code');
155+
expect(arr.get(1).length).to.equal(1);
156+
157+
scopes = arr.get(1).get(0).value;
158+
expect(scopes).to.be.instanceof(namespace.elements.Array);
159+
expect(scopes.length).to.equal(1);
160+
expect(scopes.get(0).toValue()).to.equal('scope');
161+
});
127162
});

0 commit comments

Comments
 (0)