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

the security validation on the operation was improved. #223

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 25 additions & 17 deletions lib/codegen.js
Expand Up @@ -30,6 +30,23 @@ var getPathToMethodName = function(opts, m, path){
return m.toLowerCase() + result[0].toUpperCase() + result.substring(1);
};

var validateSecurity = function(securityDefinitions, security) {
var secureTypes = [];
if (securityDefinitions !== undefined && security !== undefined) {
var mergedSecurity = _.merge([], security).map(function(securityOptions) {
return Object.keys(securityOptions);
});
if(securityDefinitions) {
for(var sk in securityDefinitions) {
if(mergedSecurity.join(',').indexOf(sk) !== -1) {
secureTypes = secureTypes.concat(securityDefinitions[sk].type);
}
}
}
}
return secureTypes;
};

var getViewForSwagger2 = function(opts, type){
var swagger = opts.swagger;
var methods = [];
Expand All @@ -47,6 +64,8 @@ var getViewForSwagger2 = function(opts, type){
definitions: []
};

var secureTypesGlobal = validateSecurity(swagger.securityDefinitions, swagger.security);

_.forEach(swagger.paths, function(api, path){
var globalParams = [];
/**
Expand All @@ -63,19 +82,8 @@ var getViewForSwagger2 = function(opts, type){
if(M === '' || authorizedMethods.indexOf(M) === -1) {
return;
}
var secureTypes = [];
if(swagger.securityDefinitions !== undefined || op.security !== undefined) {
var mergedSecurity = _.merge([], swagger.security, op.security).map(function(security){
return Object.keys(security);
});
if(swagger.securityDefinitions) {
for(var sk in swagger.securityDefinitions) {
if(mergedSecurity.join(',').indexOf(sk) !== -1){
secureTypes.push(swagger.securityDefinitions[sk].type);
}
}
}
}
var secureTypes = secureTypesGlobal.length > 0 ? secureTypesGlobal : validateSecurity(swagger.securityDefinitions, op.security);

var methodName = (op.operationId ? normalizeName(op.operationId) : getPathToMethodName(opts, m, path));
// Make sure the method name is unique
if(methods.indexOf(methodName) !== -1) {
Expand All @@ -100,10 +108,10 @@ var getViewForSwagger2 = function(opts, type){
isPOST: M === 'POST',
summary: op.description || op.summary,
externalDocs: op.externalDocs,
isSecure: swagger.security !== undefined || op.security !== undefined,
isSecureToken: secureTypes.indexOf('oauth2') !== -1,
isSecureApiKey: secureTypes.indexOf('apiKey') !== -1,
isSecureBasic: secureTypes.indexOf('basic') !== -1,
isSecure: secureTypes.length > 0,
isSecureToken: secureTypes.indexOf('oauth2') !== -1,
isSecureApiKey: secureTypes.indexOf('apiKey') !== -1,
isSecureBasic: secureTypes.indexOf('basic') !== -1,
parameters: [],
headers: []
};
Expand Down