mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-15 22:38:47 +00:00
공개 키 등록시 인증 절차 추가
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { and, eq, gt, lte } from "drizzle-orm";
|
||||
import db from "./drizzle";
|
||||
import { client, userClient } from "./schema";
|
||||
import { client, userClient, userClientChallenge, UserClientState } from "./schema";
|
||||
|
||||
export const createClient = async (pubKey: string, userId: number) => {
|
||||
await db.transaction(async (tx) => {
|
||||
return await db.transaction(async (tx) => {
|
||||
const insertRes = await tx.insert(client).values({ pubKey }).returning({ id: client.id });
|
||||
await tx.insert(userClient).values({
|
||||
userId,
|
||||
clientId: insertRes[0]!.id,
|
||||
});
|
||||
const { id: clientId } = insertRes[0]!;
|
||||
await tx.insert(userClient).values({ userId, clientId });
|
||||
|
||||
return clientId;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -25,3 +25,58 @@ export const getUserClient = async (userId: number, clientId: number) => {
|
||||
.execute();
|
||||
return userClients[0] ?? null;
|
||||
};
|
||||
|
||||
export const setUserClientStateToPending = async (userId: number, clientId: number) => {
|
||||
await db
|
||||
.update(userClient)
|
||||
.set({ state: UserClientState.Pending })
|
||||
.where(
|
||||
and(
|
||||
eq(userClient.userId, userId),
|
||||
eq(userClient.clientId, clientId),
|
||||
eq(userClient.state, UserClientState.Challenging),
|
||||
),
|
||||
)
|
||||
.execute();
|
||||
};
|
||||
|
||||
export const createUserClientChallenge = async (
|
||||
userId: number,
|
||||
clientId: number,
|
||||
challenge: string,
|
||||
allowedIp: string,
|
||||
expiresAt: number,
|
||||
) => {
|
||||
await db
|
||||
.insert(userClientChallenge)
|
||||
.values({
|
||||
userId,
|
||||
clientId,
|
||||
challenge,
|
||||
allowedIp,
|
||||
expiresAt,
|
||||
})
|
||||
.execute();
|
||||
};
|
||||
|
||||
export const getUserClientChallenge = async (challenge: string, ip: string) => {
|
||||
const challenges = await db
|
||||
.select()
|
||||
.from(userClientChallenge)
|
||||
.where(
|
||||
and(
|
||||
eq(userClientChallenge.challenge, challenge),
|
||||
eq(userClientChallenge.allowedIp, ip),
|
||||
gt(userClientChallenge.expiresAt, Date.now()),
|
||||
),
|
||||
)
|
||||
.execute();
|
||||
return challenges[0] ?? null;
|
||||
};
|
||||
|
||||
export const cleanupExpiredUserClientChallenges = async () => {
|
||||
await db
|
||||
.delete(userClientChallenge)
|
||||
.where(lte(userClientChallenge.expiresAt, Date.now()))
|
||||
.execute();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user