switch back to json

This commit is contained in:
Huskydog9988
2025-03-23 20:29:50 -04:00
parent c1272dc7a7
commit 690aa042df
4 changed files with 34 additions and 28 deletions

View File

@ -7,8 +7,7 @@ model LinkedAuthMec {
mec AuthMec mec AuthMec
enabled Boolean @default(true) enabled Boolean @default(true)
credentials Json // TODO: remove this, automate via migration credentials Json
password String?
user User @relation(fields: [userId], references: [id]) user User @relation(fields: [userId], references: [id])

View File

@ -1,9 +1,11 @@
import { AuthMec } from "@prisma/client"; import { AuthMec } from "@prisma/client";
import { JsonArray } from "@prisma/client/runtime/library"; import { JsonArray } from "@prisma/client/runtime/library";
import { type } from "arktype";
import prisma from "~/server/internal/db/database"; import prisma from "~/server/internal/db/database";
import { import {
checkHashArgon2, checkHashArgon2,
checkHashBcrypt, checkHashBcrypt,
simpleAuth,
} from "~/server/internal/security/simple"; } from "~/server/internal/security/simple";
import sessionHandler from "~/server/internal/session"; import sessionHandler from "~/server/internal/session";
@ -23,19 +25,9 @@ export default defineEventHandler(async (h3) => {
where: { where: {
mec: AuthMec.Simple, mec: AuthMec.Simple,
enabled: true, enabled: true,
OR: [ user: {
{ username,
// TODO: check if this is even needed with below condition },
credentials: {
array_starts_with: username,
},
},
{
user: {
username,
},
},
],
}, },
include: { include: {
user: { user: {
@ -81,13 +73,18 @@ export default defineEventHandler(async (h3) => {
} else { } else {
// using new (modern) login flow // using new (modern) login flow
if (authMek.password === null) const creds = simpleAuth(authMek.credentials);
if (creds instanceof type.errors) {
// hover out.summary to see validation errors
console.error(creds.summary);
throw createError({ throw createError({
statusCode: 500, statusCode: 400,
statusMessage: statusMessage: creds.summary,
"Invalid password state. Please contact the server administrator.",
}); });
else if (!(await checkHashArgon2(password, authMek.password))) }
if (!(await checkHashArgon2(password, creds.password)))
throw createError({ throw createError({
statusCode: 401, statusCode: 401,
statusMessage: "Invalid username or password.", statusMessage: "Invalid username or password.",

View File

@ -1,6 +1,10 @@
import { AuthMec, Invitation } from "@prisma/client"; import { AuthMec, Invitation } from "@prisma/client";
import prisma from "~/server/internal/db/database"; import prisma from "~/server/internal/db/database";
import { createHashArgon2 } from "~/server/internal/security/simple"; import {
createHashArgon2,
simpleAuth,
SimpleAuthType,
} from "~/server/internal/security/simple";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import * as jdenticon from "jdenticon"; import * as jdenticon from "jdenticon";
import objectHandler from "~/server/internal/objects"; import objectHandler from "~/server/internal/objects";
@ -67,13 +71,16 @@ export default defineEventHandler(async (h3) => {
[`internal:read`, `${userId}:write`] [`internal:read`, `${userId}:write`]
); );
const hash = await createHashArgon2(user.password); const creds: SimpleAuthType = {
version: "v1.0.0",
password: await createHashArgon2(user.password),
};
const [linkMec] = await prisma.$transaction([ const [linkMec] = await prisma.$transaction([
prisma.linkedAuthMec.create({ prisma.linkedAuthMec.create({
data: { data: {
mec: AuthMec.Simple, mec: AuthMec.Simple,
credentials: {}, credentials: creds,
password: hash,
user: { user: {
create: { create: {
id: userId, id: userId,

View File

@ -1,10 +1,13 @@
import bcrypt from "bcryptjs"; import bcrypt from "bcryptjs";
import * as argon2 from "argon2"; import * as argon2 from "argon2";
import { type } from "arktype";
// const bcryptRounds = 10; export const simpleAuth = type({
// export async function createHashBcrypt(password: string) { version: "string.semver",
// return await bcrypt.hash(password, bcryptRounds); password: "string",
// } });
export type SimpleAuthType = typeof simpleAuth.infer;
export async function checkHashBcrypt(password: string, hash: string) { export async function checkHashBcrypt(password: string, hash: string) {
return await bcrypt.compare(password, hash); return await bcrypt.compare(password, hash);