Add basic authentication to the API
This commit is contained in:
parent
be470cda9e
commit
51a9783b5d
1 changed files with 27 additions and 2 deletions
29
src/main.ts
29
src/main.ts
|
|
@ -2,8 +2,7 @@
|
||||||
import "module-alias/register";
|
import "module-alias/register";
|
||||||
|
|
||||||
// Begin personal code
|
// Begin personal code
|
||||||
import { DB_DEFAULTS } from "@/constants";
|
import basic from "@hapi/basic";
|
||||||
import { Server } from "@hapi/hapi";
|
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import glob from "glob";
|
import glob from "glob";
|
||||||
import toml from "toml";
|
import toml from "toml";
|
||||||
|
|
@ -42,6 +41,32 @@ async function init() {
|
||||||
port: config.server.port,
|
port: config.server.port,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Setup authentication
|
||||||
|
server.register(basic);
|
||||||
|
server.auth.strategy(`simple`, `basic`, {
|
||||||
|
async validate(request: Request, user: string, pass: string, h: ResponseToolkit) {
|
||||||
|
|
||||||
|
// Are we attempting to authenticate, then use the auth password
|
||||||
|
if (request.path === `/discord/auth`) {
|
||||||
|
return {
|
||||||
|
isValid: config.discord.auth_password === pass,
|
||||||
|
credentials: { pass },
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Ensure the guild has a config
|
||||||
|
if (!config.guilds[user]) {
|
||||||
|
return { isValid: false, };
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
isValid: config.guilds[user].password === pass,
|
||||||
|
credentials: { user }
|
||||||
|
};
|
||||||
|
},
|
||||||
|
allowEmptyUsername: true,
|
||||||
|
});
|
||||||
|
server.auth.default(`simple`)
|
||||||
|
|
||||||
// Register all the routes
|
// Register all the routes
|
||||||
let files = glob.sync(
|
let files = glob.sync(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue