Cleanup of unused code, moving mounts to plug

This commit is contained in:
Zef Hemel
2022-07-11 13:14:31 +02:00
parent bd462e3d42
commit 8d61812d0e
6 changed files with 1 additions and 429 deletions
-29
View File
@@ -1,29 +0,0 @@
import { beforeEach, afterEach, expect, test } from "@jest/globals";
import { unlink } from "fs/promises";
import knex, { Knex } from "knex";
import { Authenticator } from "./auth";
let db: Knex<any, unknown[]> | undefined;
beforeEach(async () => {
db = knex({
client: "better-sqlite3",
connection: {
filename: "auth-test.db",
},
useNullAsDefault: true,
});
});
afterEach(async () => {
db!.destroy();
await unlink("auth-test.db");
});
test("Test auth", async () => {
let auth = new Authenticator(db!);
await auth.ensureTables();
await auth.createAccount("admin", "admin");
expect(await auth.verify("admin", "admin")).toBe(true);
expect(await auth.verify("admin", "sup")).toBe(false);
});
-71
View File
@@ -1,71 +0,0 @@
import * as crypto from "crypto";
import { Knex } from "knex";
import { promisify } from "util";
const pbkdf2 = promisify(crypto.pbkdf2);
type Account = {
username: string;
hashed_password: any;
salt: any;
};
export class Authenticator {
tableName = "tokens";
constructor(private db: Knex<any, unknown[]>) {}
middleware(req: any, res: any, next: any) {
console.log("GOing through here", req.headers.authorization);
// if (req.headers)
next();
}
async ensureTables() {
if (!(await this.db.schema.hasTable(this.tableName))) {
await this.db.schema.createTable(this.tableName, (table) => {
table.string("username");
table.binary("hashed_password");
table.binary("salt");
table.primary(["username"]);
});
// await this.createAccount("admin", "admin");
console.log(`Created table ${this.tableName}`);
}
}
async createAccount(username: string, password: string) {
var salt = crypto.randomBytes(16);
let encryptedPassword = await pbkdf2(password, salt, 310000, 32, "sha256");
await this.db<Account>(this.tableName).insert({
username,
hashed_password: encryptedPassword,
salt,
});
}
async updatePassword(username: string, password: string) {
var salt = crypto.randomBytes(16);
let encryptedPassword = await pbkdf2(password, salt, 310000, 32, "sha256");
await this.db<Account>(this.tableName).update({
username,
hashed_password: encryptedPassword,
salt,
});
}
async verify(username: string, password: string): Promise<boolean> {
let users = await this.db<Account>(this.tableName).where({ username });
if (users.length === 0) {
throw new Error(`No such user: ${username}`);
}
let user = users[0];
let encryptedPassword = await pbkdf2(
password,
user.salt,
310000,
32,
"sha256"
);
return crypto.timingSafeEqual(user.hashed_password, encryptedPassword);
}
}
-3
View File
@@ -29,7 +29,6 @@ import { esbuildSyscalls } from "@plugos/plugos/syscalls/esbuild";
import { systemSyscalls } from "./syscalls/system";
import { plugPrefix } from "@silverbulletmd/common/spaces/constants";
import { Authenticator } from "./auth";
import sandboxSyscalls from "@plugos/plugos/syscalls/sandbox";
// import globalModules from "../common/dist/global.plug.json";
@@ -265,8 +264,6 @@ export class ExpressServer {
await ensureFTSTable(this.db, "fts");
await this.ensureIndexPage();
let auth = new Authenticator(this.db);
// Serve static files (javascript, css, html)
this.app.use("/", express.static(this.distDir));
+1 -3
View File
@@ -27,9 +27,7 @@
]
},
"test": {
"source": [
"auth.test.ts"
],
"source": [],
"outputFormat": "commonjs",
"isLibrary": true,
"context": "node"