Skip to main content

A field type that encrypts values using Fernet symmetric encryption.

Project description

ODMantic Fernet Field Type

Publish Python Package python-3.8-3.9-3.10-3.11-3.12-3.13 Package version PyPI Downloads


A field type that encrypts values using Fernet symmetric encryption.

Features

  • EncryptedString: A custom field type that transparently encrypts data before storing it in MongoDB and decrypts it when retrieved
  • EncryptedInt: A custom field type to encrypt Integer values.
  • EncryptedFloat: A custom field type to encrypt Floats values.
  • EncryptedJSON: A custom field type to encrypt JSONs.
  • Simple integration with ODMantic models
  • Compatible with FastAPI and starlette-admin
  • Keys rotation is possible by providing multiple comma separated keys in the env variable.

Installation

pip install odmantic-fernet-field-type

Quick Start

1. Set up your encryption key

This package requires a Fernet encryption key stored in the FERNET_KEY environment variable. You can generate a suitable key by running:

python -m pip install odmantic-fernet-field-type
fernet-key

This will output a generated key along with instructions for setting up your environment.

2. Basic Usage

Single Key

# .env
...

FERNET_KEY="xxxxxxxyyyyyyyyzzzzzzzzzzz="

Multiple Keys (For rotation)

# .env
...

FERNET_KEY="pppppppqqqqqqqrrrrrrrrrr=,xxxxxxxyyyyyyyyzzzzzzzzzzz="
from odmantic import Model
# Note: The import package is "odmantic_fernet_field" and not "odmantic_fernet_field_type"
from odmantic_fernet_field import EncryptedString, EncryptedInt, EncryptedFloat, EncryptedJSON

class User(Model):
    name: str
    email: str
    password_hash: str
    # This field will be automatically encrypted in the database
    secret_answer: EncryptedString
    account_no: EncryptedInt
    account_balance: EncryptedFloat
    bank_details: EncryptedJSON

...

# Create and save a user - the secret_answer, account_no, account_balance & bank_details will be encrypted in MongoDB
user = User(
    name="John", email="john@example.com", password_hash="...", secret_answer="April 1st, 2025", account_no=1234567890, 
    account_balance=1000000.00, bank_details={
        "accountHolder": "John Doe",
        "accountNumber": 1234567890,
        "type": "Checking",
        "isActive": True
    }
)

# When you retrieve the user, the secret_answer is automatically decrypted
retrieved_user = await engine.find_one(User, User.email == "john@example.com")
assert retrieved_user.secret_answer == "April 1st, 2025"  # This will pass!

Integration with FastAPI and starlette-admin

The package has been tested and works with FastAPI and starlette-admin:

from fastapi import FastAPI
from starlette_admin import Admin
from starlette_admin.contrib.odmantic import ModelView
from models import User

app = FastAPI()
admin = Admin(title="Admin Panel")

class UserAdmin(ModelView):
    # Configure your admin view
    pass

admin.add_view(UserAdmin(User))
admin.mount_to(app)

Security Considerations

  • Never hardcode encryption keys in your source code
  • Use environment variables
  • Rotate your encryption keys periodically [NEW]
  • Back up your encryption keys - if lost, encrypted data cannot be recovered

Compatibility

  • Python 3.9+
  • ODMantic 1.0.2+
  • MongoDB 6.0+
  • Tested with MongoDB 8.0.5

Dependencies

  • odmantic 1.0.2+
  • python-dotenv 1.0.1+
  • cryptography 44.0.2+

Inspiration

This package was inspired by django-fernet-fields, which provides similar functionality for Django models.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

odmantic_fernet_field_type-0.0.3.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

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

odmantic_fernet_field_type-0.0.3-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file odmantic_fernet_field_type-0.0.3.tar.gz.

File metadata

File hashes

Hashes for odmantic_fernet_field_type-0.0.3.tar.gz
Algorithm Hash digest
SHA256 0c3dd17672e5094e0300f4375f68d757fc8c473aace879e78b7d9b2ab9ac95bb
MD5 bb8e1e42d924b4084f7332e1776d4605
BLAKE2b-256 2965a89c42fff5d491b9a4f96e8d09b8021470698d6b8eb753d8f8d667489a47

See more details on using hashes here.

File details

Details for the file odmantic_fernet_field_type-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for odmantic_fernet_field_type-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 107987bd3d2189488821ce63ee4ea54301530feb82f94157e25420b95516ccc0
MD5 80b00a05218890d45f15f7f88097bae2
BLAKE2b-256 17e436e111b9575822da1d5ed01be893602ca36ea32cb48a857d19c192c8fb18

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