Skip to main content

Upload Python Package Python package

Password Mixin

Mixin that adds some useful methods to ORM objects

Compatible with Python 3.5 >= 3.9

Install

pip install password-mixin

Setup

first create your objects (or ORM model) and add a __hash_secret__ meta field. Assign your application's secret value to __hash_secret__.

from password_mixin import PasswordMixin
from sqlalchemy import Model # or Django , Flask-Sqlalchemy... etc.

class UserModel(OrmModel, PasswordMixin):

    password = Column(String()) # you must have a `password`.


    # Now create a meta field to define the secret used to create the salt, for example:
    __hash_secret__ = "your-app-secret"

Usage

The password is saved as the following: "<hash_name>:<hash>"

Password Hashing

from password_mixin import PasswordAttributeError
try:
    user = UserModel()
    user.password = "wizard123"
    user.hash_password() # password is now `sha256:7ac5cf88e8c9d262b49af168d9c30e47f2945cc9c207f20af0a39f09aa04595e`
    # Now you can save your user to your db etc.
except PasswordAttributeError:
    # handle no password attribute

Validating Passwords

from password_mixin import PasswordMatchError

try:
    user.check_password("wizard111")
except PasswordMatchError:
     # handle passwords don't match

Example with Flask & Flask-Sqlalchemy

class UserModel(db.Model, PasswordMixin):

    __tablename__ = "users"
    __hash_secret__ = "wizard123"

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(100), nullable=False)
    password = db.Column(db.String(100), nullable=False)

    def create_user(self):
        self.hash_password()
        db.session.add(self)

Now, with the above setup you can run the following

u = UserModel(email="test1@test.com", password="wizard123")
u.create_user()
db.session.commit()

Metadata

Release files for password-mixin 0.1.3

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

Source distribution (sdist)

Source distribution for password-mixin 0.1.3
File Size Uploaded
password-mixin-0.1.3.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for password-mixin 0.1.3
File Interpreter ABI Platform
password_mixin-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 7.8 kB

Release files / password-mixin-0.1.3.tar.gz

Download URL password-mixin-0.1.3.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d611a663abc5f5b62c86a95b6c992b0cdad842722f2f34bcc6a50b57066ef1a1
BLAKE2b-256 checksum
How to use checksums
634a62f9e4ba9f5c31d7ef7362e1e23e22c412f98d27387ad0e2f406aa0e0955
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

Release files / password_mixin-0.1.3-py3-none-any.whl

Download URL password_mixin-0.1.3-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
85fd4679ff15fc6421aa844c3ca6b51c45d1dc587ff810b8843df20f8f9c768e
BLAKE2b-256 checksum
How to use checksums
ceccf6ceb838e6e433f0742d42dbd760f8cda8433261977e6812af68278d40c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.0

2 release files

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