mirror of
https://github.com/versia-pub/server.git
synced 2026-04-27 20:59:15 +02:00
feat(api): ✨ Implement Challenges API
This commit is contained in:
parent
924ff9b2d4
commit
8f9472b221
26 changed files with 2656 additions and 104 deletions
39
utils/challenges.ts
Normal file
39
utils/challenges.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { createChallenge } from "altcha-lib";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { db } from "~/drizzle/db";
|
||||
import { Challenges } from "~/drizzle/schema";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const generateChallenge = async (
|
||||
maxNumber = config.validation.challenges.difficulty,
|
||||
) => {
|
||||
const expirationDate = new Date(
|
||||
Date.now() + config.validation.challenges.expiration * 1000,
|
||||
);
|
||||
|
||||
const uuid = (await db.execute(sql<string>`SELECT uuid_generate_v7()`))
|
||||
.rows[0].uuid_generate_v7 as string;
|
||||
|
||||
const challenge = await createChallenge({
|
||||
hmacKey: config.validation.challenges.key,
|
||||
expires: expirationDate,
|
||||
maxNumber,
|
||||
algorithm: "SHA-256",
|
||||
params: {
|
||||
challenge_id: uuid,
|
||||
},
|
||||
});
|
||||
|
||||
const result = (
|
||||
await db
|
||||
.insert(Challenges)
|
||||
.values({
|
||||
id: uuid,
|
||||
challenge,
|
||||
expiresAt: expirationDate.toISOString(),
|
||||
})
|
||||
.returning()
|
||||
)[0];
|
||||
|
||||
return result;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue