Skip to content

Commit 4a71bb6

Browse files
Fix CORS support for openapi.yaml; exempting openapi.yaml from authentication requirements.
1 parent 2be03fe commit 4a71bb6

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/requestHandler.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ export default class RequestHandler {
154154
res: express.Response,
155155
next: express.NextFunction
156156
): Promise<void> {
157-
const authenticationExemptRoutes: string[] = ["/", `/${CERT_NAME}`];
157+
const authenticationExemptRoutes: string[] = [
158+
"/",
159+
`/${CERT_NAME}`,
160+
"/openapi.yaml",
161+
];
158162

159163
if (
160164
!authenticationExemptRoutes.includes(req.path) &&
@@ -258,10 +262,10 @@ export default class RequestHandler {
258262
certificateInfo:
259263
this.requestIsAuthenticated(req) && certificate
260264
? {
261-
validityDays: getCertificateValidityDays(certificate),
262-
regenerateRecommended:
263-
!getCertificateIsUptoStandards(certificate),
264-
}
265+
validityDays: getCertificateValidityDays(certificate),
266+
regenerateRecommended:
267+
!getCertificateIsUptoStandards(certificate),
268+
}
265269
: undefined,
266270
apiExtensions: this.requestIsAuthenticated(req)
267271
? this.apiExtensions.map(({ manifest }) => manifest)
@@ -1140,6 +1144,14 @@ export default class RequestHandler {
11401144
res.status(200).send(this.settings.crypto.cert);
11411145
}
11421146

1147+
async openapiYamlGet(
1148+
req: express.Request,
1149+
res: express.Response
1150+
): Promise<void> {
1151+
res.setHeader("Content-Type", "application/yaml; charset=utf-8");
1152+
res.status(200).send(openapiYaml);
1153+
}
1154+
11431155
async notFoundHandler(
11441156
req: express.Request,
11451157
res: express.Response,
@@ -1176,12 +1188,6 @@ export default class RequestHandler {
11761188
}
11771189

11781190
setupRouter() {
1179-
// Serve /openapi.yaml unauthenticated
1180-
this.api.get("/openapi.yaml", (req, res) => {
1181-
res.setHeader("Content-Type", "application/yaml; charset=utf-8");
1182-
res.status(200).send(openapiYaml);
1183-
});
1184-
11851191
this.api.use((req, res, next) => {
11861192
const originalSend = res.send;
11871193
res.send = function (body, ...args) {
@@ -1266,6 +1272,7 @@ export default class RequestHandler {
12661272
this.api.route("/open/*").post(this.openPost.bind(this));
12671273

12681274
this.api.get(`/${CERT_NAME}`, this.certificateGet.bind(this));
1275+
this.api.get("/openapi.yaml", this.openapiYamlGet.bind(this));
12691276
this.api.get("/", this.root.bind(this));
12701277

12711278
this.api.use(this.apiExtensionRouter);

0 commit comments

Comments
 (0)