Skip to content

Commit

Permalink
feat: verify captcha
Browse files Browse the repository at this point in the history
  • Loading branch information
cptchloroplast committed Oct 7, 2024
1 parent 708021c commit 4351675
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 68 deletions.
65 changes: 0 additions & 65 deletions metadata.md

This file was deleted.

2 changes: 2 additions & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export type Environment = {
OAUTH_TENANT: string
EMAIL_OAUTH_AUDIENCE: string
EMAIL_OAUTH_SCOPE: string
HCAPTCHA_SECRET: string
HCAPTCHA_SITEKEY: string
}

type Runtime = import("@astrojs/cloudflare").Runtime<Environment>
Expand Down
27 changes: 24 additions & 3 deletions src/pages/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,33 @@ import { json } from "@utils"

export async function POST(context: APIContext) {
const data = await context.request.json<any>()
if (!data["h-captcha-response"]) return json({
const token = data["h-captcha-response"]
if (!token) return json({
ok: false,
message: "No robots allowed!"
message: "You skipped the captcha..."
})

// Do h-captcha verification
const response = await fetch("https://api.hcaptcha.com/siteverify", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
response: token,
secret: context.locals.runtime.env.HCAPTCHA_SECRET,
sitekey: context.locals.runtime.env.HCAPTCHA_SITEKEY,
})
})
const body = await response.json<{
success: boolean
challenge_ts: string
hostname: string
"error-codes": string[]
}>()
if (!body.success) return json({
ok: false,
message: "No robots allowed!"
})

return json({
ok: true,
Expand Down
1 change: 1 addition & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module "page" {
production_secrets = {
RSA_PRIVATE_KEY = var.RSA_PRIVATE_KEY
OAUTH_CLIENT_SECRET = module.client.client_secret
HCAPTCHA_SECRET = var.HCAPTCHA_SECRET
}

production_buckets = {
Expand Down
3 changes: 3 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ variable "pages_hostname" {}

# Environment Variables
variable "HCAPTCHA_SITEKEY" {}
variable "HCAPTCHA_SECRET" {
sensitive = true
}
variable "RSA_PUBLIC_KEY" {}
variable "RSA_PRIVATE_KEY" {
sensitive = true
Expand Down

0 comments on commit 4351675

Please sign in to comment.