Allow api access
This commit is contained in:
parent
07263baf6a
commit
f2a8b1e58e
2 changed files with 59 additions and 1 deletions
27
packages/server/src/HTTPError.js
Normal file
27
packages/server/src/HTTPError.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* Represents an http error.
|
||||
*/
|
||||
export class HTTPError extends Error
|
||||
{
|
||||
/**
|
||||
* The http status code.
|
||||
*
|
||||
* @type {number}
|
||||
*/
|
||||
status;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the {@link HTTPError `HTTPError`} class.
|
||||
*
|
||||
* @param {number} status
|
||||
* The http status code.
|
||||
*
|
||||
* @param {string} message
|
||||
* The error message.
|
||||
*/
|
||||
constructor(status, message)
|
||||
{
|
||||
super(message);
|
||||
this.status = status;
|
||||
}
|
||||
}
|
|
@ -1,18 +1,49 @@
|
|||
import { join } from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import express from "express";
|
||||
import { HTTPError } from "./HTTPError.js";
|
||||
|
||||
const dirname = fileURLToPath(new URL(".", import.meta.url));
|
||||
const app = express();
|
||||
|
||||
const apiKeys = [
|
||||
"c4game"
|
||||
];
|
||||
|
||||
app.use(express.static(join(dirname, "..", "..", "game", "lib", "static")));
|
||||
app.use("/api", express.json());
|
||||
|
||||
app.use(
|
||||
"/api",
|
||||
(request, response, next) =>
|
||||
{
|
||||
const keyParam = "token";
|
||||
|
||||
if (keyParam in request.query)
|
||||
{
|
||||
let key = request.query[keyParam];
|
||||
|
||||
if (typeof key === "string" && apiKeys.includes(key))
|
||||
{
|
||||
next();
|
||||
}
|
||||
else
|
||||
{
|
||||
next(new HTTPError(401, "The specified API token is invalid"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
next(new HTTPError(401, "An API token is required"));
|
||||
}
|
||||
});
|
||||
|
||||
app.use(
|
||||
[
|
||||
(error, request, response, next) =>
|
||||
{
|
||||
response.send(`${error}`);
|
||||
response.status(error?.status ?? 500);
|
||||
response.status(error instanceof HTTPError ? error.status : 500);
|
||||
}
|
||||
]);
|
||||
|
||||
|
|
Loading…
Reference in a new issue