Skip to main content

Flask-Tjfu-Captcha 1.0.0

Implement CAPTCHA verification for Flask in a straightforward manner.

Primary Dependencies

  1. captcha >=0.5.0
  2. cryptocode >=0.1
  3. Flask

Installation

pip install flask-tjfu-captcha

Getting Started

from datetime import timedelta

from flask import Flask
from flask_tjfu_captcha import TjfuCaptcha

app = Flask(__name__)
tjfu_captcha = TjfuCaptcha(
    app,
    True  # Must be in Debug mode
)


@app.route('/get_captcha')
def get_captcha():
    return tjfu_captcha.generate_image_captcha(
        secret_key="YOUR_SECRET_KEY",
        length=4,
        expires_in=timedelta(minutes=5),
        only_digits=True
    ).to_dict()


@app.route('/verify_captcha')
@tjfu_captcha.required_captcha(
    "YOUR_SECRET_KEY"
)
def verify_captcha():
    return "OK"


if __name__ == '__main__':
    app.run(debug=True)

/get_captcha will generate a JSON with two attributes:

  • encrypted_code: encoded information of the generated captcha.
  • img_base64: base64 encoded string of the captcha image.
{
  "encrypted_code": "...",
  "img_base64": "..."
}

/verify_captcha uses required_captcha to enforce captcha verification before executing requests within verify_captcha. To pass captcha verification, the client must provide two headers: Tjfu-Captcha-Code containing the captcha code visible in the captcha image requested, and Tjfu-Captcha-Encrypted-Code containing the encoded information mentioned above.

GET http://0.0.0.0:5000/verify_captcha
Tjfu-Captcha-Encrypted-Code: <encrypted_code>
Tjfu-Captcha-Code: <code>

Skipping Captcha Verification in Debug Mode

To bypass captcha verification during testing, you can use the Tjfu-Captcha-Debugging-Ignore header with any value, and TjfuCaptcha must be in Debug mode.

from flask import Flask
from flask_tjfu_captcha import TjfuCaptcha

app = Flask(__name__)
tjfu_captcha = TjfuCaptcha(
    app,
    True  # Must be in Debug mode
)
GET http://0.0.0.0:5000/verify_captcha
Tjfu-Captcha-Debugging-Ignore: <you can put any value>

Customization

You can customize headers for captcha verification using TJFU_CAPTCHA_ENCRYPTED_CODE_HEADER_KEY, TJFU_CAPTCHA_CAPTCHA_CODE_HEADER_KEY, TJFU_CAPTCHA_DEBUGGING_IGNORE_HEADER_KEY, and TJFU_CAPTCHA_FONTS to adjust captcha image fonts.

from flask import Flask

app = Flask(__name__)

app.config['TJFU_CAPTCHA_FONTS'] = "['your_font_path', 'your_other_font_path']"
# Change Tjfu-Captcha-Encrypted-Code header to Request-ID
app.config['TJFU_CAPTCHA_ENCRYPTED_CODE_HEADER_KEY'] = 'Request-ID'
# Change Tjfu-Captcha-Code header to My-Captcha-Code
app.config['TJFU_CAPTCHA_CAPTCHA_CODE_HEADER_KEY'] = 'My-Captcha-Code'
# Change Tjfu-Captcha-Debugging-Ignore header to Ignore-Captcha
app.config['TJFU_CAPTCHA_DEBUGGING_IGNORE_HEADER_KEY'] = 'Ignore-Captcha'

You can also customize responses for missing headers (on_missing_header) and invalid captcha codes (on_invalid_captcha_code).

from flask import Flask
from flask_tjfu_captcha import TjfuCaptcha

app = Flask(__name__)
tjfu_captcha = TjfuCaptcha(
    app,
    True
)


def on_missing_header(header, status_code):
    return {
        "error": f"Missing header: {header}",
        "status_code": status_code
    }


def on_invalid_captcha_code(status_code):
    return {
        "error": "Invalid captcha code",
        "status_code": status_code
    }


tjfu_captcha.on_missing_header(
    on_missing_header
)

tjfu_captcha.on_invalid_captcha_code(
    on_invalid_captcha_code
)

Changelog

Version 1.0.0 - Initial Release - June 22, 2024

  • Initial release of the library with core functionalities:
    • Perform initialization and Captcha verification
    • Provide functions for customization

Each section in this changelog provides a summary of what was added, changed, fixed, or removed in each release of the software. This helps users and developers understand the evolution of the project over time and highlights important updates or improvements made in each version.

Metadata

Release files for flask-tjfu-captcha 1.0.0

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-tjfu-captcha 1.0.0
File Size Uploaded Provenance Actions
flask-tjfu-captcha-1.0.0.tar.gz 4.3 kB Without provenance Details for flask-tjfu-captcha-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-tjfu-captcha 1.0.0
File Interpreter ABI Platform Actions
flask_tjfu_captcha-1.0.0-py3-none-any.whl Python 3 none any Details for flask_tjfu_captcha-1.0.0-py3-none-any.whl

Total release size: 8.9 kB

Release files / flask-tjfu-captcha-1.0.0.tar.gz

Download URL flask-tjfu-captcha-1.0.0.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a588d7fb3d2c928fc22b5cb7c3590bdb96746ff19262224c26375fce5970b2e7
BLAKE2b-256 checksum
How to use checksums
c41d5b2eb69d1f425f6d36c142aefc32003a5a12f231596835b3b1dc28813898
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release files / flask_tjfu_captcha-1.0.0-py3-none-any.whl

Download URL flask_tjfu_captcha-1.0.0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
877bfb6fdee0b9a87768c3d1647efcf87dce6671a034bbeffda3b5749972a05b
BLAKE2b-256 checksum
How to use checksums
84c7f7129993915557621be363dad9a9424703f6f8a46f32e4b1e74bac9b9b5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Security for flask-tjfu-captcha 1.0.0

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.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

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