summaryrefslogtreecommitdiff
path: root/passlib/ext/django/models.py
blob: 0bf9b992b67dc654856ecd7514bf11cc5b8d46ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""passlib.ext.django.models

.. warning::

    This code is experimental and subject to change
    (though it should work).

see the Passlib documentation for details on how to use this app
"""
#===================================================================
#imports
#===================================================================
#site
from django.conf import settings
#pkg
from passlib.context import CryptContext
from passlib.utils import is_crypt_context
from passlib.utils.compat import bytes, unicode, base_string_types
from passlib.ext.django.utils import DEFAULT_CTX, get_category, \
    set_django_password_context

#===================================================================
#main
#===================================================================
def patch():
    #get config
    ctx = getattr(settings, "PASSLIB_CONTEXT", "passlib-default")
    catfunc = getattr(settings, "PASSLIB_GET_CATEGORY", get_category)

    #parse & validate input value
    if ctx == "disabled" or ctx is None:
        # remove any patching that was already set, just in case.
        set_django_password_context(None)
        return
    if ctx == "passlib-default":
        ctx = DEFAULT_CTX
    if isinstance(ctx, base_string_types):
        ctx = CryptContext.from_string(ctx)
    if not is_crypt_context(ctx):
        raise TypeError("django settings.PASSLIB_CONTEXT must be CryptContext "
                        "instance or configuration string: %r" % (ctx,))

    #monkeypatch django.contrib.auth.models:User
    set_django_password_context(ctx, get_category=catfunc)

patch()

#===================================================================
#eof
#===================================================================