Skip to content

Commit

Permalink
Added invisible captcha
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalmishraa committed Jul 31, 2024
1 parent 077941c commit ef34d70
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
33 changes: 30 additions & 3 deletions apps/web/app/api/submission/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { isRequestAllowed } from "@/app/lib/redis";


const JUDGE0_URI = process.env.JUDGE0_URI;
const CLOUDFLARE_TURNSTILE_SECRET_KEY = process.env.CLOUDFLARE_TURNSTILE_SECRET_KEY!;
const CLOUDFLARE_TURNSTILE_URL = "https://challenges.cloudflare.com/turnstile/v0/siteverify";


export async function POST(req: NextRequest) {
Expand All @@ -36,6 +38,7 @@ export async function POST(req: NextRequest) {
};



//get and check the submission input
const submissionInput = SubmissionInput.safeParse(await req.json());
if (!submissionInput.success) {
Expand All @@ -47,6 +50,30 @@ export async function POST(req: NextRequest) {
);
};

if (process.env.NODE_ENV === "production") {
let formData = new FormData();
formData.append("secret", CLOUDFLARE_TURNSTILE_SECRET_KEY!);
formData.append("response", submissionInput.data.token);

const result = await fetch(CLOUDFLARE_TURNSTILE_URL, {
body: formData,
method: 'POST',
});

const outcome = await result.json();

if (!outcome.success && process.env.NODE_ENV == "production") {
return NextResponse.json(
{
message: "Please try again! something went wrong",
status: 403,
}
);
}
}



//get the problem
const dbProblem = await db.problem.findUnique({
where: {
Expand Down Expand Up @@ -89,7 +116,7 @@ export async function POST(req: NextRequest) {
})),
}
);


//create the submission in the database
const submission = await db.submission.create({
Expand Down Expand Up @@ -153,7 +180,7 @@ export async function GET(req: NextRequest) {
},
});

if(submission?.status === "AC"){
if (submission?.status === "AC") {
const updatedProblem = await db.problem.update({
where: {
id: submission.problemId,
Expand All @@ -174,7 +201,7 @@ export async function GET(req: NextRequest) {
);
}
}

if (!submission) {
return NextResponse.json(
{
Expand Down
15 changes: 11 additions & 4 deletions apps/web/app/components/ProblemSubmitBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { signIn, useSession } from "next-auth/react";
import { submissions as SubmissionsType } from "@prisma/client";
import { Turnstile } from "@marsidev/react-turnstile";

const TURNSTILE_SITE_KEY = process.env.NEXT_PUBLIC_CLOUDFLARE_TURNSTILE_SITE_KEY || "0x4AAAAAAAc4qhUEsytXspC_";
const TURNSTILE_SITE_KEY = process.env.NEXT_PUBLIC_CLOUDFLARE_TURNSTILE_SITE_KEY || "0x4AAAAAAAgE_1Uk6Lva-ewz";

enum SubmitStatus {
SUBMIT = "SUBMIT",
Expand Down Expand Up @@ -106,6 +106,7 @@ function SubmitProblem({
setCode(defaultCode);
}, [problem]);


async function pollWithBackoff(id: string, retries: number) {

if (retries === 0) {
Expand Down Expand Up @@ -144,16 +145,16 @@ function SubmitProblem({
languageId: language,
problemId: problem.id,
activeContestId: contestId,
token: token,
token: token
});

if(response.status === 429){
if (response.status === 429) {
setStatus(SubmitStatus.FAILED);
toast.error("Try again after sometime");
return;
};

if(response.status != 200){
if (response.status != 200) {
setStatus(SubmitStatus.FAILED);
toast.error("Failed to submit");
return;
Expand Down Expand Up @@ -204,6 +205,12 @@ function SubmitProblem({
/>
</div>
<div className="flex justify-end">
{process.env.NODE_ENV === "production" && (
<Turnstile
onSuccess={(token) => setToken(token)}
siteKey={TURNSTILE_SITE_KEY}
/>
)};
<Button
disabled={status === SubmitStatus.PENDING}
type="submit"
Expand Down
3 changes: 2 additions & 1 deletion k8s/5-web/configmap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ metadata:
name: algoearth-web-config
data:
MOUNT_PATH: /dev/problems
NEXTAUTH_URL: https://algoearth.vishalmishra.tech
NEXTAUTH_URL: https://algoearth.vishalmishra.tech
NODE_ENV : production
21 changes: 18 additions & 3 deletions k8s/5-web/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ spec:
image: vishal022/algoearth-web:latest
ports:
- containerPort: 3000
volumeMounts:
- name: problems
mountPath: /dev/problems
env:
- name: DATABASE_URL
valueFrom:
Expand All @@ -39,19 +42,31 @@ spec:
secretKeyRef:
name: algoearth-web-secret
key: JUDGE0_URI
- name: CLOUDFLARE_TURNSTILE_SITE_KEY
valueFrom:
secretKeyRef:
name: algoearth-web-secret
key: CLOUDFLARE_TURNSTILE_SITE_KEY
- name: CLOUDFLARE_TURNSTILE_SECRET_KEY
valueFrom:
secretKeyRef:
name: algoearth-web-secret
key: CLOUDFLARE_TURNSTILE_SECRET_KEY
- name: MOUNT_PATH
valueFrom:
configMapKeyRef:
name: algoearth-web-config
key: MOUNT_PATH
- name: NODE_ENV
valueFrom:
configMapKeyRef:
name: algoearth-web-config
key: NODE_ENV
- name: NEXTAUTH_URL
valueFrom:
configMapKeyRef:
name: algoearth-web-config
key: NEXTAUTH_URL
volumeMounts:
- name: problems
mountPath: /problems
volumes:
- name: problems
persistentVolumeClaim:
Expand Down
2 changes: 2 additions & 0 deletions k8s/5-web/secret.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ data:
REDIS_URL:
NEXTAUTH_SECRET:
JUDGE0_URI:
CLOUDFLARE_TURNSTILE_SECRET_KEY:
CLOUDFLARE_TURNSTILE_SITE_KEY:
2 changes: 1 addition & 1 deletion packages/common/zod/submissionInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export const SubmissionInput = z.object({
languageId: z.enum(["js", "cpp", "rs","java"]),
problemId: z.string(),
activeContestId: z.string().optional(),
token: z.string(),
token: z.string(),
});

0 comments on commit ef34d70

Please sign in to comment.