Skip to content

Commit

Permalink
simplify security scheme handling
Browse files Browse the repository at this point in the history
  • Loading branch information
seriousme committed Jan 19, 2024
1 parent 6d3d95e commit 6dadfee
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 70 deletions.
22 changes: 9 additions & 13 deletions examples/generated-standaloneJS-project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async function generateRoutes(fastify, opts) {
},
handler: buildHandler(service, "addPet").bind(Service),
prehandler: buildPreHandler(security, [
[{ name: "petstore_auth", parameters: ["write:pets", "read:pets"] }],
{ petstore_auth: ["write:pets", "read:pets"] },
]).bind(Security),
});

Expand Down Expand Up @@ -217,7 +217,7 @@ async function generateRoutes(fastify, opts) {
},
handler: buildHandler(service, "updatePet").bind(Service),
prehandler: buildPreHandler(security, [
[{ name: "petstore_auth", parameters: ["write:pets", "read:pets"] }],
{ petstore_auth: ["write:pets", "read:pets"] },
]).bind(Security),
});

Expand All @@ -244,7 +244,7 @@ async function generateRoutes(fastify, opts) {
},
handler: buildHandler(service, "findPetsByStatus").bind(Service),
prehandler: buildPreHandler(security, [
[{ name: "petstore_auth", parameters: ["write:pets", "read:pets"] }],
{ petstore_auth: ["write:pets", "read:pets"] },
]).bind(Security),
});

Expand All @@ -269,7 +269,7 @@ async function generateRoutes(fastify, opts) {
},
handler: buildHandler(service, "findPetsByTags").bind(Service),
prehandler: buildPreHandler(security, [
[{ name: "petstore_auth", parameters: ["write:pets", "read:pets"] }],
{ petstore_auth: ["write:pets", "read:pets"] },
]).bind(Security),
});

Expand All @@ -290,9 +290,7 @@ async function generateRoutes(fastify, opts) {
},
},
handler: buildHandler(service, "getPetById").bind(Service),
prehandler: buildPreHandler(security, [
[{ name: "api_key", parameters: [] }],
]).bind(Security),
prehandler: buildPreHandler(security, [{ api_key: [] }]).bind(Security),
});

fastify.route({
Expand Down Expand Up @@ -326,7 +324,7 @@ async function generateRoutes(fastify, opts) {
},
handler: buildHandler(service, "updatePetWithForm").bind(Service),
prehandler: buildPreHandler(security, [
[{ name: "petstore_auth", parameters: ["write:pets", "read:pets"] }],
{ petstore_auth: ["write:pets", "read:pets"] },
]).bind(Security),
});

Expand All @@ -348,7 +346,7 @@ async function generateRoutes(fastify, opts) {
},
handler: buildHandler(service, "deletePet").bind(Service),
prehandler: buildPreHandler(security, [
[{ name: "petstore_auth", parameters: ["write:pets", "read:pets"] }],
{ petstore_auth: ["write:pets", "read:pets"] },
]).bind(Security),
});

Expand Down Expand Up @@ -383,7 +381,7 @@ async function generateRoutes(fastify, opts) {
},
handler: buildHandler(service, "uploadFile").bind(Service),
prehandler: buildPreHandler(security, [
[{ name: "petstore_auth", parameters: ["write:pets", "read:pets"] }],
{ petstore_auth: ["write:pets", "read:pets"] },
]).bind(Security),
});

Expand All @@ -392,9 +390,7 @@ async function generateRoutes(fastify, opts) {
url: "/store/inventory",
schema: {},
handler: buildHandler(service, "getInventory").bind(Service),
prehandler: buildPreHandler(security, [
[{ name: "api_key", parameters: [] }],
]).bind(Security),
prehandler: buildPreHandler(security, [{ api_key: [] }]).bind(Security),
});

fastify.route({
Expand Down
31 changes: 13 additions & 18 deletions examples/generated-standaloneJS-project/securityHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,17 @@ export default class SecurityHandlers {
}
const mapKey = JSON.stringify(schemes);
if (!this.handlerMap.has(mapKey)) {
const processedSchemesList = [];
for (const schemeList of schemes) {
const processedSchemes = [];
if (schemeList?.length > 0) {
for (const scheme of schemeList) {
if (!(scheme.name in this.handlers)) {
this.handlers[scheme.name] = () => {
throw `Missing handler for "${scheme.name}" validation`;
};
this.missingHandlers.push(scheme.name);
}
processedSchemes.push(scheme);
for (const name in schemeList) {
if (!(name in this.handlers)) {
this.handlers[name] = () => {
throw `Missing handler for "${name}" validation`;
};
this.missingHandlers.push(name);
}
}
processedSchemesList.push(processedSchemes);
}
this.handlerMap.set(mapKey, this._buildHandler(processedSchemesList));
this.handlerMap.set(mapKey, this._buildHandler(schemes));
}
return this.handlerMap.has(mapKey);
}
Expand All @@ -65,17 +59,18 @@ export default class SecurityHandlers {
const schemeListDone = [];
let statusCode = 401;
for (const schemeList of schemes) {
let scheme;
let name;
const andList = [];
try {
for (scheme of schemeList) {
andList.push(scheme.name);
for (name in schemeList) {
const parameters = schemeList[name];
andList.push(name);
// all the handlers in a scheme list must succeed
await securityHandlers[scheme.name](req, reply, scheme.parameters);
await securityHandlers[name](req, reply, parameters);
}
return; // If one list of schemes passes, no need to try any others
} catch (err) {
req.log.debug(`Security handler '${scheme.name}' failed: '${err}'`);
req.log.debug(`Security handler '${name}' failed: '${err}'`);
handlerErrors.push(err);
if (err.statusCode !== undefined) {
statusCode = err.statusCode;
Expand Down
19 changes: 1 addition & 18 deletions lib/ParserBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,6 @@ export class ParserBase {
}
}

parseSecurity(schemes) {
return schemes
? schemes.map((item) => {
const result = [];
for (const name in item) {
result.push({
name,
parameters: item[name],
});
return result;
}
})
: undefined;
}

removeRecursion(schemas) {
function escapeJsonPointer(str) {
return str.replace(/~/g, "~0").replace(/\//g, "~1");
Expand Down Expand Up @@ -121,9 +106,7 @@ export class ParserBase {
operationId:
operationSpec.operationId || this.makeOperationId(operation, path),
openapiSource: operationSpec,
security: this.parseSecurity(
operationSpec.security || this.spec.security,
),
security: operationSpec.security || this.spec.security,
};

if (operationSpec["x-fastify-config"]) {
Expand Down
31 changes: 13 additions & 18 deletions lib/securityHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,17 @@ export default class SecurityHandlers {
}
const mapKey = JSON.stringify(schemes);
if (!this.handlerMap.has(mapKey)) {
const processedSchemesList = [];
for (const schemeList of schemes) {
const processedSchemes = [];
if (schemeList?.length > 0) {
for (const scheme of schemeList) {
if (!(scheme.name in this.handlers)) {
this.handlers[scheme.name] = () => {
throw `Missing handler for "${scheme.name}" validation`;
};
this.missingHandlers.push(scheme.name);
}
processedSchemes.push(scheme);
for (const name in schemeList) {
if (!(name in this.handlers)) {
this.handlers[name] = () => {
throw `Missing handler for "${name}" validation`;
};
this.missingHandlers.push(name);
}
}
processedSchemesList.push(processedSchemes);
}
this.handlerMap.set(mapKey, this._buildHandler(processedSchemesList));
this.handlerMap.set(mapKey, this._buildHandler(schemes));
}
return this.handlerMap.has(mapKey);
}
Expand All @@ -65,17 +59,18 @@ export default class SecurityHandlers {
const schemeListDone = [];
let statusCode = 401;
for (const schemeList of schemes) {
let scheme;
let name;
const andList = [];
try {
for (scheme of schemeList) {
andList.push(scheme.name);
for (name in schemeList) {
const parameters = schemeList[name];
andList.push(name);
// all the handlers in a scheme list must succeed
await securityHandlers[scheme.name](req, reply, scheme.parameters);
await securityHandlers[name](req, reply, parameters);
}
return; // If one list of schemes passes, no need to try any others
} catch (err) {
req.log.debug(`Security handler '${scheme.name}' failed: '${err}'`);
req.log.debug(`Security handler '${name}' failed: '${err}'`);
handlerErrors.push(err);
if (err.statusCode !== undefined) {
statusCode = err.statusCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"securityHandlers": {
"fileName": "securityHandlers.js",
"checksum": "285e187c9dec60a54cbc651faef6a13aa82e0f2919cc14be5b2edacd42c0db6c"
"checksum": "86252e229179c494fff9e5f0a614351dcedc69b3bd5f65f5882ef22d610b4d71"
},
"testPlugin": {
"fileName": "test-plugin.js",
Expand Down
4 changes: 2 additions & 2 deletions test/test-swagger.v2.standaloneJS.checksums.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
},
"plugin": {
"fileName": "index.js",
"checksum": "0fa45b86cfe223bb9b4b8b1c7e66a4ae1ed84551e8f3e85947c1dab690ebc8a7"
"checksum": "f4b27a6530e2ae6a0acdf48cd09a8060db4add0bebe53f9ced3b1d8209f35803"
},
"readme": {
"fileName": "README.md",
"checksum": "525645f6c7f8244fd91a768c331ef08165d62210fcdd4f80e3f7fa77c272acba"
},
"securityHandlers": {
"fileName": "securityHandlers.js",
"checksum": "285e187c9dec60a54cbc651faef6a13aa82e0f2919cc14be5b2edacd42c0db6c"
"checksum": "86252e229179c494fff9e5f0a614351dcedc69b3bd5f65f5882ef22d610b4d71"
},
"testPlugin": {
"fileName": "test-plugin.js",
Expand Down

0 comments on commit 6dadfee

Please sign in to comment.