Skip to main content

One-Time Password Field for WT-Forms

Test Status Deploy Status Maintainability Test Coverage

What This?

This module has Google's 2 factor authentication fields for WT-Forms.

Features

  • Single-Page Application driven secret key generation.
  • QRCode Generation
  • Secret field and authentication validator that is based on [WTForms]

How to use

If you want QR Code:

First, write the form to generate secret key and authenticate it:

form.py

#!/usr/bin/env python
# coding=utf-8

from flask_wtf import Form
import wtforms.fields as fld
import wtforms.fields.html5 as fld5
import wtforms.validators as vld
from wtf_otp import OTPSecretKeyField, OTPCheck

class UserInfoForm(Form):
  name = fld.StringField()
  email = fld5.EmailField(validators=[
    vld.InputRequired(), vld.Email()
  ])
  password = fld.PasswordField()
  new_password = fld.PasswordField()
  confirm_password = fld.PasswordField(validators=[
    vld.EqualTo("new_password")
  ])
  secret = OTPSecretKeyField(
    qrcode_url="/qrcode", validators=[vld.InputRequired()]
  )

class LoginForm(Form):
  email = fld5.EmailField(validators=[
    vld.InputRequired(), vld.Email()
  ])
  passwrd = fld.PasswordField(validators=[
    vld.InputRequired()
  ])
  auth = fld.IntegerField(validators=[
    validators=[vld.InputRequired(), OTPCheck()]
  ])

Next, write the app as usual. However, you want to add a new route of which path is "/qrcode" that generates the qrcode. Fortuantely, the sufficient function is provided. An instance of OTPSecretKeyField provides qrcode function:

from .form import UserInfoForm

# /qrcode is assigned to generate_qrcode.
def generate_qrcode(req, res):
  form = UserInfoForm()
  secret = req.args.get("secret")
  if not secret:
      abort(404)
  res.make_response(form.secret.qrcode(
      secret, name="Test Example", issuer_name="Test Corp"
  ))
  resp.mimetype = "image/svg+xml"

Note that you can't get the instance by UserInfoForm.secret, because UserInfoForm.secret returns an instance of UnboundField.

If you don't want QRCode

If you don't want QRCode, just remove qrcode_url from the corresponding classes and remove QRCode generation class.

More detail

If you want more detail, please refer source code and example code.

License (MIT License)

The MIT License (MIT) Copyright (c) 2016- Hiroaki Yamamoto

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Metadata

Release files for wtf-otp 0.7.12

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

Source distribution (sdist)

Source distribution for wtf-otp 0.7.12
File Size Uploaded Provenance
wtf_otp-0.7.12.tar.gz 8.9 kB Without provenance Details

Release files / wtf_otp-0.7.12.tar.gz

Download URL wtf_otp-0.7.12.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2221d23a9574859adff8d363cb7eab5c61c8eedff03f619442dbaac03cd52f15
BLAKE2b-256 checksum
How to use checksums
3748867a8c0eb0dc2dd06885979319cf2ec13209879a214f113dcd934e398857
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.12.0 Linux/6.2.0-1015-azure

Security for wtf-otp 0.7.12

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.

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