Skip to main content

Encrypted IDs for Django Models

Project description

======================
Django Encrypted Id (cryptography)
======================

.. image:: https://badge.fury.io/py/django-encrypted-id-cryptography.svg
:target: https://badge.fury.io/py/django-encrypted-id-cryptography

.. image:: https://travis-ci.org/slinkymanbyday/django-encrypted-id-cryptography.svg?branch=master
:target: https://travis-ci.org/slinkymanbyday/django-encrypted-id-cryptography


django-encrypted-id-cryptography is a Django model which allows the use of encrypted ids for when you don't want to expose the regular pk.

**Note: This is a fork from django-encrypted-id and acts *mostly* a drop in replacement, it requires an additional settings and the encrypted ids are not compaitable. It also uses a different crypto library**


Requirements
------------
The following have been tested, however it should work with more.

* Django [1.11, 2.0, 2.2]
* Python [2.7, 3.5]
* Cryptography

Quickstart
----------

Install django-encrypted-id-cryptography::

pip install django-encrypted-id-cryptography


Create an encryption key:

.. code-block:: python

> from cryptography.fernet import Fernet
> Fernet.generate_key()

'3CNek72sQ3syTXX6h-Z1t3OLKKY1lfAgnTW_THUz37M='


.. code-block:: python

ID_ENCRYPT_KEY = ['3CNek72sQ3syTXX6h-Z1t3OLKKY1lfAgnTW_THUz37M=']

Features
--------

This password validator returns a ValidationError if the PWNED Passwords API
detects the password in its data set. Note that the API is heavily rate-limited,
so there is a timeout (:code:`PWNED_VALIDATOR_TIMEOUT`).

If :code:`PWNED_VALIDATOR_FAIL_SAFE` is True, anything besides an API-identified bad password
will pass, including a timeout. If :code:`PWNED_VALIDATOR_FAIL_SAFE` is False, anything
besides a good password will fail and raise a ValidationError.


Use
--------

Consider this example model:

.. code-block:: python

from django.db import models

from encrypted_id.models import EncryptedIDModel


class Foo(EncryptedIDModel):
text = models.TextField()


By inheriting from ``EncryptedIDModel``, you get .ekey as a property on your
model instances. This is how they will look like:

.. code-block:: python

In [1]: from tapp.models import Foo

In [2]: f = Foo.objects.create(text="asd")

In [3]: f.id
Out[3]: 1

In [4]: f.ekey
Out[4]: 'gAAAAABcyQ2WlhRT6zec6WCRMJ3mDkZL9SCy98JeMvrERki6DJgc3WeIRMbAMm86_zmV0sP3_iPvbAHGgb7RfEGrnIIYdggaig=='
You can do reverse lookup:

In [5]: from encrypted_id import decode

In [6]: decode(f.ekey)
Out[6]: 1

If you can not inherit from the helper base class, no problem, you can just use
the ``ekey()`` function from ``encrypted_id`` package:

.. code-block:: python

In [7]: from encrypted_id import ekey

In [8]: from django.contrib.auth.models import User

In [9]: ekey(User.objects.get(pk=1))
Out[9]: 'gAAAAABcyQ2WlhRT6zec6WCRMJ3mDkZL9SCy98JeMvrERki6DJgc3WeIRMbAMm86_zmV0sP3_iPvbAHGgb7RfEGrnIIYdggaig=='


To do the reverse lookup, you have two helpers available. First is provided by
``EncryptedIDManager``, which is used by default if you inherit from
``EncryptedIDModel``, and have not overwritten the ``.objects``:

.. code-block:: python

In [10]: Foo.objects.get_by_ekey(f.ekey)
Out[10]: <Foo: Foo object>


But sometimes you will prefer the form:

.. code-block:: python

In [11]: Foo.objects.get_by_ekey_or_404(f.ekey)
Out[11]: <Foo: Foo object>


Which works the same, but instead of raising ``DoesNotExist``, it raises
``Http404``, so it can be used in views.

You your manager is not inheriting from ``EncryptedIDManager``, you can use:

.. code-block:: python

In [12]: e = ekey(User.objects.first())

In [13]: e
Out[13]: 'gAAAAABcyQ2WlhRT6zec6WCRMJ3mDkZL9SCy98JeMvrERki6DJgc3WeIRMbAMm86_zmV0sP3_iPvbAHGgb7RfEGrnIIYdggaig=='

In [14]: get_object_or_404(User, e)
Out[14]: <User: amitu>


``encrypted_id.get_object_or_404``, as well as
``EncryptedIDManager.get_by_ekey`` and
``EncryptedIDManager.get_by_ekey_or_404`` take extra keyword argument, that can
be used to filter if you want.

If you are curious, the regex used to match the generated ids is:

.. code-block:: python

"[0-9a-zA-Z-_=]+"


Running Tests
-------------

::

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox


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

django-encrypted-id-cryptography-1.1.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

django_encrypted_id_cryptography-1.1.0-py2.py3-none-any.whl (6.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-encrypted-id-cryptography-1.1.0.tar.gz.

File metadata

  • Download URL: django-encrypted-id-cryptography-1.1.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for django-encrypted-id-cryptography-1.1.0.tar.gz
Algorithm Hash digest
SHA256 205bd30cc52e078989d99551c5445b36806a92d438e7fb3585c73e70b314d46b
MD5 f02e8ed7d93beeddc9b040f04f3c7f9e
BLAKE2b-256 8b3455385cacf4680e0723964765febda4f4c3298a786a1f71c643cfd1e5688b

See more details on using hashes here.

File details

Details for the file django_encrypted_id_cryptography-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_encrypted_id_cryptography-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for django_encrypted_id_cryptography-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c8b6102496b7db3ce88f2d6d23b7907a473bd983febb1e43f240e05b308b88ca
MD5 b99b60f2bc8b5b88030f7256ac8e1e57
BLAKE2b-256 8fbe51d93abd9f22ce550659fef0a63049a2b4d09bec467897e2f4a8a878c46b

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