Skip to main content

Flask-xCaptcha

Build Status

The new xCaptcha implementation for Flask without Flask-WTF.

Can also be used as standalone

Compatible with:

  • Google ReCaptcha (default)
  • hCaptcha
  • Any other similarly configured captcha

This project was forked from Mardix's Flask-ReCaptcha project


Installation

pip install flask-xcaptcha

Usage - Minimal Implementation

Google ReCaptcha - Using app variable

from flask import Flask
from flask_xcaptcha import XCaptcha

app = Flask(__name__)
app.config.update(
    XCAPTCHA_SITE_KEY=#<your_site_key>,
    XCAPTCHA_SECRET_KEY=#<your_secret_key>
)
xcaptcha = XCaptcha(app=app)

Google ReCaptcha - Without app variable

from flask_xcaptcha import XCaptcha

xcaptcha = XCaptcha(
    site_key=#<your_site_key>,
    secret_key=#<your_secret_key>
)

hCaptcha - Using app variable

from flask import Flask
from flask_xcaptcha import XCaptcha

app = Flask(__name__)
app.config.update(
    XCAPTCHA_SITE_KEY=#<your_site_key>,
    XCAPTCHA_SECRET_KEY=#<your_secret_key>,
    XCAPTCHA_VERIFY_URL=https://hcaptcha.com/siteverify,
    XCAPTCHA_API_URL=https://hcaptcha.com/1/api.js,
    XCAPTCHA_DIV_CLASS=h-captcha
)
xcaptcha = XCaptcha(app=app)

hCaptcha - Without app variable

from flask_xcaptcha import XCaptcha

xcaptcha = XCaptcha(
    site_key=#<your_site_key>,
    secret_key=#<your_secret_key>,
    verify_url=https://hcaptcha.com/siteverify,
    api_url=https://hcaptcha.com/1/api.js,
    div_class=h-captcha
)

App Config Variables

Flask-xCaptcha is configured through the standard Flask config API. Add these to your app config as shown above to further configure your xCaptcha

Variable Description Allowed Values Default Required?
XCAPTCHA_SITE_KEY Site key provided by xCaptcha service Your site key Required
XCAPTCHA_SECRET_KEY Secret key provided by xCaptcha service Your secret key Required
XCAPTCHA_ENABLED Enable verification. If false, verification will be disabled True / False True Optional
XCAPTCHA_THEME Theme for the xCaptcha element light / dark (service dependent) "light" Optional
XCAPTCHA_TYPE Type of xCaptcha service dependent "image" Optional
XCAPTCHA_SIZE Size of xCaptcha normal / compact (service dependent) "normal" Optional
XCAPTCHA_TABINDEX Set the tabindex of the widget and popup integer 0 Optional
XCAPTCHA_VERIFY_URL The URL to verify the filled in xCaptcha at URL "https://www.google.com/recaptcha/api/siteverify" Optional
XCAPTCHA_API_URL The URL of the xCaptcha API JS script URL "//www.google.com/recaptcha/api.js" Optional
XCAPTCHA_DIV_CLASS The class of the div element surrounding the xCaptcha string "g-recaptcha" Optional
XCAPTCHA_CALLBACK The string that recaptcha finds on a button to see when a button is submitted "OnSubmitCallback" Optional

In your template: {{ xcaptcha }}

Inside of the form you want to protect, include the tag: {{ xcaptcha }}

It will insert the code automatically

<form method="post" action="/submit">
    ... your field
    ... your field

    {{ xcaptcha }}

    [submit button]
</form>

Verify the captcha

In the view that's going to validate the captcha

from flask import Flask
from flask_xcaptcha import XCaptcha

app = Flask(__name__)
app.config.update(
    XCAPTCHA_SITE_KEY=#<your_site_key>,
    XCAPTCHA_SECRET_KEY=#<your_secret_key>
)
xcaptcha = XCaptcha(app=app)

@route("/submit", methods=["POST"])
def submit():

    if xcaptcha.verify():
        # SUCCESS
        pass
    else:
        # FAILED
        pass

API

XCaptcha.init(app=None, site_key=None, secret_key=None, is_enabled=True, theme="light", xtype="image", size="normal", tabindex=0, verify_url="https://www.google.com/recaptcha/api/siteverify", api_url="//www.google.com/recaptcha/api.js", div_class="g-recaptcha",**kwargs)

Initialises the XCaptcha using values set in the app config (if an app is supplied), and otherwise using directly passed arguments

XCaptcha.get_code()

Returns the HTML code to replace {{ xcaptcha }} with.

XCaptcha.verify()

Returns a bool indicating whether or not the xCaptcha was successfully completed

{{ xcaptcha }}

This will insert an HTML div element containing the captcha into a Jinja2 template

(c) 2023 Max Levine

Metadata

Release files for Flask-xCaptcha 0.5.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-xCaptcha 0.5.5
File Size Uploaded Provenance Actions
Flask-xCaptcha-0.5.5.tar.gz 5.4 kB Without provenance Details for Flask-xCaptcha-0.5.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for Flask-xCaptcha 0.5.5
File Interpreter ABI Platform Actions
Flask_xCaptcha-0.5.5-py3-none-any.whl Python 3 none any Details for Flask_xCaptcha-0.5.5-py3-none-any.whl

Total release size: 10.6 kB

Release files / Flask-xCaptcha-0.5.5.tar.gz

Download URL Flask-xCaptcha-0.5.5.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8d1c0bfcf70ee10630e11636875dd51bd5e4e20fed339068cf19a101d5a9180e
BLAKE2b-256 checksum
How to use checksums
47c2b76f37eec67f7d59933307279f8dccf6cae913897ca63222094556fa9fbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / Flask_xCaptcha-0.5.5-py3-none-any.whl

Download URL Flask_xCaptcha-0.5.5-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ff8fc70a26048e6668bd7a450dcf31ca4f49cacc7d5556d47dc7e3557016e8cf
BLAKE2b-256 checksum
How to use checksums
d1a1e5ca66fb727925ac0ecb9b36dd7e047e7049b991a91abdfd08f8e8cf0207
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Security for Flask-xCaptcha 0.5.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page