Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joegasewicz/password-mixin
Small Python library that adds password hashing methods to ORM objects
https://github.com/joegasewicz/password-mixin
crypto hashing-algorithm orm password-hashing passwords
Last synced: 15 days ago
JSON representation
Small Python library that adds password hashing methods to ORM objects
- Host: GitHub
- URL: https://github.com/joegasewicz/password-mixin
- Owner: joegasewicz
- License: mit
- Created: 2021-05-31T13:40:21.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-11-26T02:28:02.000Z (almost 3 years ago)
- Last Synced: 2024-05-20T11:10:49.275Z (6 months ago)
- Topics: crypto, hashing-algorithm, orm, password-hashing, passwords
- Language: Python
- Homepage:
- Size: 44.9 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[![Upload Python Package](https://github.com/joegasewicz/password-mixin/actions/workflows/python-publish.yml/badge.svg)](https://github.com/joegasewicz/password-mixin/actions/workflows/python-publish.yml)
[![Python package](https://github.com/joegasewicz/password-mixin/actions/workflows/python-package.yml/badge.svg)](https://github.com/joegasewicz/password-mixin/actions/workflows/python-package.yml)# Password Mixin
Mixin that adds some useful methods to ORM objectsCompatible with Python `3.5 >`
# Install
```bash
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__`.```python
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: `":"`### Password Hashing
```python
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
```python
from password_mixin import PasswordMatchErrortry:
user.check_password("wizard111")
except PasswordMatchError:
# handle passwords don't match
```### Example with Flask & Flask-Sqlalchemy
```python
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```python
u = UserModel(email="[email protected]", password="wizard123")
u.create_user()
db.session.commit()
```