Skip to main content

A field type that encrypts values using Fernet symmetric encryption.

Project description

ODMantic Fernet Field Type

A specialized encrypted string field type for ODMantic that provides transparent encryption/decryption of string data in MongoDB.

Features

  • EncryptedString: A custom field type that transparently encrypts data before storing it in MongoDB and decrypts it when retrieved
  • Simple integration with ODMantic models
  • Compatible with FastAPI and starlette-admin

Installation

pip install odmantic-fernet-field-type

Quick Start

1. Setup 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

from odmantic import Model, Field
from odmantic_fernet_field import EncryptedString

class User(Model):
    name: str
    email: str
    password_hash: str
    # This field will be automatically encrypted in the database
    secret_answer: EncryptedString

...

# Create and save a user - the secret_answer will be encrypted in MongoDB
user = User(name="John", email="john@example.com", password_hash="...", secret_answer="April 1st, 2025")

# 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 or a secure key management solution
  • Rotate your encryption keys periodically [Coming Soon]
  • 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.1.tar.gz (4.3 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.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for odmantic_fernet_field_type-0.0.1.tar.gz
Algorithm Hash digest
SHA256 62aa6f4734b140e9b9de311d0b3de6dc1ac0f8c28ddba833ba6fe31cc58347d7
MD5 0326481ad138b4491a2574f50e8ff75a
BLAKE2b-256 4b28627488a67dc20606338b59f02661c3a8c44233921aa7619d0fb838447edd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for odmantic_fernet_field_type-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 59610b02f5c6b4bbc54fdf0d9497b8efc74013ac93c7c4e81ed0058e300f2486
MD5 ab5f5cfdaef5162275831f3871780175
BLAKE2b-256 444baeb9b225ff402e9bb3be18e8f59097990e109662e8426fe5353ca1e381e3

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