From 7b22f2d237bc7b858f2981cf549819fbb6cc1778 Mon Sep 17 00:00:00 2001 From: Marek Lesko Date: Fri, 31 Oct 2025 15:57:16 +0000 Subject: [PATCH] feat: integrate reCAPTCHA for contact form validation and submission WIP #8 --- Api/Api.csproj | 1 + Api/Helpers/ReCapthchaAssessment.cs | 62 +++++++ .../contact/contact.component.html | 158 +++++++++--------- .../front-pages/contact/contact.component.ts | 83 ++++++++- Web/src/index.html | 3 +- 5 files changed, 228 insertions(+), 79 deletions(-) create mode 100644 Api/Helpers/ReCapthchaAssessment.cs diff --git a/Api/Api.csproj b/Api/Api.csproj index baa142e..ed7a306 100755 --- a/Api/Api.csproj +++ b/Api/Api.csproj @@ -10,6 +10,7 @@ + diff --git a/Api/Helpers/ReCapthchaAssessment.cs b/Api/Helpers/ReCapthchaAssessment.cs new file mode 100644 index 0000000..ea4c6cd --- /dev/null +++ b/Api/Helpers/ReCapthchaAssessment.cs @@ -0,0 +1,62 @@ +using System; +using Google.Api.Gax.ResourceNames; +using Google.Cloud.RecaptchaEnterprise.V1; + +namespace Api.Helpers +{ + public static class ReCaptchaAssessment + { + // Checks the supplied token with Recaptcha Enterprise and returns true if valid. + // Outputs a short reason when false (invalid reason or exception message). + public static bool CheckToken(string token, out string reason, string projectId = "webserverfarm", string recaptchaKey = "6Lf6df0rAAAAAMXcAx1umneXl1QJo9rTrflpWCvB") + { + reason = string.Empty; + + if (string.IsNullOrWhiteSpace(token)) + { + reason = "empty-token"; + return false; + } + + try + { + var client = RecaptchaEnterpriseServiceClient.Create(); + + var request = new CreateAssessmentRequest + { + Parent = $"projects/{projectId}", + Assessment = new Assessment + { + Event = new Event + { + SiteKey = recaptchaKey, + Token = token + } + } + }; + + var response = client.CreateAssessment(request); + + if (response?.TokenProperties == null) + { + reason = "missing-token-properties"; + return false; + } + + if (!response.TokenProperties.Valid) + { + reason = response.TokenProperties.InvalidReason.ToString(); + return false; + } + + reason = "valid"; + return true; + } + catch (Exception ex) + { + reason = ex.Message; + return false; + } + } + } +} \ No newline at end of file diff --git a/Web/src/app/pages/front-pages/contact/contact.component.html b/Web/src/app/pages/front-pages/contact/contact.component.html index 755167e..c850f64 100755 --- a/Web/src/app/pages/front-pages/contact/contact.component.html +++ b/Web/src/app/pages/front-pages/contact/contact.component.html @@ -11,90 +11,98 @@
-
-
- -
- -
-
-
-
-
-
- - Meno* - - - - - Telefónne číslo* - - - -
-
- - Priezvisko* - - - - - Email* - - - -
+
+ +
+ +
+
+
+
+
+
+ + Meno* + + + + + Telefónne číslo* + + +
+
+ + Priezvisko* + + + + + Email* + + + +
+
-
-
- Dopyt týkajúci sa* - - - - Všeobecný dopyt - Žiadosť o registráciu - - -
+
+
+ Dopyt týkajúci sa* + + + + Všeobecný dopyt + Žiadosť o registráciu + +
-
-
- Správa - - - -
+
+
+
+ Správa + + +
-
-
- - -
-
Kontaktujte nás dnes
-

- Máte otázky alebo potrebujete pomoc? Napíšte nám správu. -

-
- -
-
Naša lokalita
-

- Navštívte nás osobne alebo nájdite naše kontaktné údaje, aby ste sa s nami mohli priamo spojiť. -

-
-
-
+ + +
+
+ +
+
+ + +
+
+ + +
+
Kontaktujte nás dnes
+

+ Máte otázky alebo potrebujete pomoc? Napíšte nám správu. +

+
+ +
+
Naša lokalita
+

+ Navštívte nás osobne alebo nájdite naše kontaktné údaje, aby ste sa s nami mohli priamo spojiť. +

+
+
+
- +