first basic auth

This commit is contained in:
2025-01-05 21:45:09 +01:00
parent 62847f569d
commit 4896c63b1a
3 changed files with 42 additions and 4 deletions

View File

@ -24,6 +24,36 @@ router.post(
"/webhook",
validateData(ttnMessageValidator),
async (req: Request, res: Response) => {
try {
const authorizationHeader = req.headers['authorization'];
if (!authorizationHeader) {
console.log("Authorization header is missing!");
res.status(401).json({ error: "Authentication failed" });
return;
} else {
const token = authorizationHeader.split(' ')[1]; // Get the token after 'Bearer'
if (!token) {
console.log("Bearer token is missing!");
res.status(401).json({ error: "Authentication failed" });
return;
}
else {
console.log(token)
if (token !== process.env.WEBHOOK_TOKEN) {
console.log("Bearer token is wrong!");
res.status(401).json({ error: "Authentication failed" });
return;
} else {
console.log("Bearer token is correct!");
}
}
}
} catch (error) {
console.log(error);
res.status(401).json({ error: "Authentication failed" });
return;
}
try {
const message = req.body as TtnMessage;
@ -96,9 +126,9 @@ router.post(
gnss:
gnnsLocation.latitude && gnnsLocation.longitude
? {
latitude: gnnsLocation.latitude,
longitude: gnnsLocation.longitude,
}
latitude: gnnsLocation.latitude,
longitude: gnnsLocation.longitude,
}
: undefined,
});
};