That django authenticate function accepts two parameters, eg
django authenticate函数接受两个参数,如。
user = authenticate(username=username, password=password)
, but I would like to pass in an additional parameter to validate the entry. Is that possible to do so or can the authenticate function be overridden to achieve that?
,但我想传递一个附加参数来验证条目。可以这样做吗?或者可以重写authenticate函数以实现这一点吗?
Please advise, thanks.
请通知,谢谢。
2 个解决方案
#1
1
authenticate is just a python function you can use *args & **kwargs to pass multiple params and you can override the authenticate method like (How to override authenticate method ) to add your own validation there.
认证只是一个python函数,您可以使用*args和**kwargs来传递多个参数,并且您可以覆盖如(如何覆盖认证方法)这样的认证方法来添加您自己的验证。
#2
1
As far as I know, the authenticate()
function takes any keyword arguments as credentials. It simply forwards the credentials to the configured authentication backends. So it really depends on the authentication backend what you do with the passed arguments.
就我所知,authenticate()函数将任何关键字参数作为凭证。它只是将凭据转发到配置的身份验证后端。因此,如何处理传递的参数实际上取决于身份验证后端。
This means you can pass whatever you want to the authenticate()
function, but you might need to implement your own authentication backend. You can achieve that by specyfing custom auth backends in the settings.py
:
这意味着您可以向authenticate()函数传递任何您想传递的内容,但您可能需要实现自己的身份验证后端。你可以通过在设置中使用自定义的auth来实现这一点。
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'confirm.django.auth.backends.EmailBackend',
)
Here's the code for the EmailBackend
:
下面是邮件后端代码:
from django.contrib.auth.backends import ModelBackend, UserModel
class EmailBackend(ModelBackend): # pylint: disable=too-few-public-methods
'''
Authentication backend class which authenticates the user against his
e-mail address instead of his username.
'''
def authenticate(self, username=None, password=None, **kwargs): # pylint: disable=unused-argument
'''
Authenticate the user via his e-mail address.
:param str username: The username used to authenticate
:param str password: The password used to authenticate
:return: When successful, the User model is returned
:rtype: User model or None
'''
try:
user = UserModel.objects.get(email=username)
except UserModel.DoesNotExist:
return None
else:
if user.check_password(password) and self.user_can_authenticate(user):
return user
return None
When a user is logging in with an e-mail address, the credentials will be passed to the ModelBackend.authenticate()
method, which will fail because there's no username matching the e-mail address. After that, the EmailBackend.authenticate()
method is called, which will succeed (if the password matches). The first backend returning a User
instance "wins".
当用户以电子邮件地址登录时,凭据将被传递给ModelBackend.authenticate()方法,该方法将失败,因为没有匹配电子邮件地址的用户名。然后,调用emailback .authenticate()方法,该方法将成功(如果密码匹配)。返回用户实例的第一个后端“wins”。
So it depends what you want to do.
这取决于你想做什么。
If you simply want to allow users logging in with either their username or e-mail address (or any other field), then this is the way to go. Just create a new auth backend and add it to the AUTHENTICATION_BACKENDS
, together with django.contrib.auth.backends.ModelBackend
.
如果您只是希望允许用户登录他们的用户名或电子邮件地址(或任何其他字段),那么这是一种方法。只需创建一个新的auth后端,并将其添加到AUTHENTICATION_BACKENDS中,并与django.致歉。
If you need an additional field verified for the login, let's say a triplet like customer ID
, username
& password
, you need to create your own backend as well. However, this time the backend should be the only one configured in the AUTHENTICATION_BACKENDS
, thus replacing django.contrib.auth.backends.ModelBackend
with it.
如果您需要为登录验证一个额外的字段,比如客户ID、用户名和密码这样的三元组,那么您还需要创建自己的后端。但是,这一次后端应该是AUTHENTICATION_BACKENDS中配置的惟一一个后端,从而取代了django. scheme . authb .backends。ModelBackend。
If you want to know more about customizing authentication, just head over to the brilliant docs.
如果您想了解更多关于自定义身份验证的知识,请访问优秀的文档。
#1
1
authenticate is just a python function you can use *args & **kwargs to pass multiple params and you can override the authenticate method like (How to override authenticate method ) to add your own validation there.
认证只是一个python函数,您可以使用*args和**kwargs来传递多个参数,并且您可以覆盖如(如何覆盖认证方法)这样的认证方法来添加您自己的验证。
#2
1
As far as I know, the authenticate()
function takes any keyword arguments as credentials. It simply forwards the credentials to the configured authentication backends. So it really depends on the authentication backend what you do with the passed arguments.
就我所知,authenticate()函数将任何关键字参数作为凭证。它只是将凭据转发到配置的身份验证后端。因此,如何处理传递的参数实际上取决于身份验证后端。
This means you can pass whatever you want to the authenticate()
function, but you might need to implement your own authentication backend. You can achieve that by specyfing custom auth backends in the settings.py
:
这意味着您可以向authenticate()函数传递任何您想传递的内容,但您可能需要实现自己的身份验证后端。你可以通过在设置中使用自定义的auth来实现这一点。
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'confirm.django.auth.backends.EmailBackend',
)
Here's the code for the EmailBackend
:
下面是邮件后端代码:
from django.contrib.auth.backends import ModelBackend, UserModel
class EmailBackend(ModelBackend): # pylint: disable=too-few-public-methods
'''
Authentication backend class which authenticates the user against his
e-mail address instead of his username.
'''
def authenticate(self, username=None, password=None, **kwargs): # pylint: disable=unused-argument
'''
Authenticate the user via his e-mail address.
:param str username: The username used to authenticate
:param str password: The password used to authenticate
:return: When successful, the User model is returned
:rtype: User model or None
'''
try:
user = UserModel.objects.get(email=username)
except UserModel.DoesNotExist:
return None
else:
if user.check_password(password) and self.user_can_authenticate(user):
return user
return None
When a user is logging in with an e-mail address, the credentials will be passed to the ModelBackend.authenticate()
method, which will fail because there's no username matching the e-mail address. After that, the EmailBackend.authenticate()
method is called, which will succeed (if the password matches). The first backend returning a User
instance "wins".
当用户以电子邮件地址登录时,凭据将被传递给ModelBackend.authenticate()方法,该方法将失败,因为没有匹配电子邮件地址的用户名。然后,调用emailback .authenticate()方法,该方法将成功(如果密码匹配)。返回用户实例的第一个后端“wins”。
So it depends what you want to do.
这取决于你想做什么。
If you simply want to allow users logging in with either their username or e-mail address (or any other field), then this is the way to go. Just create a new auth backend and add it to the AUTHENTICATION_BACKENDS
, together with django.contrib.auth.backends.ModelBackend
.
如果您只是希望允许用户登录他们的用户名或电子邮件地址(或任何其他字段),那么这是一种方法。只需创建一个新的auth后端,并将其添加到AUTHENTICATION_BACKENDS中,并与django.致歉。
If you need an additional field verified for the login, let's say a triplet like customer ID
, username
& password
, you need to create your own backend as well. However, this time the backend should be the only one configured in the AUTHENTICATION_BACKENDS
, thus replacing django.contrib.auth.backends.ModelBackend
with it.
如果您需要为登录验证一个额外的字段,比如客户ID、用户名和密码这样的三元组,那么您还需要创建自己的后端。但是,这一次后端应该是AUTHENTICATION_BACKENDS中配置的惟一一个后端,从而取代了django. scheme . authb .backends。ModelBackend。
If you want to know more about customizing authentication, just head over to the brilliant docs.
如果您想了解更多关于自定义身份验证的知识,请访问优秀的文档。