mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 15:08:46 +00:00
클라이언트 등록시 검증키도 등록하도록 변경 (WiP)
This commit is contained in:
@@ -1,10 +1,21 @@
|
||||
import { and, eq, gt, lte } from "drizzle-orm";
|
||||
import { and, or, eq, gt, lte, count } from "drizzle-orm";
|
||||
import db from "./drizzle";
|
||||
import { client, userClient, userClientChallenge } from "./schema";
|
||||
|
||||
export const createClient = async (pubKey: string, userId: number) => {
|
||||
export const createClient = async (encPubKey: string, sigPubKey: string, userId: number) => {
|
||||
return await db.transaction(async (tx) => {
|
||||
const insertRes = await tx.insert(client).values({ pubKey }).returning({ id: client.id });
|
||||
const clients = await tx
|
||||
.select()
|
||||
.from(client)
|
||||
.where(or(eq(client.encPubKey, sigPubKey), eq(client.sigPubKey, encPubKey)));
|
||||
if (clients.length > 0) {
|
||||
throw new Error("Already used public key(s)");
|
||||
}
|
||||
|
||||
const insertRes = await tx
|
||||
.insert(client)
|
||||
.values({ encPubKey, sigPubKey })
|
||||
.returning({ id: client.id });
|
||||
const { id: clientId } = insertRes[0]!;
|
||||
await tx.insert(userClient).values({ userId, clientId });
|
||||
|
||||
@@ -12,11 +23,28 @@ export const createClient = async (pubKey: string, userId: number) => {
|
||||
});
|
||||
};
|
||||
|
||||
export const getClientByPubKey = async (pubKey: string) => {
|
||||
const clients = await db.select().from(client).where(eq(client.pubKey, pubKey)).execute();
|
||||
export const getClient = async (clientId: number) => {
|
||||
const clients = await db.select().from(client).where(eq(client.id, clientId)).execute();
|
||||
return clients[0] ?? null;
|
||||
};
|
||||
|
||||
export const getClientByPubKeys = async (encPubKey: string, sigPubKey: string) => {
|
||||
const clients = await db
|
||||
.select()
|
||||
.from(client)
|
||||
.where(and(eq(client.encPubKey, encPubKey), eq(client.sigPubKey, sigPubKey)))
|
||||
.execute();
|
||||
return clients[0] ?? null;
|
||||
};
|
||||
|
||||
export const countClientByPubKey = async (pubKey: string) => {
|
||||
const clients = await db
|
||||
.select({ count: count() })
|
||||
.from(client)
|
||||
.where(or(eq(client.encPubKey, pubKey), eq(client.encPubKey, pubKey)));
|
||||
return clients[0]?.count ?? 0;
|
||||
};
|
||||
|
||||
export const createUserClient = async (userId: number, clientId: number) => {
|
||||
await db.insert(userClient).values({ userId, clientId }).execute();
|
||||
};
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
import { sqliteTable, text, integer, primaryKey } from "drizzle-orm/sqlite-core";
|
||||
import { sqliteTable, text, integer, primaryKey, unique } from "drizzle-orm/sqlite-core";
|
||||
import { user } from "./user";
|
||||
|
||||
export const client = sqliteTable("client", {
|
||||
id: integer("id").primaryKey(),
|
||||
pubKey: text("public_key").notNull().unique(), // Base64
|
||||
});
|
||||
export const client = sqliteTable(
|
||||
"client",
|
||||
{
|
||||
id: integer("id").primaryKey(),
|
||||
encPubKey: text("encryption_public_key").notNull().unique(), // Base64
|
||||
sigPubKey: text("signature_public_key").notNull().unique(), // Base64
|
||||
},
|
||||
(t) => ({
|
||||
unq: unique().on(t.encPubKey, t.sigPubKey),
|
||||
}),
|
||||
);
|
||||
|
||||
export const userClient = sqliteTable(
|
||||
"user_client",
|
||||
|
||||
Reference in New Issue
Block a user