Skip to main content

ODMantic Fernet Field Type

Publish Python Package python-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.
  • EncryptedDecimal: A custom field type to encrypt decimal.Decimal 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 decimal import Decimal

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, EncryptedDecimal

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
    amount: EncryptedDecimal

...

# Create and save a user - the secret_answer, account_no, account_balance, bank_details & amount 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, amount=Decimal("100.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
  • 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.

Metadata

Release files for odmantic-fernet-field-type 0.0.4

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

Source distribution (sdist)

Source distribution for odmantic-fernet-field-type 0.0.4
File Size Uploaded
odmantic_fernet_field_type-0.0.4.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for odmantic-fernet-field-type 0.0.4
File Interpreter ABI Platform
odmantic_fernet_field_type-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 14.4 kB

Release files / odmantic_fernet_field_type-0.0.4.tar.gz

Download URL odmantic_fernet_field_type-0.0.4.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
641b81badd55c050c3a894807babb2834eb5b68b5aef2280c383052bea6cd0f9
BLAKE2b-256 checksum
How to use checksums
8bda73d8f33526ed58be144bdda0f4091fdd11b88f74676a0ac6df30640012af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / odmantic_fernet_field_type-0.0.4-py3-none-any.whl

Download URL odmantic_fernet_field_type-0.0.4-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
36ce06a62bae2e1c8bcb3ef5dd5182770413b540a5dd40aa57f0490104f654d4
BLAKE2b-256 checksum
How to use checksums
fd7f21560ea484eab4a3c843b386db657cd897c5a76f91f74d55b60f6c51cd44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

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