mirror of
https://github.com/imputnet/cobalt.git
synced 2025-02-17 19:00:13 +00:00
api/core: fix & clean up auth middleware
This commit is contained in:
parent
30c51b9fe8
commit
974b98f0ac
|
@ -99,39 +99,41 @@ export function runAPI(express, app, __dirname) {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
app.post('/', (req, res, next) => {
|
app.post('/', (req, res, next) => {
|
||||||
|
if (!env.turnstileSecret || !env.jwtSecret) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (env.turnstileSecret && env.jwtSecret) {
|
const authorization = req.header("Authorization");
|
||||||
const authorization = req.header("Authorization");
|
if (!authorization) {
|
||||||
if (!authorization) {
|
return fail(res, "error.api.auth.jwt.missing");
|
||||||
return fail(res, "error.api.auth.jwt.missing");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!authorization.startsWith("Bearer ") || authorization.length > 256) {
|
|
||||||
return fail(res, "error.api.auth.jwt.invalid");
|
|
||||||
}
|
|
||||||
|
|
||||||
const verifyJwt = jwt.verify(
|
|
||||||
authorization.split("Bearer ", 2)[1]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!verifyJwt) {
|
|
||||||
return fail(res, "error.api.auth.jwt.invalid");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!acceptRegex.test(req.header('Accept'))) {
|
|
||||||
return fail(res, 'ErrorInvalidAcceptHeader');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!acceptRegex.test(req.header('Content-Type'))) {
|
|
||||||
return fail(res, 'ErrorInvalidContentType');
|
|
||||||
}
|
|
||||||
|
|
||||||
req.authorized = true;
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!authorization.startsWith("Bearer ") || authorization.length > 256) {
|
||||||
|
return fail(res, "error.api.auth.jwt.invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
const verifyJwt = jwt.verify(
|
||||||
|
authorization.split("Bearer ", 2)[1]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!verifyJwt) {
|
||||||
|
return fail(res, "error.api.auth.jwt.invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!acceptRegex.test(req.header('Accept'))) {
|
||||||
|
return fail(res, 'ErrorInvalidAcceptHeader');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!acceptRegex.test(req.header('Content-Type'))) {
|
||||||
|
return fail(res, 'ErrorInvalidContentType');
|
||||||
|
}
|
||||||
|
|
||||||
|
req.authorized = true;
|
||||||
} catch {
|
} catch {
|
||||||
return fail(res, "error.api.generic");
|
return fail(res, "error.api.generic");
|
||||||
}
|
}
|
||||||
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/', apiLimiter);
|
app.post('/', apiLimiter);
|
||||||
|
|
Loading…
Reference in a new issue