Check and prefer user token if provided

This commit is contained in:
Owen
2026-02-24 19:48:32 -08:00
parent b37e1d0cc0
commit 55e24df671

View File

@@ -1,4 +1,7 @@
import { generateSessionToken } from "@server/auth/sessions/app"; import {
generateSessionToken,
validateSessionToken
} from "@server/auth/sessions/app";
import { import {
clients, clients,
db, db,
@@ -26,8 +29,9 @@ import { APP_VERSION } from "@server/lib/consts";
export const olmGetTokenBodySchema = z.object({ export const olmGetTokenBodySchema = z.object({
olmId: z.string(), olmId: z.string(),
secret: z.string(), secret: z.string().optional(),
token: z.string().optional(), userToken: z.string().optional(),
token: z.string().optional(), // this is the olm token
orgId: z.string().optional() orgId: z.string().optional()
}); });
@@ -49,7 +53,7 @@ export async function getOlmToken(
); );
} }
const { olmId, secret, token, orgId } = parsedBody.data; const { olmId, secret, token, orgId, userToken } = parsedBody.data;
try { try {
if (token) { if (token) {
@@ -84,6 +88,24 @@ export async function getOlmToken(
); );
} }
if (userToken) {
const { session: userSession, user } =
await validateSessionToken(userToken);
if (!userSession || !user) {
return next(
createHttpError(HttpCode.BAD_REQUEST, "Invalid user token")
);
}
if (user.userId !== existingOlm.userId) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"User token does not match olm"
)
);
}
} else if (secret) {
// this is for backward compatibility, we want to move towards userToken but some old clients may still be using secret so we will support both for now
const validSecret = await verifyPassword( const validSecret = await verifyPassword(
secret, secret,
existingOlm.secretHash existingOlm.secretHash
@@ -99,6 +121,14 @@ export async function getOlmToken(
createHttpError(HttpCode.BAD_REQUEST, "Secret is incorrect") createHttpError(HttpCode.BAD_REQUEST, "Secret is incorrect")
); );
} }
} else {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"Either secret or userToken is required"
)
);
}
logger.debug("Creating new olm session token"); logger.debug("Creating new olm session token");