Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quart compatibility #7

Closed
HexyeDEV opened this issue Nov 8, 2022 · 22 comments
Closed

Quart compatibility #7

HexyeDEV opened this issue Nov 8, 2022 · 22 comments
Assignees
Labels
enhancement New feature or request

Comments

@HexyeDEV
Copy link

HexyeDEV commented Nov 8, 2022

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 added a commit that referenced this issue Nov 9, 2022
@KnugiHK KnugiHK self-assigned this Nov 9, 2022
@KnugiHK KnugiHK added the enhancement New feature or request label Nov 9, 2022
@KnugiHK
Copy link
Owner

KnugiHK commented Nov 9, 2022

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
Copy link
Owner

KnugiHK commented Nov 9, 2022

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

@HexyeDEV
Copy link
Author

HexyeDEV commented Nov 9, 2022

Still getting the same error

@KnugiHK
Copy link
Owner

KnugiHK commented Nov 9, 2022

Are you using the dev branch?

@HexyeDEV
Copy link
Author

HexyeDEV commented Nov 9, 2022

Yes

@KnugiHK
Copy link
Owner

KnugiHK commented Nov 9, 2022

Is the config HCAPTCHA_ASYNC entry set to True?

@HexyeDEV
Copy link
Author

HexyeDEV commented Nov 9, 2022

Yes

@KnugiHK
Copy link
Owner

KnugiHK commented Nov 9, 2022

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

  • flask_request (if presented)
  • quart_request (if presented)
  • request

@HexyeDEV
Copy link
Author

HexyeDEV commented Nov 9, 2022

I'm sorry wdym?
Where should i get them

@KnugiHK
Copy link
Owner

KnugiHK commented Nov 9, 2022

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
Copy link
Owner

KnugiHK commented Nov 9, 2022

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

@HexyeDEV
Copy link
Author

HexyeDEV commented Nov 9, 2022

I use hCaptcha(app)

@HexyeDEV
Copy link
Author

HexyeDEV commented Nov 9, 2022

These are the ids btw: 140065711599920 140065728038752 140065728038752

@KnugiHK
Copy link
Owner

KnugiHK commented Nov 9, 2022

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
Copy link
Owner

KnugiHK commented Nov 9, 2022

These are the ids btw: 140065711599920 140065728038752 140065728038752

Looks fine. Does it work now?

@HexyeDEV
Copy link
Author

HexyeDEV commented Nov 9, 2022

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
Copy link
Owner

KnugiHK commented Nov 10, 2022

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

@HexyeDEV
Copy link
Author

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
Copy link
Owner

KnugiHK commented Jan 15, 2023

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
Copy link
Author

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

@KnugiHK
Copy link
Owner

KnugiHK commented Jan 16, 2023

I will leave this open until next release.

@KnugiHK
Copy link
Owner

KnugiHK commented Nov 19, 2024

Released in 0.7.0.

@KnugiHK KnugiHK closed this as completed Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants