mirror of
https://github.com/detleph/server.git
synced 2026-09-04 00:26:03 +02:00
Update the admin revision on password change
+ Update type of revision to DateTime
This commit is contained in:
@@ -49,7 +49,7 @@ model Admin {
|
||||
name String // TODO: This should maybe be unique
|
||||
password String // TODO: Probably specify hash size (VarChar or some other type)
|
||||
permission_level AdminLevel @default(STANDARD)
|
||||
revision String @default(dbgenerated("gen_random_uuid()")) @db.Uuid
|
||||
revision DateTime @default(now())
|
||||
groups Group[]
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,18 @@ import { Request, Response } from "express";
|
||||
import { AdminLevel } from "@prisma/client";
|
||||
import argon2 from "argon2";
|
||||
import { DataType, generateInvalidBodyError } from "./common";
|
||||
import { authClient } from "../lib/redis";
|
||||
|
||||
export const regenerateRevision = async (pid: string) => {
|
||||
// TOOO: Add error handling
|
||||
const { revision } = await prisma.admin.update({
|
||||
where: { pid },
|
||||
data: { revision: new Date() },
|
||||
select: { revision: true },
|
||||
});
|
||||
|
||||
await authClient.set(pid, revision.toISOString());
|
||||
};
|
||||
|
||||
const AUTH_ERROR = {
|
||||
type: "failure",
|
||||
@@ -164,7 +176,8 @@ export const updateForeignPassword = async (
|
||||
}
|
||||
|
||||
if (req.auth.permission_level == "ELEVATED" && user_to_upate.permission_level == "STANDARD") {
|
||||
updatePasswordField(req.params.pid, req.body.new_password);
|
||||
await updatePasswordField(req.params.pid, req.body.new_password); // REVIEW: Should this be awaited?
|
||||
await regenerateRevision(req.params.pid);
|
||||
|
||||
res.status(200).json({
|
||||
type: "success",
|
||||
@@ -210,7 +223,8 @@ export const updateOwnPassword = async (req: Request<{}, {}, UpdatePasswordBody>
|
||||
}
|
||||
|
||||
if (await argon2.verify(user_to_upate.password, req.body.password, { type: argon2.argon2id })) {
|
||||
updatePasswordField(pid, req.body.new_password);
|
||||
await updatePasswordField(pid, req.body.new_password);
|
||||
await regenerateRevision(pid);
|
||||
|
||||
return res.status(200).json({
|
||||
type: "success",
|
||||
|
||||
@@ -21,7 +21,7 @@ function createAdminJWT(admin: Admin & { groups: Group[] }) {
|
||||
pid: admin.pid,
|
||||
name: admin.name,
|
||||
permission_level: admin.permission_level,
|
||||
revision: admin.revision,
|
||||
revision: admin.revision.toISOString(),
|
||||
groups: admin.groups.map((group) => group.pid),
|
||||
};
|
||||
|
||||
@@ -64,7 +64,7 @@ export const authenticateUser = async (req: Request<{}, {}, AuthenticateUserBody
|
||||
|
||||
if (await argon2.verify(user.password, password, { type: argon2.argon2id })) {
|
||||
// Set password revision ID in redis
|
||||
await authClient.set(user.pid, user.revision);
|
||||
await authClient.set(user.pid, user.revision.toISOString());
|
||||
|
||||
return res.status(200).json({
|
||||
type: "success",
|
||||
|
||||
@@ -62,7 +62,7 @@ export const requireAuthentication = async (req: Request, res: Response, next: N
|
||||
const user = await prisma.admin.findUnique({ where: { pid }, select: { revision: true } });
|
||||
|
||||
if (user) {
|
||||
db_revision = user.revision;
|
||||
db_revision = user.revision.toISOString();
|
||||
|
||||
await authClient.set(pid, db_revision);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user