mirror of
https://github.com/mollersuite/monofile.git
synced 2024-11-21 21:36:26 -08:00
hwhw
This commit is contained in:
parent
11e0494137
commit
10b4e2bf9e
|
@ -1,6 +1,4 @@
|
||||||
import bodyParser from "body-parser"
|
|
||||||
import { Hono } from "hono"
|
import { Hono } from "hono"
|
||||||
import {stream as startHonoStream} from "hono/streaming"
|
|
||||||
import * as Accounts from "../../../lib/accounts.js"
|
import * as Accounts from "../../../lib/accounts.js"
|
||||||
import * as auth from "../../../lib/auth.js"
|
import * as auth from "../../../lib/auth.js"
|
||||||
import RangeParser, { type Range } from "range-parser"
|
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 Files, { WebError } from "../../../lib/files.js"
|
||||||
import { getAccount, requiresPermissions } from "../../../lib/middleware.js"
|
import { getAccount, requiresPermissions } from "../../../lib/middleware.js"
|
||||||
import {Readable} from "node:stream"
|
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 formidable from "formidable"
|
||||||
import { HttpBindings } from "@hono/node-server"
|
import { HttpBindings } from "@hono/node-server"
|
||||||
import pkg from "../../../../../package.json" assert {type: "json"}
|
import pkg from "../../../../../package.json" assert {type: "json"}
|
||||||
import { type StatusCode } from "hono/utils/http-status"
|
import { type StatusCode } from "hono/utils/http-status"
|
||||||
import { EventEmitter } from "node:events"
|
|
||||||
export let primaryApi = new Hono<{
|
export let primaryApi = new Hono<{
|
||||||
Variables: {
|
Variables: {
|
||||||
account: Accounts.Account
|
account: Accounts.Account
|
||||||
|
|
|
@ -30,37 +30,8 @@ const router = new Hono<{
|
||||||
router.use(getAccount)
|
router.use(getAccount)
|
||||||
|
|
||||||
export default function (files: Files) {
|
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")!)) {
|
router.post("/", async (ctx) => {
|
||||||
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) => {
|
|
||||||
const body = await ctx.req.json()
|
const body = await ctx.req.json()
|
||||||
if (!Configuration.accounts.registrationEnabled) {
|
if (!Configuration.accounts.registrationEnabled) {
|
||||||
return ServeError(ctx, 403, "account registration disabled")
|
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(
|
router.put(
|
||||||
"/dfv",
|
"/dfv",
|
||||||
requiresAccount,
|
requiresAccount,
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
"admin",
|
"admin",
|
||||||
"public",
|
"public",
|
||||||
"file",
|
"file",
|
||||||
|
"session",
|
||||||
{
|
{
|
||||||
"file": "customization",
|
"file": "customization",
|
||||||
"to": "/account/customization"
|
"to": "/account/customization"
|
||||||
|
|
66
src/server/routes/api/v1/session.ts
Normal file
66
src/server/routes/api/v1/session.ts
Normal 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
|
||||||
|
}
|
Loading…
Reference in a new issue