Skip to content

Commit 8f1dfed

Browse files
Added support for OAuth2 flows
1 parent 72f2a10 commit 8f1dfed

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

lib/schemaUtils.js

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/sc
7575
VALIDATION: 'VALIDATION',
7676
CONVERSION: 'CONVERSION'
7777
},
78+
FLOW_TYPE = {
79+
authorizationCode: 'authorization_code',
80+
implicit: 'implicit',
81+
password: 'password_credentials',
82+
clientCredentials: 'client_credentials'
83+
},
7884

7985
// These are the methods supported in the PathItem schema
8086
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#pathItemObject
@@ -1110,8 +1116,87 @@ module.exports = {
11101116
}
11111117
else if (securityDef.type === 'oauth2') {
11121118
helper = {
1113-
type: 'oauth2'
1119+
type: 'oauth2',
1120+
oauth2: []
11141121
};
1122+
1123+
let flowObj, currentFlowType, flowCollectionIdentifier;
1124+
if (securityDef.flows) {
1125+
/*
1126+
//===================[]========================\\
1127+
|| OAuth2 Flow Name || Key name in collection ||
1128+
|]===================[]========================[|
1129+
|| clientCredentials || client_credentials ||
1130+
|| password || password_credentials ||
1131+
|| implicit || implicit ||
1132+
|| authorizationCode || authorization_code ||
1133+
\\===================[]========================//
1134+
Ref : https://swagger.io/docs/specification/authentication/oauth2/
1135+
1136+
1137+
Other flow types in collection
1138+
• "authorization_code_with_pkce"
1139+
1140+
*/
1141+
if (securityDef.flows.hasOwnProperty('clientCredentials')) {
1142+
currentFlowType = FLOW_TYPE.clientCredentials;
1143+
flowObj = _.get(securityDef, 'flows.clientCredentials');
1144+
}
1145+
else if (securityDef.flows.hasOwnProperty('authorizationCode')) {
1146+
currentFlowType = FLOW_TYPE.authorizationCode;
1147+
flowObj = _.get(securityDef, 'flows.authorizationCode');
1148+
}
1149+
else if (securityDef.flows.hasOwnProperty('password')) {
1150+
currentFlowType = FLOW_TYPE.password;
1151+
flowObj = _.get(securityDef, 'flows.password');
1152+
}
1153+
else if (securityDef.flows.hasOwnProperty('implicit')) {
1154+
currentFlowType = FLOW_TYPE.implicit;
1155+
flowObj = _.get(securityDef, 'flows.implicit');
1156+
}
1157+
}
1158+
1159+
if (currentFlowType) { // Means the flow is of supported type
1160+
1161+
// Fields supported by all flows -> refreshUrl, scopes
1162+
if (!_.isEmpty(flowObj.scope)) {
1163+
helper.oauth2.push({
1164+
key: 'scope',
1165+
value: _.isString(flowObj.scopes) ? flowObj.scopes : ''
1166+
});
1167+
}
1168+
1169+
/* refreshURL is indicated by key 'redirect_uri' in collection
1170+
Ref : https://stackoverflow.com/a/42131366/19078409 */
1171+
if (!_.isEmpty(flowObj.refreshUrl)) {
1172+
helper.oauth2.push({
1173+
key: 'redirect_uri',
1174+
value: _.isString(flowObj.refreshUrl) ? flowObj.refreshUrl : ''
1175+
});
1176+
}
1177+
1178+
// Fields supported by all flows except implicit -> tokenUrl
1179+
if (currentFlowType !== FLOW_TYPE.implicit) {
1180+
helper.oauth2.push({
1181+
key: 'accessTokenUrl',
1182+
value: _.isString(flowObj.tokenUrl) ? flowObj.tokenUrl : '<Access Token URL>'
1183+
});
1184+
}
1185+
1186+
// Fields supported by all flows all except password, clientCredentials -> authorizationUrl
1187+
if (currentFlowType !== FLOW_TYPE.password && currentFlowType !== FLOW_TYPE.clientCredentials) {
1188+
helper.oauth2.push({
1189+
key: 'authUrl',
1190+
value: _.isString(flowObj.authUrl) ? flowObj.authUrl : '<Auth URL>'
1191+
});
1192+
}
1193+
1194+
flowCollectionIdentifier = {
1195+
key: 'grant_type',
1196+
value: currentFlowType
1197+
};
1198+
helper.oauth2.push(flowCollectionIdentifier);
1199+
}
11151200
}
11161201
else if (securityDef.type === 'apiKey') {
11171202
helper = {

0 commit comments

Comments
 (0)