KnugiHK / flask-hcaptcha

A hCaptcha extension for Flask based on flask-recaptcha
MIT License
21 stars 7 forks source link

Quart compatibility #7

Open HexyeDEV opened 1 year ago

HexyeDEV commented 1 year ago

Is there any way to make this work in quart? I followed the readme and I'm getting this error when i try to check hcaptcha.verify(): "RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed an active HTTP request. Consult the documentation on testing for information about how to avoid this problem."

KnugiHK commented 1 year ago

I have added the compatibility with Quart into the dev branch. It is not yet well tested, but it works in my environment. You can give it a try.

KnugiHK commented 1 year ago

To use the plugin in Quart, remember to set the config HCAPTCHA_ASYNC to True.

HexyeDEV commented 1 year ago

Still getting the same error

KnugiHK commented 1 year ago

Are you using the dev branch?

HexyeDEV commented 1 year ago

Yes

KnugiHK commented 1 year ago

Is the config HCAPTCHA_ASYNC entry set to True?

HexyeDEV commented 1 year ago

Yes

KnugiHK commented 1 year ago

Could you verify and paste the id() of the following variables in the plugin here?

HexyeDEV commented 1 year ago

I'm sorry wdym? Where should i get them

KnugiHK commented 1 year ago

I'm sorry wdym? Where should i get them

Add print(id(flask_request), id(quart_request), id(request)) to line 100 of the flask_hcaptcha.py. When you start your app, there should be two to three int being shown in your console.

        else:
            self.verify = self.verify_sync
            try:
                request = flask_request
            except NameError:
                print("flask_hcaptcha: Missing dependencies")
                exit()
        print(id(flask_request), id(quart_request), id(request))
        @app.context_processor
        def get_code():
            return dict(hcaptcha=Markup(self.get_code()))
KnugiHK commented 1 year ago

Also, how you initialize the plugin? Through hCaptcha(app) or hcaptcha.init_app(app)?

HexyeDEV commented 1 year ago

I use hCaptcha(app)

HexyeDEV commented 1 year ago

These are the ids btw: 140065711599920 140065728038752 140065728038752

KnugiHK commented 1 year ago

I use hCaptcha(app)

Try to use init_app instead. I noticed there could be a bug for hCaptcha(app) during development of quart compatibility.

KnugiHK commented 1 year ago

These are the ids btw: 140065711599920 140065728038752 140065728038752

Looks fine. Does it work now?

HexyeDEV commented 1 year ago

Oh ok it was my fault for the previous error, I somehow managed to clone the main branch even after double checking. Anyway now even if the captcha is correct await hcaptcha.verify() returns False

KnugiHK commented 1 year ago

Could you provide detailed step for me to reproduce the problem?

HexyeDEV commented 1 year ago

Just a post request and in the endpoint I tried using:

if await hcaptcha.verify():
    print(True)
else:
    print(False)

and It Is printing false

KnugiHK commented 1 year ago

Here is an example, see if /submit return a correct value in your environment.

from quart import Quart, render_template_string
from flask_hcaptcha import hCaptcha
app = Quart(__name__)
app.config["HCAPTCHA_ASYNC"] = True
app.config["HCAPTCHA_ENABLED"] = True
app.config["HCAPTCHA_SITE_KEY"] = "<site key>"
app.config["HCAPTCHA_SECRET_KEY"] = "<secret key>"
hcaptcha = hCaptcha(app)

@app.route("/")
async def index():
    return await render_template_string(
        """
        <form method="post" action="/submit">
        {{ hcaptcha }}
        <input type="submit">
        </form>
        """
    )

@app.route("/submit", methods=["POST"])
async def hello():
    if await hcaptcha.verify():
        return "True"
    else:
        return "False"

if __name__ == "__main__":
    app.run()
HexyeDEV commented 1 year ago

I made my own hcaptcha handler. anyway I tried this and looks like it's working

KnugiHK commented 1 year ago

I will leave this open until next release.