Skip to content

Commit 46ed506

Browse files
Merge pull request #728 from postmanlabs/feature/add-inherit-authentication-support-for-v2
Added alwaysInheritAuthentication option to v2
2 parents da61fd1 + 2879de2 commit 46ed506

File tree

8 files changed

+139
-4
lines changed

8 files changed

+139
-4
lines changed

OPTIONS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ allowUrlPathVarMatching|boolean|-|false|Whether to allow matching path variables
2121
enableOptionalParameters|boolean|-|true|Optional parameters aren't selected in the collection. Once enabled they will be selected in the collection and request as well.|CONVERSION|v2, v1
2222
keepImplicitHeaders|boolean|-|false|Whether to keep implicit headers from the OpenAPI specification, which are removed by default.|CONVERSION|v2, v1
2323
includeDeprecated|boolean|-|true|Select whether to include deprecated operations, parameters, and properties in generated collection or not|CONVERSION, VALIDATION|v2, v1
24+
alwaysInheritAuthentication|boolean|-|false|Whether authentication details should be included on every request, or always inherited from the collection.|CONVERSION|v2, v1

lib/options.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,18 @@ module.exports = {
374374
usage: ['CONVERSION', 'VALIDATION'],
375375
supportedIn: [VERSION20, VERSION30, VERSION31],
376376
supportedModuleVersion: [MODULE_VERSION.V2, MODULE_VERSION.V1]
377+
},
378+
{
379+
name: 'Always inherit authentication',
380+
id: 'alwaysInheritAuthentication',
381+
type: 'boolean',
382+
default: false,
383+
description: 'Whether authentication details should be included on every request, or always inherited from ' +
384+
'the collection.',
385+
external: true,
386+
usage: ['CONVERSION'],
387+
supportedIn: [VERSION20, VERSION30, VERSION31],
388+
supportedModuleVersion: [MODULE_VERSION.V2, MODULE_VERSION.V1]
377389
}
378390
];
379391

lib/schemaUtils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2647,7 +2647,9 @@ module.exports = {
26472647
}
26482648

26492649
// handling authentication here (for http type only)
2650-
authHelper = this.getAuthHelper(openapi, operation.security);
2650+
if (!options.alwaysInheritAuthentication) {
2651+
authHelper = this.getAuthHelper(openapi, operation.security);
2652+
}
26512653

26522654
// creating the request object
26532655
item = new sdk.Item({

libV2/schemaUtils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,8 @@ module.exports = {
18661866
requestBody = resolveRequestBodyForPostmanRequest(context, operationItem[method]),
18671867
request,
18681868
securitySchema = _.get(operationItem, [method, 'security']),
1869-
authHelper = generateAuthForCollectionFromOpenAPI(context.openapi, securitySchema);
1869+
authHelper = generateAuthForCollectionFromOpenAPI(context.openapi, securitySchema),
1870+
{ alwaysInheritAuthentication } = context.computedOptions;
18701871

18711872
headers.push(..._.get(requestBody, 'headers', []));
18721873
pathVariables.push(...baseUrlData.pathVariables);
@@ -1885,7 +1886,7 @@ module.exports = {
18851886
},
18861887
headers,
18871888
body: _.get(requestBody, 'body'),
1888-
auth: authHelper
1889+
auth: alwaysInheritAuthentication ? undefined : authHelper
18891890
};
18901891

18911892
const { responses, acceptHeader } = resolveResponseForPostmanRequest(context, operationItem[method], request);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: 3.0.0
2+
info:
3+
title: "Reproduce Authorization issue"
4+
version: 0.0.1
5+
security:
6+
- MyAuth: []
7+
- BearerAuth: []
8+
paths:
9+
/health:
10+
get:
11+
summary: "health"
12+
description: "Health check - always returns OK"
13+
operationId: "get_healthz"
14+
security:
15+
- BearerAuth: []
16+
responses:
17+
'200':
18+
description: "OK"
19+
content:
20+
text/plain:
21+
schema:
22+
type: "string"
23+
default: "OK"
24+
/status:
25+
get:
26+
summary: "status"
27+
description: "Returns the service version"
28+
operationId: "get_status"
29+
security:
30+
- MyAuth: []
31+
responses:
32+
'200':
33+
description: "Service info multi-line string"
34+
content:
35+
text/plain:
36+
schema:
37+
type: "string"
38+
components:
39+
securitySchemes:
40+
BearerAuth:
41+
type: http
42+
scheme: bearer
43+
bearerFormat: token
44+
MyAuth:
45+
type: apiKey
46+
description: "This is my auth"
47+
name: Mera-Auth
48+
in: header

test/system/structure.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const optionIds = [
2929
'includeReferenceMap',
3030
'includeDeprecated',
3131
'parametersResolution',
32-
'disabledParametersValidation'
32+
'disabledParametersValidation',
33+
'alwaysInheritAuthentication'
3334
],
3435
expectedOptions = {
3536
collapseFolders: {
@@ -222,6 +223,15 @@ const optionIds = [
222223
description: 'Whether disabled parameters of collection should be validated',
223224
external: false,
224225
usage: ['VALIDATION']
226+
},
227+
alwaysInheritAuthentication: {
228+
name: 'Always inherit authentication',
229+
type: 'boolean',
230+
default: false,
231+
description: 'Whether authentication details should be included on every request, or always inherited from ' +
232+
'the collection.',
233+
external: true,
234+
usage: ['CONVERSION']
225235
}
226236
};
227237

test/unit/base.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ describe('CONVERT FUNCTION TESTS ', function() {
4646
tooManyRefs = path.join(__dirname, VALID_OPENAPI_PATH, '/too-many-refs.json'),
4747
tagsFolderSpec = path.join(__dirname, VALID_OPENAPI_PATH + '/petstore-detailed.yaml'),
4848
securityTestCases = path.join(__dirname, VALID_OPENAPI_PATH + '/security-test-cases.yaml'),
49+
securityTestInheritance = path.join(__dirname, VALID_OPENAPI_PATH + '/security-test-inheritance.yaml'),
4950
emptySecurityTestCase = path.join(__dirname, VALID_OPENAPI_PATH + '/empty-security-test-case.yaml'),
5051
rootUrlServerWithVariables = path.join(__dirname, VALID_OPENAPI_PATH + '/root_url_server_with_variables.json'),
5152
parameterExamples = path.join(__dirname, VALID_OPENAPI_PATH + '/parameteres_with_examples.yaml'),
@@ -97,6 +98,34 @@ describe('CONVERT FUNCTION TESTS ', function() {
9798
path.join(__dirname, VALID_OPENAPI_PATH, '/recursiveRefComponents.yaml');
9899

99100

101+
it('Should explicitly set auth when specified on a request ' +
102+
securityTestInheritance, function(done) {
103+
var openapi = fs.readFileSync(securityTestInheritance, 'utf8');
104+
Converter.convert({ type: 'string', data: openapi }, {}, (err, conversionResult) => {
105+
106+
expect(err).to.be.null;
107+
expect(conversionResult.output[0].data.auth.type).to.equal('apikey');
108+
expect(conversionResult.output[0].data.item[0].request.auth.type).to.equal('bearer');
109+
expect(conversionResult.output[0].data.item[1].request.auth.type).to.equal('apikey');
110+
done();
111+
});
112+
});
113+
114+
it('Should not explicitly set auth when specified on a request when passed alwaysInheritAuthentication ' +
115+
securityTestInheritance, function(done) {
116+
var openapi = fs.readFileSync(securityTestInheritance, 'utf8');
117+
Converter.convert(
118+
{ type: 'string', data: openapi },
119+
{ alwaysInheritAuthentication: true }, (err, conversionResult) => {
120+
121+
expect(err).to.be.null;
122+
expect(conversionResult.output[0].data.auth.type).to.equal('apikey');
123+
expect(conversionResult.output[0].data.item[0].request.auth).to.be.undefined;
124+
expect(conversionResult.output[0].data.item[1].request.auth).to.be.undefined;
125+
done();
126+
});
127+
});
128+
100129
it('Should add collection level auth with type as `bearer`' +
101130
securityTestCases, function(done) {
102131
var openapi = fs.readFileSync(securityTestCases, 'utf8'),

test/unit/convertV2.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const expect = require('chai').expect,
3939
issue193 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#193.yml'),
4040
tooManyRefs = path.join(__dirname, VALID_OPENAPI_PATH, '/too_many_ref_example.json'),
4141
securityTestCases = path.join(__dirname, VALID_OPENAPI_PATH + '/security-test-cases.yaml'),
42+
securityTestInheritance = path.join(__dirname, VALID_OPENAPI_PATH + '/security-test-inheritance.yaml'),
4243
emptySecurityTestCase = path.join(__dirname, VALID_OPENAPI_PATH + '/empty-security-test-case.yaml'),
4344
rootUrlServerWithVariables = path.join(__dirname, VALID_OPENAPI_PATH + '/root_url_server_with_variables.json'),
4445
parameterExamples = path.join(__dirname, VALID_OPENAPI_PATH + '/parameteres_with_examples.yaml'),
@@ -96,6 +97,37 @@ const expect = require('chai').expect,
9697

9798
describe('The convert v2 Function', function() {
9899

100+
it('Should explicitly set auth when specified on a request ' +
101+
securityTestInheritance, function(done) {
102+
var openapi = fs.readFileSync(securityTestInheritance, 'utf8');
103+
Converter.convertV2({ type: 'string', data: openapi }, {}, (err, conversionResult) => {
104+
105+
expect(err).to.be.null;
106+
expect(conversionResult.output[0].data.auth.type).to.equal('apikey');
107+
expect(conversionResult.output[0].data.item[0].item[0].request.auth.type).to.equal('apikey');
108+
expect(conversionResult.output[0].data.item[1].item[0].request.auth.type).to.equal('bearer');
109+
done();
110+
});
111+
});
112+
113+
it('Should not explicitly set auth when specified on a request when passed alwaysInheritAuthentication ' +
114+
securityTestInheritance, function(done) {
115+
const isEmptyArrayOrNull = (value) => {
116+
return Array.isArray(value) && value.length === 0 || value === null;
117+
};
118+
var openapi = fs.readFileSync(securityTestInheritance, 'utf8');
119+
Converter.convertV2(
120+
{ type: 'string', data: openapi },
121+
{ alwaysInheritAuthentication: true }, (err, conversionResult) => {
122+
123+
expect(err).to.be.null;
124+
expect(conversionResult.output[0].data.auth.type).to.equal('apikey');
125+
expect(conversionResult.output[0].data.item[0].item[0].request.auth).to.satisfy(isEmptyArrayOrNull);
126+
expect(conversionResult.output[0].data.item[1].item[0].request.auth).to.satisfy(isEmptyArrayOrNull);
127+
done();
128+
});
129+
});
130+
99131
it('Should add collection level auth with type as `bearer`' +
100132
securityTestCases, function(done) {
101133
var openapi = fs.readFileSync(securityTestCases, 'utf8'),

0 commit comments

Comments
 (0)