flask-tjfu-captcha


Nameflask-tjfu-captcha JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/duynguyen02/flask-tjfu-captcha
SummaryImplement CAPTCHA verification for Flask in a straightforward manner.
upload_time2024-06-22 12:29:53
maintainerNone
docs_urlNone
authorduynguyen02
requires_pythonNone
licenseNone
keywords python flask
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Flask-Tjfu-Captcha 1.0.0

Implement CAPTCHA verification for Flask in a straightforward manner.

### Primary Dependencies

1. [captcha >=0.5.0](https://pypi.org/project/captcha/)
2. [cryptocode >=0.1](https://pypi.org/project/cryptocode/)
3. [Flask](https://pypi.org/project/Flask/)

### Installation

```
pip install flask-tjfu-captcha
```

### Getting Started

```python
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.

```json
{
  "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.

```http request
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.

```python
from flask import Flask
from flask_tjfu_captcha import TjfuCaptcha

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

```http request
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.

```python
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`).

```python
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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/duynguyen02/flask-tjfu-captcha",
    "name": "flask-tjfu-captcha",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "Python, Flask",
    "author": "duynguyen02",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/c4/1d/5b2eb69d1f425f6d36c142aefc32003a5a12f231596835b3b1dc28813898/flask-tjfu-captcha-1.0.0.tar.gz",
    "platform": null,
    "description": "# Flask-Tjfu-Captcha 1.0.0\n\nImplement CAPTCHA verification for Flask in a straightforward manner.\n\n### Primary Dependencies\n\n1. [captcha >=0.5.0](https://pypi.org/project/captcha/)\n2. [cryptocode >=0.1](https://pypi.org/project/cryptocode/)\n3. [Flask](https://pypi.org/project/Flask/)\n\n### Installation\n\n```\npip install flask-tjfu-captcha\n```\n\n### Getting Started\n\n```python\nfrom datetime import timedelta\n\nfrom flask import Flask\nfrom flask_tjfu_captcha import TjfuCaptcha\n\napp = Flask(__name__)\ntjfu_captcha = TjfuCaptcha(\n    app,\n    True  # Must be in Debug mode\n)\n\n\n@app.route('/get_captcha')\ndef get_captcha():\n    return tjfu_captcha.generate_image_captcha(\n        secret_key=\"YOUR_SECRET_KEY\",\n        length=4,\n        expires_in=timedelta(minutes=5),\n        only_digits=True\n    ).to_dict()\n\n\n@app.route('/verify_captcha')\n@tjfu_captcha.required_captcha(\n    \"YOUR_SECRET_KEY\"\n)\ndef verify_captcha():\n    return \"OK\"\n\n\nif __name__ == '__main__':\n    app.run(debug=True)\n```\n\n`/get_captcha` will generate a JSON with two attributes:\n\n- `encrypted_code`: encoded information of the generated captcha.\n- `img_base64`: base64 encoded string of the captcha image.\n\n```json\n{\n  \"encrypted_code\": \"...\",\n  \"img_base64\": \"...\"\n}\n```\n\n`/verify_captcha` uses `required_captcha` to enforce captcha verification before executing requests\nwithin `verify_captcha`. To pass captcha verification, the client must provide two headers: `Tjfu-Captcha-Code`\ncontaining the captcha code visible in the captcha image requested, and `Tjfu-Captcha-Encrypted-Code` containing the\nencoded information mentioned above.\n\n```http request\nGET http://0.0.0.0:5000/verify_captcha\nTjfu-Captcha-Encrypted-Code: <encrypted_code>\nTjfu-Captcha-Code: <code>\n```\n\n### Skipping Captcha Verification in Debug Mode\n\nTo bypass captcha verification during testing, you can use the `Tjfu-Captcha-Debugging-Ignore` header with any value,\nand TjfuCaptcha must be in Debug mode.\n\n```python\nfrom flask import Flask\nfrom flask_tjfu_captcha import TjfuCaptcha\n\napp = Flask(__name__)\ntjfu_captcha = TjfuCaptcha(\n    app,\n    True  # Must be in Debug mode\n)\n```\n\n```http request\nGET http://0.0.0.0:5000/verify_captcha\nTjfu-Captcha-Debugging-Ignore: <you can put any value>\n```\n\n### Customization\n\nYou can customize headers for captcha verification\nusing `TJFU_CAPTCHA_ENCRYPTED_CODE_HEADER_KEY`, `TJFU_CAPTCHA_CAPTCHA_CODE_HEADER_KEY`, `TJFU_CAPTCHA_DEBUGGING_IGNORE_HEADER_KEY`,\nand `TJFU_CAPTCHA_FONTS` to adjust captcha image fonts.\n\n```python\nfrom flask import Flask\n\napp = Flask(__name__)\n\napp.config['TJFU_CAPTCHA_FONTS'] = \"['your_font_path', 'your_other_font_path']\"\n# Change Tjfu-Captcha-Encrypted-Code header to Request-ID\napp.config['TJFU_CAPTCHA_ENCRYPTED_CODE_HEADER_KEY'] = 'Request-ID'\n# Change Tjfu-Captcha-Code header to My-Captcha-Code\napp.config['TJFU_CAPTCHA_CAPTCHA_CODE_HEADER_KEY'] = 'My-Captcha-Code'\n# Change Tjfu-Captcha-Debugging-Ignore header to Ignore-Captcha\napp.config['TJFU_CAPTCHA_DEBUGGING_IGNORE_HEADER_KEY'] = 'Ignore-Captcha'\n```\n\nYou can also customize responses for missing headers (`on_missing_header`) and invalid captcha\ncodes (`on_invalid_captcha_code`).\n\n```python\nfrom flask import Flask\nfrom flask_tjfu_captcha import TjfuCaptcha\n\napp = Flask(__name__)\ntjfu_captcha = TjfuCaptcha(\n    app,\n    True\n)\n\n\ndef on_missing_header(header, status_code):\n    return {\n        \"error\": f\"Missing header: {header}\",\n        \"status_code\": status_code\n    }\n\n\ndef on_invalid_captcha_code(status_code):\n    return {\n        \"error\": \"Invalid captcha code\",\n        \"status_code\": status_code\n    }\n\n\ntjfu_captcha.on_missing_header(\n    on_missing_header\n)\n\ntjfu_captcha.on_invalid_captcha_code(\n    on_invalid_captcha_code\n)\n```\n## Changelog\n\n### Version 1.0.0 - Initial Release - June 22, 2024\n\n- Initial release of the library with core functionalities:\n    - Perform initialization and Captcha verification\n    - Provide functions for customization\n\nEach section in this changelog provides a summary of what was added, changed, fixed, or removed in each release of the\nsoftware. This helps users and developers understand the evolution of the project over time and highlights important\nupdates or improvements made in each version.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Implement CAPTCHA verification for Flask in a straightforward manner.",
    "version": "1.0.0",
    "project_urls": {
        "Homepage": "https://github.com/duynguyen02/flask-tjfu-captcha"
    },
    "split_keywords": [
        "python",
        " flask"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "84c7f7129993915557621be363dad9a9424703f6f8a46f32e4b1e74bac9b9b5f",
                "md5": "5f02aa5bd67dbf15e91f79d2d98a01ae",
                "sha256": "877bfb6fdee0b9a87768c3d1647efcf87dce6671a034bbeffda3b5749972a05b"
            },
            "downloads": -1,
            "filename": "flask_tjfu_captcha-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5f02aa5bd67dbf15e91f79d2d98a01ae",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 4591,
            "upload_time": "2024-06-22T12:29:49",
            "upload_time_iso_8601": "2024-06-22T12:29:49.709535Z",
            "url": "https://files.pythonhosted.org/packages/84/c7/f7129993915557621be363dad9a9424703f6f8a46f32e4b1e74bac9b9b5f/flask_tjfu_captcha-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c41d5b2eb69d1f425f6d36c142aefc32003a5a12f231596835b3b1dc28813898",
                "md5": "6ed2d38fabe6f3de747b690774d63344",
                "sha256": "a588d7fb3d2c928fc22b5cb7c3590bdb96746ff19262224c26375fce5970b2e7"
            },
            "downloads": -1,
            "filename": "flask-tjfu-captcha-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "6ed2d38fabe6f3de747b690774d63344",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4298,
            "upload_time": "2024-06-22T12:29:53",
            "upload_time_iso_8601": "2024-06-22T12:29:53.237615Z",
            "url": "https://files.pythonhosted.org/packages/c4/1d/5b2eb69d1f425f6d36c142aefc32003a5a12f231596835b3b1dc28813898/flask-tjfu-captcha-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-22 12:29:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "duynguyen02",
    "github_project": "flask-tjfu-captcha",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "flask-tjfu-captcha"
}
        
Elapsed time: 0.27802s