mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-26 12:15:35 +02:00
remove api-key-org association for root keys
This commit is contained in:
parent
6d359b6bb9
commit
7402590f49
11 changed files with 47 additions and 26 deletions
|
@ -35,6 +35,11 @@ export async function verifyApiKeyApiKeyAccess(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (callerApiKey.isRoot) {
|
||||||
|
// Root keys can access any key in any org
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
const [callerApiKeyOrg] = await db
|
const [callerApiKeyOrg] = await db
|
||||||
.select()
|
.select()
|
||||||
.from(apiKeyOrg)
|
.from(apiKeyOrg)
|
||||||
|
|
|
@ -28,6 +28,11 @@ export async function verifyApiKeyClientAccess(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (apiKey.isRoot) {
|
||||||
|
// Root keys can access any key in any org
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
const client = await db
|
const client = await db
|
||||||
.select()
|
.select()
|
||||||
.from(clients)
|
.from(clients)
|
||||||
|
|
|
@ -27,6 +27,11 @@ export async function verifyApiKeyOrgAccess(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (req.apiKey?.isRoot) {
|
||||||
|
// Root keys can access any key in any org
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
if (!req.apiKeyOrg) {
|
if (!req.apiKeyOrg) {
|
||||||
const apiKeyOrgRes = await db
|
const apiKeyOrgRes = await db
|
||||||
.select()
|
.select()
|
||||||
|
|
|
@ -37,6 +37,11 @@ export async function verifyApiKeyResourceAccess(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (apiKey.isRoot) {
|
||||||
|
// Root keys can access any key in any org
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
if (!resource.orgId) {
|
if (!resource.orgId) {
|
||||||
return next(
|
return next(
|
||||||
createHttpError(
|
createHttpError(
|
||||||
|
|
|
@ -45,6 +45,11 @@ export async function verifyApiKeyRoleAccess(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (apiKey.isRoot) {
|
||||||
|
// Root keys can access any key in any org
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
const orgIds = new Set(rolesData.map((role) => role.orgId));
|
const orgIds = new Set(rolesData.map((role) => role.orgId));
|
||||||
|
|
||||||
for (const role of rolesData) {
|
for (const role of rolesData) {
|
||||||
|
|
|
@ -32,6 +32,11 @@ export async function verifyApiKeySetResourceUsers(
|
||||||
return next(createHttpError(HttpCode.BAD_REQUEST, "Invalid user IDs"));
|
return next(createHttpError(HttpCode.BAD_REQUEST, "Invalid user IDs"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (apiKey.isRoot) {
|
||||||
|
// Root keys can access any key in any org
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
if (userIds.length === 0) {
|
if (userIds.length === 0) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { db } from "@server/db";
|
import { db } from "@server/db";
|
||||||
import {
|
import { sites, apiKeyOrg } from "@server/db";
|
||||||
sites,
|
|
||||||
apiKeyOrg
|
|
||||||
} from "@server/db";
|
|
||||||
import { and, eq, or } from "drizzle-orm";
|
import { and, eq, or } from "drizzle-orm";
|
||||||
import createHttpError from "http-errors";
|
import createHttpError from "http-errors";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
@ -31,6 +28,11 @@ export async function verifyApiKeySiteAccess(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (apiKey.isRoot) {
|
||||||
|
// Root keys can access any key in any org
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
const site = await db
|
const site = await db
|
||||||
.select()
|
.select()
|
||||||
.from(sites)
|
.from(sites)
|
||||||
|
|
|
@ -66,6 +66,11 @@ export async function verifyApiKeyTargetAccess(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (apiKey.isRoot) {
|
||||||
|
// Root keys can access any key in any org
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
if (!resource.orgId) {
|
if (!resource.orgId) {
|
||||||
return next(
|
return next(
|
||||||
createHttpError(
|
createHttpError(
|
||||||
|
|
|
@ -27,6 +27,11 @@ export async function verifyApiKeyUserAccess(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (apiKey.isRoot) {
|
||||||
|
// Root keys can access any key in any org
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
if (!req.apiKeyOrg || !req.apiKeyOrg.orgId) {
|
if (!req.apiKeyOrg || !req.apiKeyOrg.orgId) {
|
||||||
return next(
|
return next(
|
||||||
createHttpError(
|
createHttpError(
|
||||||
|
|
|
@ -63,15 +63,6 @@ export async function createRootApiKey(
|
||||||
lastChars,
|
lastChars,
|
||||||
isRoot: true
|
isRoot: true
|
||||||
});
|
});
|
||||||
|
|
||||||
const allOrgs = await trx.select().from(orgs);
|
|
||||||
|
|
||||||
for (const org of allOrgs) {
|
|
||||||
await trx.insert(apiKeyOrg).values({
|
|
||||||
apiKeyId,
|
|
||||||
orgId: org.orgId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -215,7 +215,7 @@ export async function createOrg(
|
||||||
orgId: newOrg[0].orgId,
|
orgId: newOrg[0].orgId,
|
||||||
roleId: roleId,
|
roleId: roleId,
|
||||||
isOwner: true
|
isOwner: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const memberRole = await trx
|
const memberRole = await trx
|
||||||
|
@ -234,18 +234,6 @@ export async function createOrg(
|
||||||
orgId
|
orgId
|
||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
|
|
||||||
const rootApiKeys = await trx
|
|
||||||
.select()
|
|
||||||
.from(apiKeys)
|
|
||||||
.where(eq(apiKeys.isRoot, true));
|
|
||||||
|
|
||||||
for (const apiKey of rootApiKeys) {
|
|
||||||
await trx.insert(apiKeyOrg).values({
|
|
||||||
apiKeyId: apiKey.apiKeyId,
|
|
||||||
orgId: newOrg[0].orgId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!org) {
|
if (!org) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue