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
|
"""passlib.ext.django.models
.. warning::
This code is experimental and subject to change,
and not officially documented in Passlib just yet
(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, CryptPolicy
from passlib.utils import is_crypt_context, bytes
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 not ctx:
return
if ctx == "passlib-default":
ctx = DEFAULT_CTX
if isinstance(ctx, (unicode, bytes)):
ctx = CryptPolicy.from_string(ctx)
if isinstance(ctx, CryptPolicy):
ctx = CryptContext(policy=ctx)
if not is_crypt_context(ctx):
raise TypeError("django settings.PASSLIB_CONTEXT must be CryptContext instance or config string: %r" % (ctx,))
#monkeypatch django.contrib.auth.models:User
set_django_password_context(ctx, get_category=catfunc)
patch()
#===================================================================
#eof
#===================================================================
|