Scanauth return objects

This commit is contained in:
Nicolai Ort 2021-03-17 17:40:01 +01:00
parent 486e450a58
commit 46b7aceb0b

View File

@ -15,14 +15,14 @@ import authchecker from './authchecker';
const ScanAuth = async (req: Request, res: Response, next: () => void) => { const ScanAuth = async (req: Request, res: Response, next: () => void) => {
let provided_token: string = req.headers["authorization"]; let provided_token: string = req.headers["authorization"];
if (provided_token == "" || provided_token === undefined || provided_token === null) { if (provided_token == "" || provided_token === undefined || provided_token === null) {
res.status(401).send("No api token provided."); res.status(401).send({ http_code: 401, short: "no_token", message: "No api token provided." });
return; return;
} }
try { try {
provided_token = provided_token.replace("Bearer ", ""); provided_token = provided_token.replace("Bearer ", "");
} catch (error) { } catch (error) {
res.status(401).send("No valid jwt or api token provided."); res.status(401).send({ http_code: 401, short: "no_token", message: "No valid jwt or api token provided." });
return; return;
} }
@ -32,7 +32,7 @@ const ScanAuth = async (req: Request, res: Response, next: () => void) => {
} }
finally { finally {
if (prefix == "" || prefix == undefined || prefix == null) { if (prefix == "" || prefix == undefined || prefix == null) {
res.status(401).send("Api token non-existent or invalid syntax."); res.status(401).send({ http_code: 401, short: "invalid_token", message: "Api token non-existent or invalid syntax." });
return; return;
} }
} }
@ -46,7 +46,7 @@ const ScanAuth = async (req: Request, res: Response, next: () => void) => {
} }
finally { finally {
if (user_authorized == false) { if (user_authorized == false) {
res.status(401).send("Api token non-existent or invalid syntax."); res.status(401).send({ http_code: 401, short: "invalid_token", message: "Api token non-existent or invalid syntax." });
return; return;
} }
else { else {
@ -56,10 +56,10 @@ const ScanAuth = async (req: Request, res: Response, next: () => void) => {
} }
else { else {
if (station.enabled == false) { if (station.enabled == false) {
res.status(401).send("Station disabled."); res.status(401).send({ http_code: 401, short: "station_disabled", message: "Station is disabled." });
} }
if (!(await argon2.verify(station.key, provided_token))) { if (!(await argon2.verify(station.key, provided_token))) {
res.status(401).send("Api token invalid."); res.status(401).send({ http_code: 401, short: "invalid_token", message: "Api token non-existent or invalid syntax." });
return; return;
} }
req.headers["station_id"] = station.id.toString(); req.headers["station_id"] = station.id.toString();