Files
impost/example-app/server/api/pow/challenge.post.ts
Zoe 570531fe32 Implement kCTF strategy
This implementation is pretty scuffed, but its more exploratory than anything else.
2025-11-21 16:20:07 +00:00

52 lines
1.5 KiB
TypeScript

import { defineEventHandler } from 'h3'
import { validate_challenge } from '@impost/lib/validator';
import * as z from 'zod';
import { outstandingChallenges } from '~~/server/utils/pow';
const challengeSchema = z.object({
challenge: z.string().startsWith("s."),
solution: z.string().startsWith("s.")
})
// post handler that takes in the challenge, and the nonce
export default defineEventHandler(async (event) => {
console.log(await readBody(event));
const body = await readValidatedBody(event, challengeSchema.safeParse);
if (!body.success) {
throw createError({
statusCode: 400,
statusMessage: 'Validation failed'
})
}
let { challenge, solution } = body.data;
const outstanding_challenge = outstandingChallenges.get(challenge);
if (outstanding_challenge === undefined) {
throw createError({
statusCode: 400,
statusMessage: 'Challenge not found'
})
}
// check if the challenge is valid
const challenge_valid = await validate_challenge(outstanding_challenge.challenge, solution);
console.log("CHALLENGE VALID", challenge_valid);
if (challenge_valid) {
// clear the challenge
clearTimeout(outstandingChallenges.get(challenge)!.timeout);
outstandingChallenges.delete(challenge);
return {
message: 'Challenge solved'
};
}
throw createError({
statusCode: 400,
statusMessage: 'Challenge is not valid'
})
})