mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
minor bug fixes, optimizations, added test cases
This commit is contained in:
@@ -1115,14 +1115,16 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
else if (securityDef.type === 'oauth2') {
|
||||
let flowObj, currentFlowType;
|
||||
|
||||
helper = {
|
||||
type: 'oauth2',
|
||||
oauth2: []
|
||||
};
|
||||
|
||||
let flowObj, currentFlowType, flowCollectionIdentifier;
|
||||
if (securityDef.flows) {
|
||||
if (_.isObject(securityDef.flows) && FLOW_TYPE[Object.keys(securityDef.flows)[0]]) {
|
||||
/*
|
||||
|
||||
//===================[]========================\\
|
||||
|| OAuth2 Flow Name || Key name in collection ||
|
||||
|]===================[]========================[|
|
||||
@@ -1133,36 +1135,24 @@ module.exports = {
|
||||
\\===================[]========================//
|
||||
Ref : https://swagger.io/docs/specification/authentication/oauth2/
|
||||
|
||||
In case of multiple flow types, the first one will be preferred
|
||||
and passed on to the collection.
|
||||
|
||||
Other flow types in collection
|
||||
Other flow types in collection which are not explicitly present in OA 3
|
||||
• "authorization_code_with_pkce"
|
||||
|
||||
*/
|
||||
if (securityDef.flows.hasOwnProperty('clientCredentials')) {
|
||||
currentFlowType = FLOW_TYPE.clientCredentials;
|
||||
flowObj = _.get(securityDef, 'flows.clientCredentials');
|
||||
}
|
||||
else if (securityDef.flows.hasOwnProperty('authorizationCode')) {
|
||||
currentFlowType = FLOW_TYPE.authorizationCode;
|
||||
flowObj = _.get(securityDef, 'flows.authorizationCode');
|
||||
}
|
||||
else if (securityDef.flows.hasOwnProperty('password')) {
|
||||
currentFlowType = FLOW_TYPE.password;
|
||||
flowObj = _.get(securityDef, 'flows.password');
|
||||
}
|
||||
else if (securityDef.flows.hasOwnProperty('implicit')) {
|
||||
currentFlowType = FLOW_TYPE.implicit;
|
||||
flowObj = _.get(securityDef, 'flows.implicit');
|
||||
}
|
||||
currentFlowType = FLOW_TYPE[Object.keys(securityDef.flows)[0]];
|
||||
flowObj = _.get(securityDef, `flows.${Object.keys(securityDef.flows)[0]}`);
|
||||
}
|
||||
|
||||
if (currentFlowType) { // Means the flow is of supported type
|
||||
|
||||
// Fields supported by all flows -> refreshUrl, scopes
|
||||
if (!_.isEmpty(flowObj.scope)) {
|
||||
if (!_.isEmpty(flowObj.scopes)) {
|
||||
helper.oauth2.push({
|
||||
key: 'scope',
|
||||
value: _.isString(flowObj.scopes) ? flowObj.scopes : ''
|
||||
value: Object.keys(flowObj.scopes).join(' ')
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1177,25 +1167,28 @@ module.exports = {
|
||||
|
||||
// Fields supported by all flows except implicit -> tokenUrl
|
||||
if (currentFlowType !== FLOW_TYPE.implicit) {
|
||||
helper.oauth2.push({
|
||||
key: 'accessTokenUrl',
|
||||
value: _.isString(flowObj.tokenUrl) ? flowObj.tokenUrl : '<Access Token URL>'
|
||||
});
|
||||
if (!_.isEmpty(flowObj.tokenUrl)) {
|
||||
helper.oauth2.push({
|
||||
key: 'accessTokenUrl',
|
||||
value: _.isString(flowObj.tokenUrl) ? flowObj.tokenUrl : '<Access Token URL>'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Fields supported by all flows all except password, clientCredentials -> authorizationUrl
|
||||
if (currentFlowType !== FLOW_TYPE.password && currentFlowType !== FLOW_TYPE.clientCredentials) {
|
||||
helper.oauth2.push({
|
||||
key: 'authUrl',
|
||||
value: _.isString(flowObj.authUrl) ? flowObj.authUrl : '<Auth URL>'
|
||||
});
|
||||
if (!_.isEmpty(flowObj.authorizationUrl)) {
|
||||
helper.oauth2.push({
|
||||
key: 'authUrl',
|
||||
value: _.isString(flowObj.authorizationUrl) ? flowObj.authorizationUrl : '<Auth URL>'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
flowCollectionIdentifier = {
|
||||
helper.oauth2.push({
|
||||
key: 'grant_type',
|
||||
value: currentFlowType
|
||||
};
|
||||
helper.oauth2.push(flowCollectionIdentifier);
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (securityDef.type === 'apiKey') {
|
||||
|
||||
@@ -2959,7 +2959,7 @@ describe('findCommonSubpath method', function () {
|
||||
});
|
||||
|
||||
describe('getAuthHelper method - OAuth2 Flows', function() {
|
||||
it('Should parse OAuth2 configuration to collection', function() {
|
||||
it('Should parse OAuth2 configuration to collection (Single Flow) - Type 1', function() {
|
||||
const openAPISpec = {
|
||||
'components': {
|
||||
'responses': {},
|
||||
@@ -3025,4 +3025,141 @@ describe('getAuthHelper method - OAuth2 Flows', function() {
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
it('Should parse OAuth2 configuration to collection (Multiple Flow types)- Type 2', function() {
|
||||
const openAPISpec = {
|
||||
components: {
|
||||
responses: {},
|
||||
schemas: {},
|
||||
securitySchemes: {
|
||||
oauth2: {
|
||||
type: 'oauth2',
|
||||
flows: {
|
||||
implicit: {
|
||||
authorizationUrl: 'https://example.com/api/oauth/dialog',
|
||||
scopes: {
|
||||
'write:pets': 'modify pets in your account',
|
||||
'read:pets': 'read your pets'
|
||||
}
|
||||
},
|
||||
authorizationCode: {
|
||||
authorizationUrl: 'https://example.com/api/oauth/dialog',
|
||||
tokenUrl: 'https://example.com/api/oauth/token',
|
||||
scopes: {
|
||||
'write:pets': 'modify pets in your account',
|
||||
'read:pets': 'read your pets'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
info: { title: 'API', version: '0.2' },
|
||||
openapi: '3.0.0',
|
||||
paths: {},
|
||||
security: [{ oauth2: [] }],
|
||||
servers: [{ url: 'https://myserver.com', variables: {} }],
|
||||
tags: [],
|
||||
securityDefs: {
|
||||
oauth2: {
|
||||
type: 'oauth2',
|
||||
flows: {
|
||||
implicit: {
|
||||
authorizationUrl: 'https://example.com/api/oauth/dialog',
|
||||
scopes: {
|
||||
'write:pets': 'modify pets in your account',
|
||||
'read:pets': 'read your pets'
|
||||
}
|
||||
},
|
||||
authorizationCode: {
|
||||
authorizationUrl: 'https://example.com/api/oauth/dialog',
|
||||
tokenUrl: 'https://example.com/api/oauth/token',
|
||||
scopes: {
|
||||
'write:pets': 'modify pets in your account',
|
||||
'read:pets': 'read your pets'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
baseUrl: 'https://myserver.com',
|
||||
baseUrlVariables: {}
|
||||
},
|
||||
securitySet = [{ oauth2: [] }],
|
||||
helperData = SchemaUtils.getAuthHelper(openAPISpec, securitySet);
|
||||
|
||||
expect(helperData.type).to.be.equal('oauth2');
|
||||
expect(helperData).to.have.property('oauth2').with.lengthOf(3);
|
||||
expect(helperData.oauth2[0]).to.be.an('object');
|
||||
expect(helperData).to.deep.equal({
|
||||
'type': 'oauth2',
|
||||
'oauth2': [
|
||||
{
|
||||
'key': 'scope',
|
||||
'value': 'write:pets read:pets'
|
||||
},
|
||||
{
|
||||
'key': 'authUrl',
|
||||
'value': 'https://example.com/api/oauth/dialog'
|
||||
},
|
||||
{
|
||||
'key': 'grant_type',
|
||||
'value': 'implicit'
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
it('Scopes are parsed as sequence of strings', function() {
|
||||
const openAPISpec = {
|
||||
components: {
|
||||
responses: {},
|
||||
schemas: {},
|
||||
securitySchemes: {
|
||||
oauth2: {
|
||||
type: 'oauth2',
|
||||
flows: {
|
||||
implicit: {
|
||||
authorizationUrl: 'https://example.com/api/oauth/dialog',
|
||||
scopes: {
|
||||
'write:pets': 'modify pets in your account',
|
||||
'read:pets': 'read your pets'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
info: { title: 'API', version: '0.2' },
|
||||
openapi: '3.0.0',
|
||||
paths: {},
|
||||
security: [{ oauth2: [] }],
|
||||
servers: [{ url: 'https://myserver.com', variables: {} }],
|
||||
tags: [],
|
||||
securityDefs: {
|
||||
oauth2: {
|
||||
type: 'oauth2',
|
||||
flows: {
|
||||
implicit: {
|
||||
authorizationUrl: 'https://example.com/api/oauth/dialog',
|
||||
scopes: {
|
||||
'write:pets': 'modify pets in your account',
|
||||
'read:pets': 'read your pets'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
baseUrl: 'https://myserver.com',
|
||||
baseUrlVariables: {}
|
||||
},
|
||||
securitySet = [{ oauth2: [] }],
|
||||
helperData = SchemaUtils.getAuthHelper(openAPISpec, securitySet);
|
||||
|
||||
expect(helperData.type).to.be.equal('oauth2');
|
||||
expect(helperData).to.have.property('oauth2').with.lengthOf(3);
|
||||
expect(helperData.oauth2[0]).to.be.an('object');
|
||||
expect(helperData.oauth2[0].key).to.be.equal('scope');
|
||||
expect(helperData.oauth2[0].value).to.be.equal('write:pets read:pets');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user