Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions functions/api/verify.js

This file was deleted.

37 changes: 37 additions & 0 deletions src/pages/api/get-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const prerender = false;

export async function POST({ request, locals }) {
const body = await request.json();
const { username } = body;

if (!username) {
return new Response(JSON.stringify({ error: 'Username is required' }), { status: 400 });
}

const normalizedUsername = username.trim().toLowerCase();
const secret = locals.runtime.env.FLAG_SECRET;

if (!secret) {
return new Response(JSON.stringify({ error: 'Server configuration error' }), { status: 500 });
}

const encoder = new TextEncoder();
const keyData = encoder.encode(secret);
const messageData = encoder.encode(normalizedUsername);

const cryptoKey = await crypto.subtle.importKey(
'raw',
keyData,
{ name: 'HMAC', hash: 'SHA-256' },
false,
['sign']
);

const signature = await crypto.subtle.sign('HMAC', cryptoKey, messageData);
const hashArray = Array.from(new Uint8Array(signature));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');

return new Response(JSON.stringify({ flag: `CTF{${hashHex}}` }), {
headers: { 'Content-Type': 'application/json' },
});
}
46 changes: 46 additions & 0 deletions src/pages/api/verify-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export const prerender = false;

export async function POST({ request, locals }) {
const body = await request.json();
const { username, flag } = body;

if (!username || !flag) {
return new Response(JSON.stringify({ error: 'Username and flag are required' }), { status: 400 });
}

const normalizedUsername = username.trim().toLowerCase();
const secret = locals.runtime.env.FLAG_SECRET;

if (!secret) {
return new Response(JSON.stringify({ error: 'Server configuration error' }), { status: 500 });
}

const encoder = new TextEncoder();
const keyData = encoder.encode(secret);
const messageData = encoder.encode(normalizedUsername);

const cryptoKey = await crypto.subtle.importKey(
'raw',
keyData,
{ name: 'HMAC', hash: 'SHA-256' },
false,
['sign']
);

const signature = await crypto.subtle.sign('HMAC', cryptoKey, messageData);
const hashArray = Array.from(new Uint8Array(signature));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');

const expectedFlag = `CTF{${hashHex}}`;

if (flag === expectedFlag) {
return new Response(JSON.stringify({ success: true, message: 'Flag valid!' }), {
headers: { 'Content-Type': 'application/json' },
});
}

return new Response(JSON.stringify({ success: false, message: 'Flag invalid!' }), {
status: 400,
headers: { 'Content-Type': 'application/json' },
});
}
Loading