diff --git a/server/.env.template b/server/.env.template index 9740f4c..7d3ca64 100644 --- a/server/.env.template +++ b/server/.env.template @@ -4,6 +4,7 @@ DB_PASSWORD="" DB_HOST="" DB_DIALECT="" DB_PORT="" +WEBHOOK_TOKEN="" #Token that is placed a the TTN Webhook auth WIGLE_TOKEN="" # Go to account and generate token "Encoded for use" WIGLE_BASE_URL="https://api.wigle.net" WIGLE_NETWORK_SEARCH="/api/v2/network/search" diff --git a/server/scripts/ttn-webhook-dummy.py b/server/scripts/ttn-webhook-dummy.py index ac1c4a2..2d9ad8e 100644 --- a/server/scripts/ttn-webhook-dummy.py +++ b/server/scripts/ttn-webhook-dummy.py @@ -9,9 +9,16 @@ import json import argparse import random +token = "ich-bin-da-token" + +headers = { + "Authorization": f"Bearer {token}", + "Content-Type": "application/json", # Adjust if needed for your payload format +} + def send_post_request(uri, data): try: - requests.post(uri, json=data, timeout=1) + requests.post(uri, json=data, timeout=1, headers=headers) except requests.exceptions.RequestException as e: pass diff --git a/server/src/controller/ttnController.ts b/server/src/controller/ttnController.ts index 606d9de..07281ae 100644 --- a/server/src/controller/ttnController.ts +++ b/server/src/controller/ttnController.ts @@ -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, }); };