cleanup
This commit is contained in:
@ -20,38 +20,42 @@ const locationService = container.resolve(LocationService);
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const validateBearerToken = (authorizationHeader: string | undefined): boolean => {
|
||||
if (!authorizationHeader) {
|
||||
console.log("Authorization header is missing!");
|
||||
return false;
|
||||
}
|
||||
|
||||
const token = authorizationHeader.split(' ')[1]; // Extract token after 'Bearer'
|
||||
if (!token) {
|
||||
console.log("Bearer token is missing!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (token !== process.env.WEBHOOK_TOKEN) {
|
||||
console.log("Bearer token is incorrect!");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
router.post(
|
||||
"/webhook",
|
||||
validateData(ttnMessageValidator),
|
||||
async (req: Request, res: Response) => {
|
||||
try {
|
||||
const authorizationHeader = req.headers['authorization'];
|
||||
if (!authorizationHeader) {
|
||||
console.log("Authorization header is missing!");
|
||||
|
||||
if (!validateBearerToken(authorizationHeader as string)) {
|
||||
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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log("Bearer token is correct!");
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
console.error("Error during authentication:", error);
|
||||
res.status(401).json({ error: "Authentication failed" });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
Reference in New Issue
Block a user