mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-14 00:31:25 +10:00
fix: more eslint stuff
This commit is contained in:
@ -1,8 +1,5 @@
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import droplet from "@drop-oss/droplet";
|
||||
import type { CertificateStore} from "./ca-store";
|
||||
import { fsCertificateStore } from "./ca-store";
|
||||
import type { CertificateStore } from "./ca-store";
|
||||
|
||||
export type CertificateBundle = {
|
||||
priv: string;
|
||||
@ -50,7 +47,7 @@ export class CertificateAuthority {
|
||||
clientId,
|
||||
clientName,
|
||||
caCertificate.cert,
|
||||
caCertificate.priv
|
||||
caCertificate.priv,
|
||||
);
|
||||
const certBundle: CertificateBundle = {
|
||||
priv,
|
||||
@ -65,7 +62,7 @@ export class CertificateAuthority {
|
||||
|
||||
async fetchClientCertificate(clientId: string) {
|
||||
const isBlacklist = await this.certificateStore.checkBlacklistCertificate(
|
||||
`client:${clientId}`
|
||||
`client:${clientId}`,
|
||||
);
|
||||
if (isBlacklist) return undefined;
|
||||
return await this.certificateStore.fetch(`client:${clientId}`);
|
||||
|
||||
@ -18,8 +18,8 @@ export const validCapabilities = Object.values(InternalClientCapability);
|
||||
|
||||
export type CapabilityConfiguration = {
|
||||
[InternalClientCapability.PeerAPI]: { endpoints: string[] };
|
||||
[InternalClientCapability.UserStatus]: {};
|
||||
[InternalClientCapability.CloudSaves]: {};
|
||||
[InternalClientCapability.UserStatus]: object;
|
||||
[InternalClientCapability.CloudSaves]: object;
|
||||
};
|
||||
|
||||
class CapabilityManager {
|
||||
@ -53,7 +53,7 @@ class CapabilityManager {
|
||||
const serverCertificate = await ca.fetchClientCertificate("server");
|
||||
if (!serverCertificate)
|
||||
throw new Error(
|
||||
"CA not initialised properly - server mTLS certificate not present"
|
||||
"CA not initialised properly - server mTLS certificate not present",
|
||||
);
|
||||
const httpsAgent = new https.Agent({
|
||||
key: serverCertificate.priv,
|
||||
@ -70,7 +70,9 @@ class CapabilityManager {
|
||||
});
|
||||
valid = true;
|
||||
break;
|
||||
} catch {}
|
||||
} catch {
|
||||
/* empty */
|
||||
}
|
||||
}
|
||||
|
||||
return valid;
|
||||
@ -81,7 +83,7 @@ class CapabilityManager {
|
||||
|
||||
async validateCapabilityConfiguration(
|
||||
capability: InternalClientCapability,
|
||||
configuration: object
|
||||
configuration: object,
|
||||
) {
|
||||
const validationFunction = this.validationFunctions[capability];
|
||||
if (!validationFunction) return false;
|
||||
@ -91,7 +93,7 @@ class CapabilityManager {
|
||||
async upsertClientCapability(
|
||||
capability: InternalClientCapability,
|
||||
rawCapability: object,
|
||||
clientId: string
|
||||
clientId: string,
|
||||
) {
|
||||
const upsertFunctions: EnumDictionary<
|
||||
InternalClientCapability,
|
||||
|
||||
@ -6,7 +6,7 @@ import { useCertificateAuthority } from "~/server/plugins/ca";
|
||||
|
||||
export type EventHandlerFunction<T> = (
|
||||
h3: H3Event<EventHandlerRequest>,
|
||||
utils: ClientUtils
|
||||
utils: ClientUtils,
|
||||
) => Promise<T> | T;
|
||||
|
||||
type ClientUtils = {
|
||||
@ -25,7 +25,7 @@ export function defineClientEventHandler<T>(handler: EventHandlerFunction<T>) {
|
||||
|
||||
let clientId: string;
|
||||
switch (method) {
|
||||
case "Debug":
|
||||
case "Debug": {
|
||||
if (!import.meta.dev) throw createError({ statusCode: 403 });
|
||||
const client = await prisma.client.findFirst({ select: { id: true } });
|
||||
if (!client)
|
||||
@ -35,7 +35,8 @@ export function defineClientEventHandler<T>(handler: EventHandlerFunction<T>) {
|
||||
});
|
||||
clientId = client.id;
|
||||
break;
|
||||
case "Nonce":
|
||||
}
|
||||
case "Nonce": {
|
||||
clientId = parts[0];
|
||||
const nonce = parts[1];
|
||||
const signature = parts[2];
|
||||
@ -59,9 +60,8 @@ export function defineClientEventHandler<T>(handler: EventHandlerFunction<T>) {
|
||||
}
|
||||
|
||||
const certificateAuthority = useCertificateAuthority();
|
||||
const certBundle = await certificateAuthority.fetchClientCertificate(
|
||||
clientId
|
||||
);
|
||||
const certBundle =
|
||||
await certificateAuthority.fetchClientCertificate(clientId);
|
||||
// This does the blacklist check already
|
||||
if (!certBundle)
|
||||
throw createError({
|
||||
@ -76,11 +76,13 @@ export function defineClientEventHandler<T>(handler: EventHandlerFunction<T>) {
|
||||
statusMessage: "Invalid nonce signature.",
|
||||
});
|
||||
break;
|
||||
default:
|
||||
}
|
||||
default: {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
statusMessage: "No authentication",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (clientId === undefined)
|
||||
@ -95,7 +97,7 @@ export function defineClientEventHandler<T>(handler: EventHandlerFunction<T>) {
|
||||
});
|
||||
if (!client)
|
||||
throw new Error(
|
||||
"client util fetch client broke - this should NOT happen"
|
||||
"client util fetch client broke - this should NOT happen",
|
||||
);
|
||||
return client;
|
||||
}
|
||||
@ -110,7 +112,7 @@ export function defineClientEventHandler<T>(handler: EventHandlerFunction<T>) {
|
||||
|
||||
if (!client)
|
||||
throw new Error(
|
||||
"client util fetch client broke - this should NOT happen"
|
||||
"client util fetch client broke - this should NOT happen",
|
||||
);
|
||||
|
||||
return client.user;
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { CertificateBundle } from "./ca";
|
||||
import prisma from "../db/database";
|
||||
import type { Platform } from "@prisma/client";
|
||||
import { useCertificateAuthority } from "~/server/plugins/ca";
|
||||
@ -10,25 +9,29 @@ export interface ClientMetadata {
|
||||
}
|
||||
|
||||
export class ClientHandler {
|
||||
private temporaryClientTable: {
|
||||
[key: string]: {
|
||||
private temporaryClientTable = new Map<
|
||||
string,
|
||||
{
|
||||
timeout: NodeJS.Timeout;
|
||||
data: ClientMetadata;
|
||||
userId?: string;
|
||||
authToken?: string;
|
||||
};
|
||||
} = {};
|
||||
}
|
||||
>();
|
||||
|
||||
async initiate(metadata: ClientMetadata) {
|
||||
const clientId = randomUUID();
|
||||
|
||||
this.temporaryClientTable[clientId] = {
|
||||
this.temporaryClientTable.set(clientId, {
|
||||
data: metadata,
|
||||
timeout: setTimeout(() => {
|
||||
if (this.temporaryClientTable[clientId])
|
||||
delete this.temporaryClientTable[clientId];
|
||||
}, 1000 * 60 * 10), // 10 minutes
|
||||
};
|
||||
timeout: setTimeout(
|
||||
() => {
|
||||
if (this.temporaryClientTable.has(clientId))
|
||||
this.temporaryClientTable.delete(clientId);
|
||||
},
|
||||
1000 * 60 * 10,
|
||||
), // 10 minutes
|
||||
});
|
||||
|
||||
return clientId;
|
||||
}
|
||||
@ -38,23 +41,23 @@ export class ClientHandler {
|
||||
}
|
||||
|
||||
async fetchClient(clientId: string) {
|
||||
const entry = this.temporaryClientTable[clientId];
|
||||
const entry = this.temporaryClientTable.get(clientId);
|
||||
if (!entry) return undefined;
|
||||
return entry;
|
||||
}
|
||||
|
||||
async attachUserId(clientId: string, userId: string) {
|
||||
if (!this.temporaryClientTable[clientId])
|
||||
throw new Error("Invalid clientId for attaching userId");
|
||||
this.temporaryClientTable[clientId].userId = userId;
|
||||
const clientTable = this.temporaryClientTable.get(clientId);
|
||||
if (!clientTable) throw new Error("Invalid clientId for attaching userId");
|
||||
clientTable.userId = userId;
|
||||
}
|
||||
|
||||
async generateAuthToken(clientId: string) {
|
||||
const entry = this.temporaryClientTable[clientId];
|
||||
const entry = this.temporaryClientTable.get(clientId);
|
||||
if (!entry) throw new Error("Invalid clientId to generate token");
|
||||
|
||||
const token = randomUUID();
|
||||
this.temporaryClientTable[clientId].authToken = token;
|
||||
entry.authToken = token;
|
||||
|
||||
return token;
|
||||
}
|
||||
@ -66,7 +69,7 @@ export class ClientHandler {
|
||||
}
|
||||
|
||||
async finialiseClient(id: string) {
|
||||
const metadata = this.temporaryClientTable[id];
|
||||
const metadata = this.temporaryClientTable.get(id);
|
||||
if (!metadata) throw new Error("Invalid client ID");
|
||||
if (!metadata.userId) throw new Error("Un-authorized client ID");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user