我有一个Python库,是为了其他用途编写的。所以我将其导入Django中使用,但我面临的问题是如何获取密码。
mycustom_lib_function(username, password)
username
可以通过request.user
传递。
但是,我不确定如何获取密码。有人能帮我吗?谢谢。
我有一个Python库,是为了其他用途编写的。所以我将其导入Django中使用,但我面临的问题是如何获取密码。
mycustom_lib_function(username, password)
username
可以通过request.user
传递。
但是,我不确定如何获取密码。有人能帮我吗?谢谢。
User
中的密码已经被哈希加密,因此您无法获得它。请询问用户。
from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User
u = User.objects.all().first()
if check_password('the default password', u.password):
print 'user password matches default password'
else:
print 'user a set custom password'
你的函数mycustom_lib_function
不应该使用明文密码。在用户通过你的应用程序进行身份验证后,你会得到一个包含哈希密码的User
对象(来自django.contrib.auth.models
):
>>> user.username
u'myusername'
>>> user.password
u'sha1$98ffc$b5fd085b8bc1c05fd241dfc97230631926141fe7'
根据标准的网络安全建议,身份验证后请勿存储密码的明文值,因此您在表单中输入的实际密码不会以明文形式存储。
请注意,您可以通过执行以下操作来检查上述哈希值:
>>> from hashlib import sha1
>>> password = 'weak_password'
>>> _, salt, hashpw = user.password.split('$')
>>> sha1(salt+password).hexdigest() == hashpw
True
django.contrib.auth
不会为您执行此操作。最好设置OAuth类型凭据系统,它可以在不需要用户向中间站点透露密码的情况下执行此功能。您无法获取用户的密码,但可以检查给定值是否为正确的密码。
from django.contrib.auth.hashers import check_password
user = User.objects.get(id = user_id) #get the correct user using mail or id
if check_password('my_old_password',user.password):
#update the password here