This commit is contained in:
May 2024-03-08 06:24:22 -08:00
parent 11e0494137
commit 10b4e2bf9e
4 changed files with 69 additions and 43 deletions

View file

@ -1,6 +1,4 @@
import bodyParser from "body-parser"
import { Hono } from "hono"
import {stream as startHonoStream} from "hono/streaming"
import * as Accounts from "../../../lib/accounts.js"
import * as auth from "../../../lib/auth.js"
import RangeParser, { type Range } from "range-parser"
@ -8,12 +6,11 @@ import ServeError from "../../../lib/errors.js"
import Files, { WebError } from "../../../lib/files.js"
import { getAccount, requiresPermissions } from "../../../lib/middleware.js"
import {Readable} from "node:stream"
import {ReadableStream as StreamWebReadable} from "node:stream/web"
import type {ReadableStream as StreamWebReadable} from "node:stream/web"
import formidable from "formidable"
import { HttpBindings } from "@hono/node-server"
import pkg from "../../../../../package.json" assert {type: "json"}
import { type StatusCode } from "hono/utils/http-status"
import { EventEmitter } from "node:events"
export let primaryApi = new Hono<{
Variables: {
account: Accounts.Account

View file

@ -30,37 +30,8 @@ const router = new Hono<{
router.use(getAccount)
export default function (files: Files) {
router.post("/login", async (ctx, res) => {
const body = await ctx.req.json()
if (
typeof body.username != "string" ||
typeof body.password != "string"
) {
ServeError(ctx, 400, "please provide a username or password")
return
}
if (auth.validate(getCookie(ctx, "auth")!)) {
ServeError(ctx, 400, "you are already logged in")
return
}
const account = Accounts.getFromUsername(body.username)
if (!account || !Accounts.password.check(account.id, body.password)) {
ServeError(ctx, 400, "username or password incorrect")
return
}
setCookie(ctx, "auth", auth.create(account.id, 3 * 24 * 60 * 60 * 1000), {
path: "/",
sameSite: "Strict",
secure: true,
httpOnly: true
})
ctx.status(200)
})
router.post("/create", async (ctx) => {
router.post("/", async (ctx) => {
const body = await ctx.req.json()
if (!Configuration.accounts.registrationEnabled) {
return ServeError(ctx, 403, "account registration disabled")
@ -115,15 +86,6 @@ export default function (files: Files) {
})
})
router.post("/logout", (ctx) => {
if (!auth.validate(getCookie(ctx, "auth")!)) {
return ServeError(ctx, 401, "not logged in")
}
auth.invalidate(getCookie(ctx, "auth")!)
return ctx.text("logged out")
})
router.put(
"/dfv",
requiresAccount,

View file

@ -6,6 +6,7 @@
"admin",
"public",
"file",
"session",
{
"file": "customization",
"to": "/account/customization"

View file

@ -0,0 +1,66 @@
// Modules
import { Hono } from "hono"
import { getCookie, setCookie } from "hono/cookie"
// Libs
import Files, { id_check_regex } from "../../../lib/files.js"
import * as Accounts from "../../../lib/accounts.js"
import * as auth from "../../../lib/auth.js"
import {
getAccount,
} from "../../../lib/middleware.js"
import ServeError from "../../../lib/errors.js"
const router = new Hono<{
Variables: {
account: Accounts.Account
}
}>()
router.use(getAccount)
export default function (files: Files) {
router.post("/", async (ctx, res) => {
const body = await ctx.req.json()
if (
typeof body.username != "string" ||
typeof body.password != "string"
) {
ServeError(ctx, 400, "please provide a username or password")
return
}
if (auth.validate(getCookie(ctx, "auth")!)) {
ServeError(ctx, 400, "you are already logged in")
return
}
const account = Accounts.getFromUsername(body.username)
if (!account || !Accounts.password.check(account.id, body.password)) {
ServeError(ctx, 400, "username or password incorrect")
return
}
setCookie(ctx, "auth", auth.create(account.id, 3 * 24 * 60 * 60 * 1000), {
path: "/",
sameSite: "Strict",
secure: true,
httpOnly: true
})
ctx.status(200)
})
router.delete("/", (ctx) => {
if (!auth.validate(getCookie(ctx, "auth")!)) {
return ServeError(ctx, 401, "not logged in")
}
auth.invalidate(getCookie(ctx, "auth")!)
return ctx.text("logged out")
})
return router
}