Skip to main content

TwoFa: Two-Factor Authentication for Python

Introduction

TwoFa is a Python library designed to make two-factor authentication (2FA) integration seamless and secure for applications. By providing an easy-to-use interface for adding an additional layer of security, TwoFa ensures that user authentication is more robust and resistant to unauthorized access.

Features

  1. Easy Integration: Simplify the process of adding 2FA to your Python applications.
  2. Multiple 2FA Methods: Supports various two-factor authentication methods, including SMS, email, and authenticator apps.
  3. Customizable: Flexible configuration to suit different security requirements.
  4. Secure: Implements industry-standard protocols to ensure the security of authentication processes.
  5. Support Multiple Storage Device: Support AWS S3Bucket, Cloudinary e.t.c to manage authentication QR code.

Installation

Install TwoFa using pip:

pip install twofa

Quick Start

Here's how to quickly set up TwoFa in your project:

1. Import TwoFa

from twofa.auth import TwoFa

2. Initialize TwoFa

twofa = TwoFa(issuer_name='MyApp')

3. Generate QR Code For Authentication App

resp = twofa.generate_qr_code(identifier="identifier", storage="s3bucket", bucket_name="bucket", access_key="s3 secrete access", secret_access="s3 secrete access key")
print(resp)

The generate_qr_code function generates a QR code for two-factor authentication, stores it in a specified storage service, and returns the URL to the stored QR code image. This function is particularly useful for applications implementing 2FA where the QR code is scanned by a user's authentication app.

Parameters

  1. identifier (str): A unique identifier for the user, such as an email address or username, to be encoded within the QR code.

  2. storage (str): The storage service to use for saving the QR code image. Currently supports "s3bucket" and "cloudinary"; future versions may include additional services.

    S3 Bucket Configuration

    i. bucket_name (str): The name of the bucket in the storage service where the QR code image will be saved.

    ii. access_key (str): The access key ID for authenticating with the storage service.

    iii. secret_access (str): The secret access key for authenticating with the storage service.

    Cloudinary Configuration

    i. cloud_name (str): The name of the bucket in the storage service where the QR code image will be saved.

    ii. api_key (str): The access key ID for authenticating with the storage service.

    iii. api_secret (str): The secret access key for authenticating with the storage service.

Returns

After calling the QR code generation function, the service returns a JSON object containing the following fields:

  1. status (bool): Indicates the success or failure of the QR code generation and storage process. A value of True means the operation was successful, while False indicates a failure.
  2. url (str): The URL where the generated QR code image has been stored. This URL can be used to display the QR code to the user for scanning with a 2FA app.
  3. user_key (str): A unique key associated with the user's 2FA setup. This key is important for identifying the 2FA configuration specific to the user and may be needed for verifying the 2FA token generated by the user's authentication app.
  4. identifier (str): The unique identifier provided during the QR code generation request. Typically, this is the user's email address or username. It confirms the entity for which the QR code was generated.
    {
        "status": true,
        "url": "http://res.cloudinary.cvvxm2e4kj.png",
        "user_key": "4CXDI7QR53NTBWA3DD4DSY",
        "identifier": "johndoe@gmail.com"
    }

Fields Description

i. status: This boolean field is crucial for error handling in your code. Always check this field to ensure the operation was successful before proceeding.

ii. url: Use this URL to retrieve the QR code image. You can embed it in an tag in HTML or download it for display in other applications.

iii. user_key: Securely store this key in your application's database associated with the user's account. It will be required for validating 2FA tokens.

iv. identifier: Provides a way to double-check that the QR code generated corresponds to the correct user account, especially in systems where multiple requests might be processed simultaneously.

Handling Errors

In case of failure, the status field will be False, and the response may include an additional field, error, detailing the reason for the failure. Ensure your application can gracefully handle such scenarios and provide appropriate feedback to the user.

4. Validate Code

The validate_code function is designed to validate a two-factor authentication (2FA) code provided by the user. It checks the code against the user's previously generated and stored 2FA setup, ensuring that the code is correct and within its validity period.

Parameters

  1. user_key (str): A unique key associated with the user's 2FA setup. This key should have been saved during the 2FA setup process.
  2. code (str): The 2FA code that the user entered, which needs to be validated.

Returns

A dictionary containing the following keys:

  1. status (bool): Indicates the success or failure of the code validation. A value of True means the code is valid, while False indicates it is invalid or expired.
  2. message (str): Provides additional information about the validation result. Typical messages include 'valid' for successful validations or error messages if the validation fails.

Example Usage

Here's how to use the validate_code function:

resp = twofa.validate_code(user_key="DEPB73TJNMZYAVZ7IG", code="856864")
print(resp)
{"status": false, "message": "not valid"}

Handling Errors

The function is designed to be robust against common errors, returning a False status for any issue encountered during validation, including invalid codes, expired codes, or internal errors. Always check the status field to determine the outcome of the validation process.

Notes

i. Ensure the user_key corresponds to the correct user's 2FA setup before attempting to validate a code.

ii. The code parameter should be treated as sensitive information and handled securely throughout your application.

iii. This function should be integrated as part of your application's authentication flow, typically after the user has provided their primary credentials.

Advanced Usage

Refer to the documentation for advanced configurations, including setting up different 2FA methods and customizing token expiration times.

Contributing

We welcome contributions to TwoFa! If you have suggestions for improvements or bug fixes, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or fix.
  3. Commit your changes.
  4. Push to the branch.
  5. Submit a pull request.

Please refer to our contribution guidelines for more details.

License

TwoFa is released under the GNU GENERAL PUBLIC LICENSE.

Metadata

Release files for twofa 1.0.2

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

Source distribution (sdist)

Source distribution for twofa 1.0.2
File Size Uploaded Provenance Actions
twofa-1.0.2.tar.gz 45.6 kB Without provenance Details for twofa-1.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for twofa 1.0.2
File Interpreter ABI Platform Actions
twofa-1.0.2-py3-none-any.whl Python 3 none any Details for twofa-1.0.2-py3-none-any.whl

Total release size: 76.5 kB

Release files / twofa-1.0.2.tar.gz

Download URL twofa-1.0.2.tar.gz
Size 45.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2a04f5bd1f7283b6af43b6f8b8d19758a20dc4b460e01fa1c2a025a4a3d139ba
BLAKE2b-256 checksum
How to use checksums
b0db25b52513fb04d2a296746a9896d980402cbb0d37f430c59d95ec689c0b33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.11

Release files / twofa-1.0.2-py3-none-any.whl

Download URL twofa-1.0.2-py3-none-any.whl
Size 30.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
82fca65fe81384edb8aa09e0cbebca47817c7fd6a50a5d5aa611e5ba943c4a71
BLAKE2b-256 checksum
How to use checksums
4dfc2e6af66b331de20bbcf96a3e64ae6eb5af1cf428bfdeeffffec572513040
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.11

Security for twofa 1.0.2

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.2 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