Skip to main content

A simple, limited (basic) wrap of the popular Captcha library for Flask projects.

Project description

flask-includes-captcha

A simple, limited (basic) wrap of the popular Captcha library for Flask projects.

The extension creates Captchas and corresponding tokens that can be used for verification.

Installation

Install this extension with pip:

$ pip install flask-includes-captcha

Usage

Set up your app and configure a secure Captcha Key:

from flask import Flask

app = Flask(__name__)
app.config['CAPTCHA_KEY'] = "use a secure key" #change this

Import the Captcha class from this extension and initialize it with your app:

from flask_includes_captcha import FlaskCaptcha

flask_captcha = FlaskCaptcha(app)

Create a form that includes a string field for the text that should be compared and a hidden field for the web token. Implement a function that validates if the text matched with the Captcha:

from flask_wtf import FlaskForm
from wtforms import StringField, HiddenField, SubmitField, ValidationError

class ProtectedForm(FlaskForm):
    captcha_text = StringField("Characters")
    captcha_token = HiddenField()
    submit = SubmitField("Submit")
    
    def validate_captcha_text(self, field):
        if not flask_captcha.verify(self.captcha_text.data, self.captcha_token.data):
            raise ValidationError("Wrong Captcha")

Send the Captcha image and the token with the form:

from flask import render_template


@app.route('/captcha', methods=["GET", "POST"])
def captcha():
    form = ProtectedForm()

    if form.validate_on_submit():
        pass  # Start processing the form from here

    captcha = flask_captcha.create()
    form.captcha_token.data = captcha["token"]

    return render_template("form.html", captcha=captcha["image"], form=form)

The Captcha must be rendered in the template (./templates/form.html):

<img src="data:image/png;base64,{{ captcha }}" alt="CAPTCHA">

<form action="" method="post">
    {{ form.hidden_tag() }}
    {{ form.captcha_text.label }}
    {{ form.captcha_text() }}
    {{ form.captcha_token }}
    {{ form.submit }}
</form>

Dependencies

  • captcha
  • flask
  • flask-wtf
  • joserfc

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-includes-captcha-0.1.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

flask_includes_captcha-0.1.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file flask-includes-captcha-0.1.1.tar.gz.

File metadata

File hashes

Hashes for flask-includes-captcha-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a4733714dceff7879026b4dd88c4c8c420cbe7441eb97435005344cff495000c
MD5 dbe57497aced1d8677ece5ced2966812
BLAKE2b-256 02e6a949a58abc9bd2d90a96fd1faba0383dd13b4ff99b4aa70d395df97f20ca

See more details on using hashes here.

File details

Details for the file flask_includes_captcha-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_includes_captcha-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b4da241d6b591b00ecc43e40c9d3389692e9638faca0a05105951cd8ab6b9b60
MD5 0729ca377a3626276da35f595acdc474
BLAKE2b-256 c27a66fbaa8429488620e5f733b6e25fd831c0d61b09ec95c7c18939802fa244

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page