Compare commits

..

No commits in common. "f6db27a22511031c48add5f338d3195ab825ab35f1face21dfd1b5e2fa79a0be" and "e43cb76e4de248ba662e76035af53da69ae85ce84a9ce020c6cc9febe1f2b274" have entirely different histories.

6 changed files with 15 additions and 75 deletions

View File

@ -14,12 +14,6 @@ TODO
- Create new user for database: `GRANT ALL PRIVILEGES ON dev_locationhub.* TO 'dbuser'@'localhost' IDENTIFIED BY '1234';`
- Import tables: `/usr/bin/mariadb -u dbuser -p1234 dev_locationhub < server/sql/tables.sql`
### TTN Integration
Create new Webhook for application. Set base url and enable "Uplink message" to api `/api/ttn/webhook`.
Add a addidtional header:
- Type: `authorization`
- Value: `Bearer your-very-secure-token`
### Testing Webhook
- To test the webhook use the python script `ttn-webhook-dummy.py` to send prerecorded TTN Uplinks.
- To test the script you can use `while true; do echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nSuccess"; nc -l -p 8080 -q 1; done`

View File

@ -4,7 +4,6 @@ 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"

View File

@ -9,16 +9,11 @@ import json
import argparse
import random
def send_post_request(uri, data, token):
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
def send_post_request(uri, data):
try:
response = requests.post(uri, json=data, timeout=1, headers=headers)
print("Return code: " + str(response.status_code))
requests.post(uri, json=data, timeout=1)
except requests.exceptions.RequestException as e:
print(e)
pass
def main():
parser = argparse.ArgumentParser(
@ -29,11 +24,6 @@ def main():
type=str,
help="The URI to send POST requests to (e.g., http://127.0.0.1:8080/api)",
)
parser.add_argument(
"token",
type=str,
help="Bearer authorization token)",
)
parser.add_argument(
"directory",
type=str,
@ -56,7 +46,7 @@ def main():
try:
data = json.load(file)
print(f"Sending {args.directory} to {args.uri}")
send_post_request(args.uri, data, args.token)
send_post_request(args.uri, data)
except json.JSONDecodeError as e:
print(f"Error reading {args.directory}: {e}")
return
@ -77,7 +67,7 @@ def main():
try:
data = json.load(file)
print(f"Sending {filename} to {args.uri}")
send_post_request(args.uri, data, args.token)
send_post_request(args.uri, data)
except json.JSONDecodeError as e:
print(f"Error reading {filename}: {e}")
@ -88,7 +78,7 @@ def main():
try:
data = json.load(file)
print(f"Sending {filename} to {args.uri}")
send_post_request(args.uri, data, args.token)
send_post_request(args.uri, data)
input("Press Enter to send the next file...")
except json.JSONDecodeError as e:
print(f"Error reading {filename}: {e}")
@ -101,10 +91,11 @@ def main():
try:
data = json.load(file)
print(f"Sending {filename} to {args.uri}")
send_post_request(args.uri, data, args.token)
send_post_request(args.uri, data)
input("Press Enter to send another random file...")
except json.JSONDecodeError as e:
print(f"Error reading {filename}: {e}")
if __name__ == "__main__":
main()

View File

@ -7,8 +7,6 @@ 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
@ -23,7 +21,7 @@ const router = express.Router();
router.post(
"/webhook",
[authenticateHeader, validateData(ttnMessageValidator)],
validateData(ttnMessageValidator),
async (req: Request, res: Response) => {
try {
const message = req.body as TtnMessage;
@ -94,17 +92,17 @@ router.post(
gnss:
gnnsLocation.latitude && gnnsLocation.longitude
? {
latitude: gnnsLocation.latitude,
longitude: gnnsLocation.longitude,
}
latitude: gnnsLocation.latitude,
longitude: gnnsLocation.longitude,
}
: undefined,
});
};
createDatabaseEntries().then();
res.status(StatusCodes.OK).send();
res.status(200);
} catch (error) {
console.log(error);
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: "Error creating uplink" });
res.status(500).json({ error: "Error creating uplink" });
}
}
);

View File

@ -1,42 +0,0 @@
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" });
}
};

View File

@ -22,4 +22,4 @@ export function validateData(schema: z.ZodObject<any, any>) {
}
}
};
}
}