【问题标题】:Android reCAPTCHA: Verifying the user's response: all the functions must be called client-side! Can I use my backend however?Android reCAPTCHA:验证用户响应:所有函数都必须调用客户端!但是我可以使用我的后端吗?
【发布时间】:2019-11-20 10:13:37
【问题描述】:

我想验证我的 Android 用户的 reCAPTCHA。所以我正在阅读这个文档:https://developers.google.com/recaptcha/docs/verify:

对于Android库用户,如果状态返回成功,您可以调用SafetyNetApi.RecaptchaTokenResult.getTokenResult()方法获取响应令牌。

在这个函数的手册中,关于getTokenResult(https://developers.google.com/android/reference/com/google/android/gms/safetynet/SafetyNetApi.RecaptchaTokenResult.html#getTokenResult())的描述如下:

获取 reCAPTCHA 用户响应令牌,该令牌必须通过调用验证用户响应中描述的 siteverify 方法进行验证。

siteverify函数的手册描述如下(https://developers.google.com/android/reference/com/google/android/gms/safetynet/SafetyNetClient.html#verifyWithRecaptcha(java.lang.String)):

使用 reCAPTCHA 提供用户证明。

如果 reCAPTCHA 确信这是真实设备上的真实用户,它将返回一个没有质询的令牌。否则,它将在返回令牌之前提供视觉/音频挑战来证明用户的人性。

我的问题

我想使用我的后端服务器 (Cloud Functions) 来验证 reCAPTCHA。但是,根据 Android 文档,上述所有功能似乎都放在客户端。事实上,siteverify 应该使用getTokenResult 获得的令牌来调用,并且两者似乎都是 Android SecureNET ReCAPTCHA Android API 的一部分...

不过,我认为使用 Cloud Functions 会更安全!但是我可以使用我的后端吗?

编辑:在 Cloud Functions 中对 siteverify 的后端调用

exports.verifyRecaptcha = functions.https.onRequest((request, response) => {

    const user_response_token = request.query.user_response_token;
    if(user_response_token == '') {
        throw new functions.https.HttpsError('invalid-argument', 'The function must be called with an adequat user response token.');
    }
    
    const remote_url = 'https://www.google.com/recaptcha/api/siteverify';
    const secret = null;
    request.post({url: remote_url, form:{secret: secret, response: user_response_token}}, function(error, response, body) {
        if(error) {
            throw new functions.https.HttpsError('unknown', error);
        }

        if(!response.statusCode != 200) {
            throw new functions.https.HttpsError('unknown', 'Something went wrong. Status code: ' + response.statusCode + '.');
        }

        if(!body.success) {
            throw new functions.https.HttpsError('unknown', 'Unable to verify this captcha.');
        }

        return response;    
    });

});

【问题讨论】:

    标签: android google-cloud-functions recaptcha


    【解决方案1】:

    您可以获取从getTokenResult() 返回的令牌,将其发送到您的后端,然后让您的后端调用siteverify 的Web API 版本:

    https://www.google.com/recaptcha/api/siteverify
    

    【讨论】:

    • 请告诉我我对siteverify 的后端调用是否正确?:) (代码插入我的问题)
    • 我不知道。看起来不错。它有效吗?我改用 URL 参数。而且我不知道return response; 应该去哪里。
    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 2019-01-28
    • 2014-01-29
    相关资源
    最近更新 更多