diff --git a/server/scripts/ttn-webhook-dummy.py b/server/scripts/ttn-webhook-dummy.py index 94c5299..2949eaf 100644 --- a/server/scripts/ttn-webhook-dummy.py +++ b/server/scripts/ttn-webhook-dummy.py @@ -15,9 +15,10 @@ def send_post_request(uri, data, token): "Content-Type": "application/json", } try: - requests.post(uri, json=data, timeout=1, headers=headers) + response = requests.post(uri, json=data, timeout=1, headers=headers) + print("Return code: " + str(response.status_code)) except requests.exceptions.RequestException as e: - pass + print(e) def main(): parser = argparse.ArgumentParser( diff --git a/server/src/controller/ttnController.ts b/server/src/controller/ttnController.ts index 448e9b2..a59b1d7 100644 --- a/server/src/controller/ttnController.ts +++ b/server/src/controller/ttnController.ts @@ -7,6 +7,8 @@ import { LpTtnEndDeviceUplinksService } from "../services/lpTtnEndDeviceUplinksS import { TtnGatewayReceptionService } from "../services/ttnGatewayReceptionService"; import { WifiScanService } from "../services/wifiScanService"; import { ttnMessageValidator } from "../validation/ttn/ttnMessageValidation"; +import { authenticateHeader } from "../middleware/authentificationMiddleware"; +import { StatusCodes } from "http-status-codes"; const lpTtnEndDeviceUplinksService = container.resolve( LpTtnEndDeviceUplinksService @@ -20,44 +22,10 @@ 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), + [authenticateHeader, validateData(ttnMessageValidator)], async (req: Request, res: Response) => { - try { - const authorizationHeader = req.headers['authorization']; - - if (!validateBearerToken(authorizationHeader as string)) { - res.status(401).json({ error: "Authentication failed" }); - return; - } - //console.log("Bearer token is correct!"); - - } catch (error) { - console.error("Error during authentication:", error); - res.status(401).json({ error: "Authentication failed" }); - } - try { const message = req.body as TtnMessage; @@ -137,10 +105,10 @@ router.post( }); }; createDatabaseEntries().then(); - res.status(200); + res.status(StatusCodes.OK).send(); } catch (error) { console.log(error); - res.status(500).json({ error: "Error creating uplink" }); + res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: "Error creating uplink" }); } } ); diff --git a/server/src/middleware/authentificationMiddleware.ts b/server/src/middleware/authentificationMiddleware.ts new file mode 100644 index 0000000..a6629a4 --- /dev/null +++ b/server/src/middleware/authentificationMiddleware.ts @@ -0,0 +1,42 @@ +import { NextFunction, Request, Response } from "express"; +import { StatusCodes } from "http-status-codes"; + +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; +}; + +export function authenticateHeader(req: Request, res: Response, next: NextFunction) { + try { + const authorizationHeader = req.headers['authorization']; + + if (!validateBearerToken(authorizationHeader as string)) { + res.status(StatusCodes.UNAUTHORIZED).json({ error: "Authentication failed" }); + return; + } + console.log("Bearer token is correct!"); + + next(); + } catch (error) { + res.status(StatusCodes.INTERNAL_SERVER_ERROR) + .json({ error: "Internal Server Error" }); + } +}; + + + diff --git a/server/src/middleware/validationMiddleware.ts b/server/src/middleware/validationMiddleware.ts index 3f5288b..8dbeaf2 100644 --- a/server/src/middleware/validationMiddleware.ts +++ b/server/src/middleware/validationMiddleware.ts @@ -22,4 +22,4 @@ export function validateData(schema: z.ZodObject) { } } }; -} +} \ No newline at end of file