Opened 14 years ago
Closed 13 years ago
#14390 closed New feature (fixed)
set_password functionality outside of the User model
Reported by: | Owned by: | Łukasz Rekucki | |
---|---|---|---|
Component: | contrib.auth | Version: | 1.2 |
Severity: | Normal | Keywords: | |
Cc: | druidjaidan@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Today I wanted to use a hashed, salted password in a custom model. Instead of reinventing the wheel or looking for it in other places, I tried to use the Django implementation from contrib.auth. Checking a password was fine as django.contrib.auth.models.check_password is not tied to the User model, but creating it was harder as set_password is a method on the
User model. I had to choose between copying the implementation from set_password or calling set_password on a throwaway User object.
Perhaps the salt-and-hash action from set_password could be placed in a separate public, documented function (make_password? hash_password?) that set_password then uses?
Attachments (2)
Change History (13)
comment:1 by , 14 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
comment:2 by , 14 years ago
Resolution: | worksforme |
---|---|
Status: | closed → reopened |
No, you really can't use that:
>>> class A(object): ... def m(self): ... return 3 ... >>> class B(object): ... pass ... >>> A.m(B()) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unbound method m() must be called with A instance as first argument (got B instance instead) >>>
comment:4 by , 14 years ago
I would fully support separating this out into a module-level function. It doesn't feel right to have it in models.py
, so let's make a utils.py
with that function in there. Can somebody make a patch?
comment:5 by , 14 years ago
Owner: | changed from | to
---|---|
Status: | reopened → new |
Triage Stage: | Unreviewed → Accepted |
I'll try to redeem myself ;)
comment:6 by , 14 years ago
Cc: | added |
---|
comment:7 by , 14 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
Status: | new → assigned |
comment:8 by , 14 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
by , 14 years ago
Attachment: | patch_ticket14390.diff added |
---|
Patch with docs and tests. Also can also view & comment this on github: http://github.com/lqc/django/commit/246059c9b1da777974ad9d804989a2fb912208f1
comment:9 by , 14 years ago
Easy pickings: | unset |
---|---|
Patch needs improvement: | set |
Severity: | → Normal |
Type: | → Uncategorized |
patch_ticket14390.diff fails to apply cleanly on to trunk
comment:10 by , 13 years ago
Type: | Uncategorized → New feature |
---|
You don't have to create a User model. If your custom model also has a password field, you can just use:
Alternatively, you can user an anonymous object:
If the later is too cumbersome for you, just copy those 2 lines from User model.