Skip to content

Commit

Permalink
Hack in captcha.
Browse files Browse the repository at this point in the history
  • Loading branch information
Heathcorp committed Mar 13, 2024
1 parent a1a2127 commit 6e72442
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
7 changes: 5 additions & 2 deletions experiments/thebutton/src/pages/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const MainPage: Component = (props) => {
{ success: boolean; count?: number; reason?: string }
>(functions, 'buttonCount');
const pressButtonFunction = httpsCallable<
{ count?: number },
{ count?: number; turnstileToken: string },
{ success: boolean; reason?: string }
>(functions, 'buttonPressed');

Expand Down Expand Up @@ -91,7 +91,10 @@ const MainPage: Component = (props) => {
};
let fail: string | undefined;
setLoadingStatus('UPLOADING');
pressButtonFunction({ count: cachedSpooledPresses })
pressButtonFunction({
count: cachedSpooledPresses,
turnstileToken: turnstileToken() ?? '',
})
.then((value) => {
console.log(value);
if (!value.data.success) {
Expand Down
32 changes: 29 additions & 3 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { getDatabase, ServerValue } from "firebase-admin/database";
import { defineString } from "firebase-functions/params";

const CF_TURNSTILE_KEY = defineString("CF_TURNSTILE_KEY");
// CF_TURNSTILE_KEY.value()

initializeApp();

export const helloWorld = https.onRequest((request, response) => {
// logger.info("Hello logs!", { structuredData: true });
response.send("Hello from Firebase!" + " " + CF_TURNSTILE_KEY.value());
response.send("Hello from Firebase!");
});

// extreme cases where we need to shut off the button but still show a count
Expand Down Expand Up @@ -46,7 +47,7 @@ export const buttonCount = https.onCall(async (data, context) => {
});

export const buttonPressed = https.onCall(
async (data: { count?: number }, context) => {
async (data: { count?: number; turnstileToken: string }, context) => {
if (COUNT_FROZEN) {
return {success: false, reason: "count frozen"};
}
Expand All @@ -63,6 +64,31 @@ export const buttonPressed = https.onCall(
};
}

// Captcha time!
try {
if (!data.turnstileToken) {
return {
success: false,
reason: 'did not supply captcha response'
}
}

const url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
const formData = new FormData();
formData.set("secret", CF_TURNSTILE_KEY.value());
formData.set("response", data.turnstileToken);
const resp = await (fetch(url, {body: formData, method: 'POST'}));
const respBody = await resp.json();
if (!respBody.success) {
return { success: false, reason: 'captcha failed' };
}
} catch(err: any) {
console.error(err);
return {
success: false, reason: 'An error occurred when validating captcha.'
}
}

try {
const inc = data.count ?? 1;

Expand All @@ -76,7 +102,7 @@ export const buttonPressed = https.onCall(

return {
success: false,
reason: "An internal error occured.",
reason: "An internal error occurred.",
};
}
}
Expand Down

0 comments on commit 6e72442

Please sign in to comment.