Add form field specifying whether v2 or invisible reCAPTCHA was used
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 22 Aug 2017 13:45:22 +0000 (15:45 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 22 Aug 2017 13:49:58 +0000 (15:49 +0200)
see #2242

com.woltlab.wcf/templates/recaptcha.tpl
wcfsetup/install/files/acp/templates/recaptcha.tpl
wcfsetup/install/files/lib/system/captcha/RecaptchaHandler.class.php
wcfsetup/install/files/lib/system/recaptcha/RecaptchaHandlerV2.class.php

index a961aff4298d61e6580019aa1aeca6e2fb8588e6..1b5e9e8e4618a1090d8ca6c4e9f1fbc420f8c865 100644 (file)
                        <dl class="{if $errorField|isset && $errorField == 'recaptchaString'}formError{/if}">
                                <dt></dt>
                                <dd>
+                                       <input type="hidden" name="recaptcha-type" value="invisible">
                                        <div id="recaptchaBucket{$recaptchaBucketID}"></div>
                                        <noscript>
                                                <div style="width: 302px; height: 473px;">
                                                                                grecaptcha.execute(WCF.recaptcha.mapping['recaptchaBucket{$recaptchaBucketID}']);
                                                                                return promise.then(function (token) {
                                                                                        return {
-                                                                                               'g-recaptcha-response': token
+                                                                                               'g-recaptcha-response': token,
+                                                                                               'recaptcha-type': 'invisible'
                                                                                        };
                                                                                });
                                                                        });
                        <dl class="{if $errorField|isset && $errorField == 'recaptchaString'}formError{/if}">
                                <dt></dt>
                                <dd>
+                               <input type="hidden" name="recaptcha-type" value="v2">
                                        <div id="recaptchaBucket{$recaptchaBucketID}"></div>
                                        <noscript>
                                                <div style="width: 302px; height: 473px;">
                        {if $ajaxCaptcha|isset && $ajaxCaptcha}
                        WCF.System.Captcha.addCallback('{$captchaID}', function() {
                                return {
-                                       'g-recaptcha-response': grecaptcha.getResponse(WCF.recaptcha.mapping['recaptchaBucket{$recaptchaBucketID}'])
+                                       'g-recaptcha-response': grecaptcha.getResponse(WCF.recaptcha.mapping['recaptchaBucket{$recaptchaBucketID}']),
+                                       'type': 'v2'
                                };
                        });
                        {/if}
index a961aff4298d61e6580019aa1aeca6e2fb8588e6..1b5e9e8e4618a1090d8ca6c4e9f1fbc420f8c865 100644 (file)
                        <dl class="{if $errorField|isset && $errorField == 'recaptchaString'}formError{/if}">
                                <dt></dt>
                                <dd>
+                                       <input type="hidden" name="recaptcha-type" value="invisible">
                                        <div id="recaptchaBucket{$recaptchaBucketID}"></div>
                                        <noscript>
                                                <div style="width: 302px; height: 473px;">
                                                                                grecaptcha.execute(WCF.recaptcha.mapping['recaptchaBucket{$recaptchaBucketID}']);
                                                                                return promise.then(function (token) {
                                                                                        return {
-                                                                                               'g-recaptcha-response': token
+                                                                                               'g-recaptcha-response': token,
+                                                                                               'recaptcha-type': 'invisible'
                                                                                        };
                                                                                });
                                                                        });
                        <dl class="{if $errorField|isset && $errorField == 'recaptchaString'}formError{/if}">
                                <dt></dt>
                                <dd>
+                               <input type="hidden" name="recaptcha-type" value="v2">
                                        <div id="recaptchaBucket{$recaptchaBucketID}"></div>
                                        <noscript>
                                                <div style="width: 302px; height: 473px;">
                        {if $ajaxCaptcha|isset && $ajaxCaptcha}
                        WCF.System.Captcha.addCallback('{$captchaID}', function() {
                                return {
-                                       'g-recaptcha-response': grecaptcha.getResponse(WCF.recaptcha.mapping['recaptchaBucket{$recaptchaBucketID}'])
+                                       'g-recaptcha-response': grecaptcha.getResponse(WCF.recaptcha.mapping['recaptchaBucket{$recaptchaBucketID}']),
+                                       'type': 'v2'
                                };
                        });
                        {/if}
index 6d20889831cb8b61b3fe14a85ea99b7dfea6347e..6028faa39feb110e046aa65ac4f462dfa2732a93 100644 (file)
@@ -63,6 +63,7 @@ class RecaptchaHandler implements ICaptchaHandler {
                }
                else {
                        // V2
+                       if (isset($_POST['recaptcha-type'])) $this->challenge = $_POST['recaptcha-type'];
                        if (isset($_POST['g-recaptcha-response'])) $this->response = $_POST['g-recaptcha-response'];
                }
        }
@@ -86,7 +87,7 @@ class RecaptchaHandler implements ICaptchaHandler {
                }
                else {
                        // V2
-                       RecaptchaHandlerV2::getInstance()->validate($this->response);
+                       RecaptchaHandlerV2::getInstance()->validate($this->response, $this->challenge ?: 'v2');
                }
        }
 }
index 4fef91ad5879d1522a5cac162b1a4445a13be429..642ec5740b02b641f5555bfbce61d200d98a31c2 100644 (file)
@@ -22,13 +22,23 @@ class RecaptchaHandlerV2 extends SingletonFactory {
         * @param       string          $response
         * @throws      UserInputException
         */
-       public function validate($response) {
+       public function validate($response, $type = 'v2') {
                // fail if response is empty to avoid sending api requests
                if (empty($response)) {
                        throw new UserInputException('recaptchaString', 'false');
                }
                
-               $request = new HTTPRequest('https://www.google.com/recaptcha/api/siteverify?secret='.rawurlencode(RECAPTCHA_PRIVATEKEY).'&response='.rawurlencode($response).'&remoteip='.rawurlencode(UserUtil::getIpAddress()), ['timeout' => 10]);
+               if ($type === 'v2') {
+                       $key = RECAPTCHA_PRIVATEKEY;
+               }
+               else if ($type === 'invisible') {
+                       $key = RECAPTCHA_PRIVATEKEY;
+               }
+               else {
+                       throw new \InvalidArgumentException('$type must be either v2 or invisible.');
+               }
+               
+               $request = new HTTPRequest('https://www.google.com/recaptcha/api/siteverify?secret='.rawurlencode($key).'&response='.rawurlencode($response).'&remoteip='.rawurlencode(UserUtil::getIpAddress()), ['timeout' => 10]);
                
                try {
                        $request->execute();