Skip to main content

flask-session-captcha

Latest version Supported python versions License

Downloads Downloads Downloads


A captcha implemention for flask using flask-session and captcha packages. Each captcha challenge answer is saved in the server side session of the challenged client. Support for different types of captchas such as numeric/letter/symbol captchas.

Requirements

  • Flask
  • flask-session with packages depending on which SESSION_TYPE you use. E.g. sqlalchemy requires flask-sqlalchemy.
  • captcha

Changelog

  • 1.4.2 - Bump pillow from 10.2.0 to 10.3.0 by @dependabot in #49
  • 1.4.1 - Fix error thrown when flask-session-captcha is init-ed without a Flask app object. Rename CAPTCHA_LOG environment variable to CAPTCHA_DEBUG_LOG.
  • 1.4.0 - Migrated from flask-sessionstore to flask-session. Added functionality for alphabetic and punctuation characters to be included in the captcha (thanks @alisharify7). Support moved to python 3.8, 3.9, 3.10, 3.11.

Usage

import uuid
import logging
from flask import Flask, request, render_template
from flask_session import Session
from flask_session_captcha import FlaskSessionCaptcha

app = Flask(__name__)
app.config["SECRET_KEY"] = uuid.uuid4().hex

# captcha configs:
app.config['CAPTCHA_ENABLE'] = True
app.config['CAPTCHA_LENGTH'] = 5
app.config['CAPTCHA_WIDTH'] = 200
app.config['CAPTCHA_HEIGHT'] = 160
# app.config['CAPTCHA_LOG'] = False # log information to terminal
# app.config['CAPTCHA_INCLUDE_ALPHABET'] = False
# app.config['CAPTCHA_INCLUDE_NUMERIC'] = True
# app.config['CAPTCHA_INCLUDE_PUNCTUATION'] = False
# app.config['CAPTCHA_SESSION_KEY'] = 'captcha_image' # In case you want to use another key in your session to store the captcha

# session config
app.config['SESSION_TYPE'] = 'redis' # or other type of drivers for session, see https://flask-session.readthedocs.io/en/latest/
Session(app)
captcha = FlaskSessionCaptcha(app)

@app.route('/', methods=['POST','GET'])
def some_route():
    if request.method == "POST":
        if captcha.validate():
            return "captcha validated successfully"
        else:
            return "invalid captcha/answer"

    return render_template("form.html")

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

Template can look as follows. captcha.validate() will be default try to validate against a form input with name "captcha".

<form method="POST">
    {{ captcha() }} <!-- This renders an <img> tag with the captcha img. -->
    <input type="text" name="captcha">
    <input type="submit">
</form>

It can also take a css_class argument to add classes to the generated DOM:

<form method="POST">
    {{ captcha(css_class="captcha") }}
    <input type="text" name="captcha">
    <input type="submit">
</form>

You can also override settings for the captcha contents itself, via include_alphabet, include_numeric and include_punctuation. Like so:

<form method="POST">
    {{ captcha(include_alphabet=True) }}
    <input type="text" name="captcha">
    <input type="submit">
</form>

Metadata

Release files for flask-session-captcha 1.5.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-session-captcha 1.5.0
File Size Uploaded
flask_session_captcha-1.5.0.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-session-captcha 1.5.0
File Interpreter ABI Platform
flask_session_captcha-1.5.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 13.4 kB

Release files / flask_session_captcha-1.5.0.tar.gz

Download URL flask_session_captcha-1.5.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2f5e92b75c0506851e8c48f7bc034c521a9f4b64614a66e7724ee8b67ae59c93
BLAKE2b-256 checksum
How to use checksums
c5d56e38c5687da421bea78f84aff4afd308f5c3dbaf93238f5117ccab1d473a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / flask_session_captcha-1.5.0-py2.py3-none-any.whl

Download URL flask_session_captcha-1.5.0-py2.py3-none-any.whl
Size 6.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d6b4e37c03f8a24459b3320efd941ce1d8967f0979be295829240aa89248b453
BLAKE2b-256 checksum
How to use checksums
5f5da31deb554246d7ab36dd56d1ae1539896a59c21c5331edf911176340f4c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release history Release notifications | RSS feed

This release

1.5.0 This release

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

1 release file

1.1.0

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

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