Skip to main content

A simple asynchronous authentication module for PostgreSQL

Project description

pg_simple_auth

A lightweight and easy-to-integrate authentication module for asynchronous Python applications using PostgreSQL and Quart. pg_simple_auth offers essential user authentication features such as signup, login, email verification, and password management, all built on secure and modern standards.

Features

  • Asynchronous Design: Fully asynchronous using asyncio and asyncpg to ensure non-blocking I/O operations.
  • Seamless Integration: Designed to work effortlessly with the Quart ASGI web framework.
  • Secure Password Management: Passwords are securely hashed using argon2, one of the most secure hashing algorithms available.
  • JWT-Based Authentication: Implements JSON Web Tokens (JWT) for stateless, secure user authentication.
  • Built-in Email Verification: Provides token-based email verification out of the box to ensure user identity.

pg_simple_auth lets you choose your framework, email sender, and app server while staying simple and easy to understand.

Installation

Install the necessary dependencies via pip:

pip install pg_simple_auth

Usage

Setting Up the Application

In your Quart application, initialize the pg_simple_auth module with the database configuration and secret key:

from quart import Quart
import asyncpg
import pg_simple_auth as auth

app = Quart(__name__)

DATABASE_URL = "postgresql://user:password@localhost/dbname"
SECRET_KEY = "your_secret_key"
TABLE_NAME = "users"

@app.before_serving
async def setup_db():
    app.db_pool = await asyncpg.create_pool(DATABASE_URL)
    await auth.initialize(app.db_pool, SECRET_KEY, TABLE_NAME)

# Add your routes and other configurations

if __name__ == '__main__':
    app.secret_key = SECRET_KEY
    app.run()

Implementing Authentication Routes

The module provides easy-to-use methods for signup, login, and user session management:

@app.route('/signup', methods=['POST'])
async def signup():
    user = ...
    password = ...
    user_info = await auth.signup(user, password)
    await auth.verify(user_info['verification_token'])
    # auto verification, you may want to send an email here
    ...

@app.route('/login', methods=['POST'])
async def login():
    user = ...
    password = ...
    await auth.login(user, password)
    user_info = await auth.login(email, password)
    if user_info:
        session['token'] = user_info['token']
    ...

@app.route('/forgot_password', methods=['POST'])
async def forgot_password():
    reset_token = await auth.forgot_password(email)
    # generate an email with the reset_token

@app.route('/change_password', methods=['GET', 'POST'])
async def change_password(token):
    ...
    if method == 'POST':
        await auth.reset_password(token, new_password)
    ...

@app.route('/signout')
async def signout():
    session.pop('token', None)
    return redirect(url_for('login'))

Example

Check the examples/1-quart.py file in this repository for a full example of how to set up and use pg_simple_auth in a Quart application.

Requirements

  • Python 3.8+
  • PostgreSQL 10+ (for proper asyncpg compatibility)
  • asyncpg library
  • Quart ASGI framework

Author

Developed by 255labs.xyz, an AI product and consulting startup committed to helping people navigate the AI era through innovative products and open-source contributions.

Changelog

  • 09/03/24 - 0.1.1, add oauth support, login_oauth(provider, oauth_id) and signup_oauth(provider, oauth_id, **insert_args)

Contributing

Contributions are highly encouraged! Please open an issue to discuss potential changes or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Acknowledgements

  • asyncpg for providing a robust asynchronous PostgreSQL driver.
  • The developers of Quart for creating an excellent ASGI framework for Python.
  • The PostgreSQL community for their powerful and reliable database system.

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

pg_simple_auth-0.1.3.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pg_simple_auth-0.1.3-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file pg_simple_auth-0.1.3.tar.gz.

File metadata

  • Download URL: pg_simple_auth-0.1.3.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for pg_simple_auth-0.1.3.tar.gz
Algorithm Hash digest
SHA256 f57647c29cb8d8e5e4cc2cc811e4cecdbdf2ea95a4eacf05dc6abc587680967c
MD5 3ed84c052a75dfc9c500e24cacec511c
BLAKE2b-256 68dd5357ba595dd35fd2189465db2ac78ff1be6188c9967d728ce217b052a006

See more details on using hashes here.

File details

Details for the file pg_simple_auth-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pg_simple_auth-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for pg_simple_auth-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b0343c1e237102e5e7e1c8f3a17ba17d0e60efac12338f87bdf6b758180d1d18
MD5 dd787933232d11e9fb69064e068b978c
BLAKE2b-256 69e9d93d9bc29108c985f11d2e793b4796b7f017f1488a7b0c0dbff8e2afb730

See more details on using hashes here.

Supported by

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