Browse Source

Add quick verify button for debug

ghorsington 3 years ago
parent
commit
c196b6fb9c
4 changed files with 51 additions and 34 deletions
  1. 1 0
      .env.template
  2. 11 2
      web/src/routes/rules/index.svelte
  3. 38 32
      web/src/routes/rules/verify.ts
  4. 1 0
      web/src/utils/environment.ts

+ 1 - 0
.env.template

@@ -18,6 +18,7 @@ NOCTBOT_ADDR=
 RPC_PORT=
 WEB_COOKIE_KEY=
 WEB_AUTH_URI=
+HCAPTCHA_ENABLED=
 HCAPTCHA_SITEKEY=
 HCAPTCHA_SECRET=
 

+ 11 - 2
web/src/routes/rules/index.svelte

@@ -18,6 +18,7 @@
       rulesText: md.ok ? md.text : "",
       sitekey: verify.captchaSitekey,
       verified: verify.userVerified,
+      needsCaptcha: verify.needsCaptcha
     };
   }
 </script>
@@ -32,6 +33,7 @@
   export let rulesText: string = "";
   export let sitekey: string = "";
   export let verified!: boolean;
+  export let needsCaptcha!: boolean;
   let showVerify: boolean = false;
   const spinner = (faSpinner as unknown) as undefined;
   let htmlContent = new Converter().makeHtml(rulesText);
@@ -87,6 +89,10 @@
   .error {
     @apply text-red-600;
   }
+
+  .verify-button {
+    @apply text-gray-200 font-bold;
+  }
 </style>
 
 <svelte:head>
@@ -105,8 +111,11 @@
           them</label>
       </form>
       {#if showVerify}
-        <!-- <a href="asd" on:click|preventDefault={() => onVerified({ key: 'wew' })}>Verify test</a> -->
-        <h-captcha site-key={sitekey} dark on:verified={onVerified} on:expired={onExpired} on:error={onError} />  
+        {#if needsCaptcha}
+          <h-captcha site-key={sitekey} dark on:verified={onVerified} on:expired={onExpired} on:error={onError} />  
+        {:else}
+          <button class="verify-button" on:click={() => void onVerified({ key: "" })}>Verify</button>
+        {/if}
       {/if}
       <div class="py-2 pointer-events-none select-none">
         {#if state == State.Verify}

+ 38 - 32
web/src/routes/rules/verify.ts

@@ -8,6 +8,7 @@ import { ENV } from "src/utils/environment";
 export interface VerifyInfo {
     captchaSitekey: string;
     userVerified: boolean;
+    needsCaptcha: boolean;
 }
 
 export interface VerifyRequest {
@@ -40,20 +41,13 @@ export const get = async (req: ExpressRequest, res: ExpressResponse): GetResult
     return res.json({
         captchaSitekey: ENV.HCAPTCHA_SITEKEY,
         userVerified: verified,
+        needsCaptcha: ENV.HCAPTCHA_ENABLED === "TRUE",
     });
 };
 
 type PostResult = Promise<ExpressResponse<Option<unknown, { error: string }>>>;
 export const post = async (req: ExpressRequest, res: ExpressResponse): PostResult => {
-    const hasToken = (body: unknown):
-        body is VerifyRequest => body instanceof Object
-                                    && (body as VerifyRequest).captchaResponse !== undefined;
-    if (!hasToken(req.body)) {
-        return res.json({
-            ok: false,
-            error: "No user token provided, please try again",
-        });
-    }
+    const needVerify = ENV.HCAPTCHA_ENABLED === "TRUE";
 
     if (!req.session?.userId) {
         return res.json({
@@ -62,32 +56,44 @@ export const post = async (req: ExpressRequest, res: ExpressResponse): PostResul
         });
     }
 
-    const response = await tryDo(got<HCaptchaResponse>(VERIFY_URL, {
-        method: "post",
-        responseType: "json",
-        form: {
-            secret: ENV.HCAPTCHA_SECRET,
-            response: req.body.captchaResponse,
-        },
-    }));
+    if (needVerify) {
+        const hasToken = (body: unknown):
+        body is VerifyRequest => body instanceof Object
+                                    && (body as VerifyRequest).captchaResponse !== undefined;
+        if (needVerify && !hasToken(req.body)) {
+            return res.json({
+                ok: false,
+                error: "No user token provided, please try again",
+            });
+        }
+
+        const response = await tryDo(got<HCaptchaResponse>(VERIFY_URL, {
+            method: "post",
+            responseType: "json",
+            form: {
+                secret: ENV.HCAPTCHA_SECRET,
+                response: req.body.captchaResponse,
+            },
+        }));
 
-    if (!response.ok) {
-        logger.error("Failed to hCaptcha user %s: %s", req.session.userId, response.error);
-        return res.json({
-            ok: false,
-            error: "Failed to verify hCaptcha response. Please try again.",
-        });
-    }
+        if (!response.ok) {
+            logger.error("Failed to hCaptcha user %s: %s", req.session.userId, response.error);
+            return res.json({
+                ok: false,
+                error: "Failed to verify hCaptcha response. Please try again.",
+            });
+        }
 
-    const captchaResponse = response.result.body;
+        const captchaResponse = response.result.body;
 
-    if (!captchaResponse.success) {
-        const errors = captchaResponse["error-codes"] ?? [];
-        logger.error("Failed hCaptcha verify on user %s. Got errors: %s", req.session.userId, errors.join(";"));
-        return res.json({
-            ok: false,
-            error: "Failed to verify hCaptcha response. Please try again.",
-        });
+        if (!captchaResponse.success) {
+            const errors = captchaResponse["error-codes"] ?? [];
+            logger.error("Failed hCaptcha verify on user %s. Got errors: %s", req.session.userId, errors.join(";"));
+            return res.json({
+                ok: false,
+                error: "Failed to verify hCaptcha response. Please try again.",
+            });
+        }
     }
 
     const verifyResponse = await rpcClient.verifyUser({ userId: req.session.userId });

+ 1 - 0
web/src/utils/environment.ts

@@ -25,6 +25,7 @@ export const ENV = {
     WEB_DATA_PATH: "",
     HCAPTCHA_SITEKEY: "",
     HCAPTCHA_SECRET: "",
+    HCAPTCHA_ENABLED: "",
 };
 
 function isValid(): boolean {