first basic auth

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

View File

@ -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"

View File

@ -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

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,
});
};