Skip to content

best way to save what you love

api/core: fix & clean up auth middleware

wukko974b98f0ac55
diff --git a/api/src/core/api.js b/api/src/core/api.js
index 396551eb..a1c2692a 100644
--- a/api/src/core/api.js
+++ b/api/src/core/api.js
@@ -99,39 +99,41 @@ export function runAPI(express, app, __dirname) {
}));
app.post('/', (req, res, next) => {
- try {
- if (env.turnstileSecret && env.jwtSecret) {
- const authorization = req.header("Authorization");
- if (!authorization) {
- return fail(res, "error.api.auth.jwt.missing");
- }
+ if (!env.turnstileSecret || !env.jwtSecret) {
+ return next();
+ }
- if (!authorization.startsWith("Bearer ") || authorization.length > 256) {
- return fail(res, "error.api.auth.jwt.invalid");
- }
+ try {
+ const authorization = req.header("Authorization");
+ if (!authorization) {
+ return fail(res, "error.api.auth.jwt.missing");
+ }
- const verifyJwt = jwt.verify(
- authorization.split("Bearer ", 2)[1]
- );
+ if (!authorization.startsWith("Bearer ") || authorization.length > 256) {
+ return fail(res, "error.api.auth.jwt.invalid");
+ }
- if (!verifyJwt) {
- return fail(res, "error.api.auth.jwt.invalid");
- }
+ const verifyJwt = jwt.verify(
+ authorization.split("Bearer ", 2)[1]
+ );
- if (!acceptRegex.test(req.header('Accept'))) {
- return fail(res, 'ErrorInvalidAcceptHeader');
- }
+ if (!verifyJwt) {
+ return fail(res, "error.api.auth.jwt.invalid");
+ }
- if (!acceptRegex.test(req.header('Content-Type'))) {
- return fail(res, 'ErrorInvalidContentType');
- }
+ if (!acceptRegex.test(req.header('Accept'))) {
+ return fail(res, 'ErrorInvalidAcceptHeader');
+ }
- req.authorized = true;
- next();
+ if (!acceptRegex.test(req.header('Content-Type'))) {
+ return fail(res, 'ErrorInvalidContentType');
}
+
+ req.authorized = true;
} catch {
return fail(res, "error.api.generic");
}
+ next();
});
app.post('/', apiLimiter);