Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the alwaysInheritAuthentication option #451

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ ignoreUnresolvedVariables|boolean|-|false|Whether to ignore mismatches resulting
strictRequestMatching|boolean|-|false|Whether requests should be strictly matched with schema operations. Setting to true will not include any matches where the URL path segments don't match exactly.|VALIDATION
disableOptionalParameters|boolean|-|false|Whether to set optional parameters as disabled|CONVERSION
keepImplicitHeaders|boolean|-|false|Whether to keep implicit headers from the OpenAPI specification, which are removed by default.|CONVERSION
alwaysInheritAuthentication|boolean|-|false|Whether authentication details should be included on every request, or always inherited from the collection.|CONVERSION
10 changes: 10 additions & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ module.exports = {
description: 'Whether to keep implicit headers from the OpenAPI specification, which are removed by default.',
external: true,
usage: ['CONVERSION']
},
{
name: 'Always inherit authentication',
id: 'alwaysInheritAuthentication',
type: 'boolean',
default: false,
description: 'Whether authentication details should be included on every request, or always inherited from ' +
'the collection.',
external: true,
usage: ['CONVERSION']
}
];

Expand Down
9 changes: 7 additions & 2 deletions lib/schemaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2321,7 +2321,12 @@ module.exports = {
}

// handling authentication here (for http type only)
authHelper = this.getAuthHelper(openapi, operation.security);
if (options.alwaysInheritAuthentication) {
authHelper = this.getAuthHelper(openapi, openapi.security);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AndrewGuenther wanted some clarity on why we're setting the value of authHelper here

}
else {
authHelper = this.getAuthHelper(openapi, operation.security);
}

// creating the request object
item = new sdk.Item({
Expand All @@ -2344,7 +2349,7 @@ module.exports = {
thisAuthObject[authMap[authMeta.currentHelper]] = authMeta.helperAttributes;
item.request.auth = new sdk.RequestAuth(thisAuthObject);
}
else {
else if (!options.alwaysInheritAuthentication) {
item.request.auth = authHelper;
}

Expand Down
48 changes: 48 additions & 0 deletions test/data/valid_openapi/security-test-inheritance.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
openapi: 3.0.0
info:
title: "Reproduce Authorization issue"
version: 0.0.1
security:
- MyAuth: []
- BearerAuth: []
paths:
/health:
get:
summary: "health"
description: "Health check - always returns OK"
operationId: "get_healthz"
security:
- BearerAuth: []
responses:
'200':
description: "OK"
content:
text/plain:
schema:
type: "string"
default: "OK"
/status:
get:
summary: "status"
description: "Returns the service version"
operationId: "get_status"
security:
- MyAuth: []
responses:
'200':
description: "Service info multi-line string"
content:
text/plain:
schema:
type: "string"
components:
securitySchemes:
BearerAuth:
type: http
scheme: bearer
bearerFormat: token
MyAuth:
type: apiKey
description: "This is my auth"
name: Mera-Auth
in: header
12 changes: 11 additions & 1 deletion test/system/structure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const optionIds = [
'optimizeConversion',
'strictRequestMatching',
'disableOptionalParameters',
'keepImplicitHeaders'
'keepImplicitHeaders',
'alwaysInheritAuthentication'
],
expectedOptions = {
collapseFolders: {
Expand Down Expand Up @@ -171,6 +172,15 @@ const optionIds = [
description: 'Whether to keep implicit headers from the OpenAPI specification, which are removed by default.',
external: true,
usage: ['CONVERSION']
},
alwaysInheritAuthentication: {
name: 'Always inherit authentication',
type: 'boolean',
default: false,
description: 'Whether authentication details should be included on every request, or always inherited from ' +
'the collection.',
external: true,
usage: ['CONVERSION']
}
};

Expand Down
28 changes: 28 additions & 0 deletions test/unit/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,38 @@ describe('CONVERT FUNCTION TESTS ', function() {
tooManyRefs = path.join(__dirname, VALID_OPENAPI_PATH, '/too-many-refs.json'),
tagsFolderSpec = path.join(__dirname, VALID_OPENAPI_PATH + '/petstore-detailed.yaml'),
securityTestCases = path.join(__dirname, VALID_OPENAPI_PATH + '/security-test-cases.yaml'),
securityTestInheritance = path.join(__dirname, VALID_OPENAPI_PATH + '/security-test-inheritance.yaml'),
emptySecurityTestCase = path.join(__dirname, VALID_OPENAPI_PATH + '/empty-security-test-case.yaml'),
rootUrlServerWithVariables = path.join(__dirname, VALID_OPENAPI_PATH + '/root_url_server_with_variables.json'),
parameterExamples = path.join(__dirname, VALID_OPENAPI_PATH + '/parameteres_with_examples.yaml');

it('Should explicitly set auth when specified on a request ' +
securityTestInheritance, function(done) {
var openapi = fs.readFileSync(securityTestInheritance, 'utf8');
Converter.convert({ type: 'string', data: openapi }, {}, (err, conversionResult) => {

expect(err).to.be.null;
expect(conversionResult.output[0].data.auth.type).to.equal('apikey');
expect(conversionResult.output[0].data.item[0].request.auth.type).to.equal('bearer');
expect(conversionResult.output[0].data.item[1].request.auth.type).to.equal('apikey');
done();
});
});

it('Should not explicitly set auth when specified on a request when passed alwaysInheritAuthentication ' +
securityTestInheritance, function(done) {
var openapi = fs.readFileSync(securityTestInheritance, 'utf8');
Converter.convert(
{ type: 'string', data: openapi },
{ alwaysInheritAuthentication: true }, (err, conversionResult) => {

expect(err).to.be.null;
expect(conversionResult.output[0].data.auth.type).to.equal('apikey');
expect(conversionResult.output[0].data.item[0].request.auth).to.be.undefined;
expect(conversionResult.output[0].data.item[1].request.auth).to.be.undefined;
done();
});
});

it('Should add collection level auth with type as `bearer`' +
securityTestCases, function(done) {
Expand Down