mirror of
https://github.com/fosrl/pangolin.git
synced 2026-01-28 22:00:51 +00:00
Small fixes around handling olm users
This commit is contained in:
@@ -229,6 +229,16 @@ export async function createClient(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const secretHash = await hashPassword(secret);
|
||||||
|
|
||||||
|
await trx.insert(olms).values({
|
||||||
|
olmId,
|
||||||
|
secretHash,
|
||||||
|
name,
|
||||||
|
clientId: newClient.clientId,
|
||||||
|
dateCreated: moment().toISOString()
|
||||||
|
});
|
||||||
|
|
||||||
return response<CreateClientResponse>(res, {
|
return response<CreateClientResponse>(res, {
|
||||||
data: newClient,
|
data: newClient,
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import {
|
|||||||
} from "@server/db";
|
} from "@server/db";
|
||||||
import { MessageHandler } from "@server/routers/ws";
|
import { MessageHandler } from "@server/routers/ws";
|
||||||
import { clients, clientSites, exitNodes, Olm, olms, sites } from "@server/db";
|
import { clients, clientSites, exitNodes, Olm, olms, sites } from "@server/db";
|
||||||
import { and, eq, inArray } from "drizzle-orm";
|
import { and, eq, inArray, isNull } from "drizzle-orm";
|
||||||
import { addPeer, deletePeer } from "../newt/peers";
|
import { addPeer, deletePeer } from "../newt/peers";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { listExitNodes } from "#dynamic/lib/exitNodes";
|
import { listExitNodes } from "#dynamic/lib/exitNodes";
|
||||||
@@ -40,7 +40,12 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
client = await getOrCreateOrgClient(orgId, olm.userId, olm.olmId, olm.name || "User Device");
|
client = await getOrCreateOrgClient(
|
||||||
|
orgId,
|
||||||
|
olm.userId,
|
||||||
|
olm.olmId,
|
||||||
|
olm.name || "User Device"
|
||||||
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(
|
logger.error(
|
||||||
`Error switching olm client ${olm.olmId} to org ${orgId}: ${err}`
|
`Error switching olm client ${olm.olmId} to org ${orgId}: ${err}`
|
||||||
@@ -292,7 +297,7 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
|
|||||||
|
|
||||||
async function getOrCreateOrgClient(
|
async function getOrCreateOrgClient(
|
||||||
orgId: string,
|
orgId: string,
|
||||||
userId: string,
|
userId: string | null,
|
||||||
olmId: string,
|
olmId: string,
|
||||||
name: string,
|
name: string,
|
||||||
trx: Transaction | typeof db = db
|
trx: Transaction | typeof db = db
|
||||||
@@ -314,17 +319,6 @@ async function getOrCreateOrgClient(
|
|||||||
throw new Error("Org has no subnet defined");
|
throw new Error("Org has no subnet defined");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify that the user belongs to the org
|
|
||||||
const [userOrg] = await trx
|
|
||||||
.select()
|
|
||||||
.from(userOrgs)
|
|
||||||
.where(and(eq(userOrgs.orgId, orgId), eq(userOrgs.userId, userId)))
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!userOrg) {
|
|
||||||
throw new Error("User does not belong to org");
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if the user has a client in the org and if not then create a client for them
|
// check if the user has a client in the org and if not then create a client for them
|
||||||
const [existingClient] = await trx
|
const [existingClient] = await trx
|
||||||
.select()
|
.select()
|
||||||
@@ -332,7 +326,7 @@ async function getOrCreateOrgClient(
|
|||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
eq(clients.orgId, orgId),
|
eq(clients.orgId, orgId),
|
||||||
eq(clients.userId, userId),
|
userId ? eq(clients.userId, userId) : isNull(clients.userId), // we dont check the user id if it is null because the olm is not tied to a user?
|
||||||
eq(clients.olmId, olmId)
|
eq(clients.olmId, olmId)
|
||||||
)
|
)
|
||||||
) // checking the olmid here because we want to create a new client PER OLM PER ORG
|
) // checking the olmid here because we want to create a new client PER OLM PER ORG
|
||||||
@@ -343,6 +337,21 @@ async function getOrCreateOrgClient(
|
|||||||
`Client does not exist in org ${orgId}, creating new client for user ${userId}`
|
`Client does not exist in org ${orgId}, creating new client for user ${userId}`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
throw new Error("User ID is required to create client in org");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that the user belongs to the org
|
||||||
|
const [userOrg] = await trx
|
||||||
|
.select()
|
||||||
|
.from(userOrgs)
|
||||||
|
.where(and(eq(userOrgs.orgId, orgId), eq(userOrgs.userId, userId)))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!userOrg) {
|
||||||
|
throw new Error("User does not belong to org");
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: more intelligent way to pick the exit node
|
// TODO: more intelligent way to pick the exit node
|
||||||
const exitNodesList = await listExitNodes(orgId);
|
const exitNodesList = await listExitNodes(orgId);
|
||||||
const randomExitNode =
|
const randomExitNode =
|
||||||
|
|||||||
Reference in New Issue
Block a user