Added support for OAuth2 flows

This commit is contained in:
Sahil
2022-07-25 16:50:47 +05:30
parent 72f2a10ac1
commit 8f1dfed405

View File

@@ -75,6 +75,12 @@ const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/sc
VALIDATION: 'VALIDATION',
CONVERSION: 'CONVERSION'
},
FLOW_TYPE = {
authorizationCode: 'authorization_code',
implicit: 'implicit',
password: 'password_credentials',
clientCredentials: 'client_credentials'
},
// These are the methods supported in the PathItem schema
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#pathItemObject
@@ -1110,8 +1116,87 @@ module.exports = {
}
else if (securityDef.type === 'oauth2') {
helper = {
type: 'oauth2'
type: 'oauth2',
oauth2: []
};
let flowObj, currentFlowType, flowCollectionIdentifier;
if (securityDef.flows) {
/*
//===================[]========================\\
|| OAuth2 Flow Name || Key name in collection ||
|]===================[]========================[|
|| clientCredentials || client_credentials ||
|| password || password_credentials ||
|| implicit || implicit ||
|| authorizationCode || authorization_code ||
\\===================[]========================//
Ref : https://swagger.io/docs/specification/authentication/oauth2/
Other flow types in collection
• "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');
}
}
if (currentFlowType) { // Means the flow is of supported type
// Fields supported by all flows -> refreshUrl, scopes
if (!_.isEmpty(flowObj.scope)) {
helper.oauth2.push({
key: 'scope',
value: _.isString(flowObj.scopes) ? flowObj.scopes : ''
});
}
/* refreshURL is indicated by key 'redirect_uri' in collection
Ref : https://stackoverflow.com/a/42131366/19078409 */
if (!_.isEmpty(flowObj.refreshUrl)) {
helper.oauth2.push({
key: 'redirect_uri',
value: _.isString(flowObj.refreshUrl) ? flowObj.refreshUrl : ''
});
}
// 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>'
});
}
// 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>'
});
}
flowCollectionIdentifier = {
key: 'grant_type',
value: currentFlowType
};
helper.oauth2.push(flowCollectionIdentifier);
}
}
else if (securityDef.type === 'apiKey') {
helper = {