mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-05 02:06:41 +00:00
feat(olm): reset/send new olm secret if a matching fingerprint is detected
This commit is contained in:
committed by
Owen Schwartz
parent
40e37b1798
commit
6c8757f230
@@ -861,6 +861,12 @@ authenticated.get(
|
|||||||
olm.getUserOlm
|
olm.getUserOlm
|
||||||
);
|
);
|
||||||
|
|
||||||
|
authenticated.post(
|
||||||
|
"/user/:userId/olm/recover",
|
||||||
|
verifyIsLoggedInUser,
|
||||||
|
olm.recoverOlmWithFingerprint
|
||||||
|
);
|
||||||
|
|
||||||
authenticated.put(
|
authenticated.put(
|
||||||
"/idp/oidc",
|
"/idp/oidc",
|
||||||
verifyUserIsServerAdmin,
|
verifyUserIsServerAdmin,
|
||||||
|
|||||||
@@ -9,3 +9,4 @@ export * from "./listUserOlms";
|
|||||||
export * from "./getUserOlm";
|
export * from "./getUserOlm";
|
||||||
export * from "./handleOlmServerPeerAddMessage";
|
export * from "./handleOlmServerPeerAddMessage";
|
||||||
export * from "./handleOlmUnRelayMessage";
|
export * from "./handleOlmUnRelayMessage";
|
||||||
|
export * from "./recoverOlmWithFingerprint";
|
||||||
|
|||||||
120
server/routers/olm/recoverOlmWithFingerprint.ts
Normal file
120
server/routers/olm/recoverOlmWithFingerprint.ts
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
import { db, fingerprints, olms } from "@server/db";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import { and, eq } from "drizzle-orm";
|
||||||
|
import { NextFunction, Request, Response } from "express";
|
||||||
|
import response from "@server/lib/response";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { fromError } from "zod-validation-error";
|
||||||
|
import { generateId } from "@server/auth/sessions/app";
|
||||||
|
import { hashPassword } from "@server/auth/password";
|
||||||
|
|
||||||
|
const paramsSchema = z
|
||||||
|
.object({
|
||||||
|
userId: z.string()
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
const bodySchema = z
|
||||||
|
.object({
|
||||||
|
platformFingerprint: z.string()
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
export async function recoverOlmWithFingerprint(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<any> {
|
||||||
|
try {
|
||||||
|
const parsedParams = paramsSchema.safeParse(req.params);
|
||||||
|
if (!parsedParams.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsedParams.error).toString()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { userId } = parsedParams.data;
|
||||||
|
|
||||||
|
const parsedBody = bodySchema.safeParse(req.body);
|
||||||
|
if (!parsedBody.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsedBody.error).toString()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { platformFingerprint } = parsedBody.data;
|
||||||
|
|
||||||
|
const result = await db
|
||||||
|
.select({
|
||||||
|
olm: olms,
|
||||||
|
fingerprint: fingerprints
|
||||||
|
})
|
||||||
|
.from(olms)
|
||||||
|
.innerJoin(fingerprints, eq(fingerprints.olmId, olms.olmId))
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(olms.userId, userId),
|
||||||
|
eq(olms.archived, false),
|
||||||
|
eq(fingerprints.platformFingerprint, platformFingerprint)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.orderBy(fingerprints.lastSeen);
|
||||||
|
|
||||||
|
if (!result || result.length == 0) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.NOT_FOUND,
|
||||||
|
"corresponding olm with this fingerprint not found"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.length > 1) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.CONFLICT,
|
||||||
|
"multiple matching fingerprints found, not resetting secrets"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [{ olm: foundOlm }] = result;
|
||||||
|
|
||||||
|
const newSecret = generateId(48);
|
||||||
|
const newSecretHash = await hashPassword(newSecret);
|
||||||
|
|
||||||
|
await db
|
||||||
|
.update(olms)
|
||||||
|
.set({
|
||||||
|
secretHash: newSecretHash
|
||||||
|
})
|
||||||
|
.where(eq(olms.olmId, foundOlm.olmId));
|
||||||
|
|
||||||
|
return response(res, {
|
||||||
|
data: {
|
||||||
|
olmId: foundOlm.olmId,
|
||||||
|
secret: newSecret
|
||||||
|
},
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Successfully retrieved olm",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||||||
|
"Failed to recover olm using provided fingerprint input"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user