The only tool you need to know what is happening and how to fix it.
Go alertingexception-trackerissue-trackinglogsmetricsmonitoringobservability-platformopen-telemetryotelsession-replaytelemetrytraces
feat: optional turnstile for registration
dduxd523f34a5228
diff --git a/backend/app/controllers/auth.controller.go b/backend/app/controllers/auth.controller.goindex a49cbcc8..c532ee15 100644--- a/backend/app/controllers/auth.controller.go+++ b/backend/app/controllers/auth.controller.go@@ -86,6 +86,13 @@ func (a authController) Register(c *gin.Context) {return}+ if services.TurnstileService.IsEnabled() {+ if err := services.TurnstileService.Verify(request.CaptchaToken, c.ClientIP()); err != nil {+ c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})+ return+ }+ }+tx := middleware.GetTx(c)// if we're not in the cloud mode only a single organization is alloweddiff --git a/backend/app/models/auth.model.go b/backend/app/models/auth.model.goindex ec54ed1b..329ddcb1 100644--- a/backend/app/models/auth.model.go+++ b/backend/app/models/auth.model.go@@ -27,6 +27,7 @@ type RegisterRequest struct {Timezone string `json:"timezone" binding:"required"`ProjectName string `json:"projectName" binding:"required"`Framework string `json:"framework" binding:"required"`+ CaptchaToken string `json:"captchaToken"`}type RegisterResponse struct {diff --git a/backend/app/services/turnstile.service.go b/backend/app/services/turnstile.service.gonew file mode 100644index 00000000..c0780ded--- /dev/null+++ b/backend/app/services/turnstile.service.go@@ -0,0 +1,61 @@+package services++import (+ "encoding/json"+ "errors"+ "net/http"+ "net/url"+ "os"+ "time"+)++type turnstileService struct {+ secretKey string+ client *http.Client+}++type turnstileResponse struct {+ Success bool `json:"success"`+ ErrorCodes []string `json:"error-codes"`+ ChallengeTs string `json:"challenge_ts"`+ Hostname string `json:"hostname"`+}++var TurnstileService = &turnstileService{+ secretKey: os.Getenv("TURNSTILE_SECRET_KEY"),+ client: &http.Client{Timeout: 10 * time.Second},+}++func (s *turnstileService) IsEnabled() bool {+ return s.secretKey != ""+}++func (s *turnstileService) Verify(token string, remoteIP string) error {+ if token == "" {+ return errors.New("captcha verification required")+ }++ data := url.Values{}+ data.Set("secret", s.secretKey)+ data.Set("response", token)+ if remoteIP != "" {+ data.Set("remoteip", remoteIP)+ }++ resp, err := s.client.PostForm("https://challenges.cloudflare.com/turnstile/v0/siteverify", data)+ if err != nil {+ return errors.New("captcha verification failed")+ }+ defer resp.Body.Close()++ var result turnstileResponse+ if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {+ return errors.New("captcha verification failed")+ }++ if !result.Success {+ return errors.New("captcha verification failed")+ }++ return nil+}diff --git a/frontend/package-lock.json b/frontend/package-lock.jsonindex 9cd81a4b..f990a8a6 100644--- a/frontend/package-lock.json+++ b/frontend/package-lock.json@@ -1,12 +1,12 @@{"name": "frontend",- "version": "1.0.1",+ "version": "1.0.2","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "frontend",- "version": "1.0.1",+ "version": "1.0.2","dependencies": {"@sveltejs/svelte-json-tree": "^2.2.1","lucide-svelte": "^0.562.0",diff --git a/frontend/package.json b/frontend/package.jsonindex 8613a624..1575d1d2 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -1,7 +1,7 @@{"name": "frontend","private": true,- "version": "1.0.1",+ "version": "1.0.2","type": "module","scripts": {"dev": "vite dev",diff --git a/frontend/src/app.d.ts b/frontend/src/app.d.tsindex eb516f13..4881416e 100644--- a/frontend/src/app.d.ts+++ b/frontend/src/app.d.ts@@ -11,6 +11,7 @@ declare global {const __APP_VERSION__: string;const __CLOUD_MODE__: string;+ const __TURNSTILE_SITE_KEY__: string;}export {};diff --git a/frontend/src/lib/components/turnstile-widget.svelte b/frontend/src/lib/components/turnstile-widget.sveltenew file mode 100644index 00000000..0c94ecb2--- /dev/null+++ b/frontend/src/lib/components/turnstile-widget.svelte@@ -0,0 +1,80 @@+<script lang="ts">+ import { onMount, onDestroy } from 'svelte';+ import { themeState } from '$lib/state/theme.svelte';++ interface Props {+ siteKey: string;+ onVerify: (token: string) => void;+ onError?: () => void;+ }++ let { siteKey, onVerify, onError }: Props = $props();++ let container: HTMLDivElement;+ let widgetId: string | null = null;++ declare global {+ interface Window {+ turnstile: {+ render: (element: HTMLElement, options: {+ sitekey: string;+ callback: (token: string) => void;+ 'error-callback'?: () => void;+ 'expired-callback'?: () => void;+ theme?: 'light' | 'dark' | 'auto';+ }) => string;+ remove: (widgetId: string) => void;+ };+ onTurnstileLoad?: () => void;+ }+ }++ function loadScript(): Promise<void> {+ return new Promise((resolve) => {+ if (window.turnstile) {+ resolve();+ return;+ }++ const existingScript = document.querySelector('script[src*="turnstile"]');+ if (existingScript) {+ window.onTurnstileLoad = resolve;+ return;+ }++ window.onTurnstileLoad = resolve;+ const script = document.createElement('script');+ script.src = 'https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onTurnstileLoad';+ script.async = true;+ script.defer = true;+ document.head.appendChild(script);+ });+ }++ function renderWidget() {+ if (!window.turnstile || !container) return;++ widgetId = window.turnstile.render(container, {+ sitekey: siteKey,+ callback: onVerify,+ 'error-callback': onError,+ 'expired-callback': () => {+ onVerify('');+ },+ theme: themeState.isDark ? 'dark' : 'light'+ });+ }++ onMount(async () => {+ await loadScript();+ renderWidget();+ });++ onDestroy(() => {+ if (widgetId && window.turnstile) {+ window.turnstile.remove(widgetId);+ }+ });+</script>++<div bind:this={container} class="flex justify-center"></div>diff --git a/frontend/src/routes/register/+page.svelte b/frontend/src/routes/register/+page.svelteindex 600a7f27..bf2eb98f 100644--- a/frontend/src/routes/register/+page.svelte+++ b/frontend/src/routes/register/+page.svelte@@ -12,6 +12,7 @@import { themeState } from '$lib/state/theme.svelte';import { toast } from 'svelte-sonner';import FrameworkIcon from '$lib/components/framework-icon.svelte';+ import TurnstileWidget from '$lib/components/turnstile-widget.svelte';let email = $state('');let name = $state('');@@ -23,6 +24,10 @@let framework = $state<Framework>('gin');let error = $state('');let loading = $state(false);+ let captchaToken = $state('');++ const turnstileSiteKey = __TURNSTILE_SITE_KEY__;+ const captchaEnabled = turnstileSiteKey !== '';const timezones = Intl.supportedValuesOf('timeZone');@@ -74,6 +79,11 @@return;}+ if (captchaEnabled && !captchaToken) {+ error = 'Please complete the captcha';+ return;+ }+loading = true;error = '';@@ -90,7 +100,8 @@organizationName,timezone,projectName,- framework+ framework,+ captchaToken})});@@ -222,7 +233,17 @@</Select.Root></div>- <Button type="submit" disabled={loading} class="w-full mt-2">+ {#if captchaEnabled}+ <div class="flex flex-col space-y-1.5 mt-2">+ <TurnstileWidget+ siteKey={turnstileSiteKey}+ onVerify={(token) => captchaToken = token}+ onError={() => captchaToken = ''}+ />+ </div>+ {/if}++ <Button type="submit" disabled={loading || (captchaEnabled && !captchaToken)} class="w-full mt-2">{#if loading}Creating account...{:else}diff --git a/frontend/vite.config.ts b/frontend/vite.config.tsindex efcf9dcb..fd55ded9 100644--- a/frontend/vite.config.ts+++ b/frontend/vite.config.ts@@ -35,7 +35,8 @@ export default defineConfig(({ mode }) => {define: {__APP_VERSION__: JSON.stringify(env.PUBLIC_APP_VERSION || pkg.version),__CLOUD_MODE__: env.CLOUD_MODE,- __BILLING_AVAILABLE__: billingExists+ __BILLING_AVAILABLE__: billingExists,+ __TURNSTILE_SITE_KEY__: JSON.stringify(env.PUBLIC_TURNSTILE_SITE_KEY || '')},resolve: {dedupe: ['d3-scale', 'd3-array', 'lucide-svelte', 'svelte']