问题描述
我正在开发一个项目来启用移动设备的django休息框架身份验证。我使用默认令牌身份验证从发送用户名和密码的帖子请求中获取用户令牌。
curl --data username = username& password = passwordhttp://127.0.0.1:8000/api/api-token-auth/
(api / api-token-auth /是使用gets_auth_token视图配置的URL)
urlpatterns = [
url(r'^ api / api-token-auth /',gets_auth_token),
url(r'^',include(router.urls)),
]
,响应是用户令牌。
{token:c8a8777aca969ea3a164967ec3bb341a3495d234}
我需要获取用户令牌auth使用邮箱密码,而不是用户名密码,或两者兼而有之。我正在阅读自定义身份验证的文档 ...但真的,对我来说不是很清楚。
这对我非常有帮助...谢谢:)。
好的,我找到了一种方式来获得auth令牌使用电子邮件或用户名...这是序列化程序:
类AuthCustomTokenSerializer(serializers.Serializer):
email_or_username = serializers.CharField()
password = serializers.CharField()
def validate(self,attrs):
email_or_username = attrs.get('email_or_username')
password = attrs.get('password')
如果email_or_username和密码:
#检查用户是否发送电子邮件
如果validateEmail(email_or_username):
user_request = get_object_or_404(
用户,
电子邮件= email_or_username,
)
email_or_username = user_request.username
user = authenticate(username = email_or_username ,password = password)
如果用户:
如果不是user.is_active:
msg = _('用户帐户被禁用')
raise exceptions.ValidationError(msg)
else:
msg = _('无法使用提供的凭据登录)
raise exceptions.ValidationError(msg)
else:
msg = _('必须包含email或username和password')
raise exceptions.ValidationError(msg)
attrs ['user'] = user
return attrs
在email_or_username字段中,用户可以发送电子邮件或用户名,并使用,我们可以检查用户是否尝试使用电子邮件或用户名登录。然后,我们可以查询获取用户实例,如果它是有效的,并进行身份验证。
这是视图。
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ ,
parsers.MultiPartParser,
parsers.JSONParser,
)
renderer_classes =(renderers.JSONRenderer,)
def post(self ,请求):
serializer = AuthCustomTokenSerializer(data = request.data)
serializer.is_valid(raise_exception = True)
user = serializer.validated_data ['user']
令牌, created = Token.objects.get_or_create(user = user)
content = {
'token':unicode(token.key),
}
返回响应(内容)
然后:
curl --dataemail_or_username = emailorusername & password = passwordhttp://127.0.0.1:8000/api/my-api-token-auth/。
已经准备好了。
I'm working on a project to enable the django rest framework authentication for mobile devices. I'm using the default token authentication for get the user token from a post request sending username and password.
curl --data "username=username&password=password" http://127.0.0.1:8000/api/api-token-auth/
(api/api-token-auth/ is the url configured with the obtain_auth_token view)
urlpatterns = [
url(r'^api/api-token-auth/', obtain_auth_token),
url(r'^', include(router.urls)),
]
and the response is the user token.
{"token":"c8a8777aca969ea3a164967ec3bb341a3495d234"}
I need to obtain the user token auth using email-password on the post instead username-password, or both. I was reading the documentation of custom authentication http://www.django-rest-framework.org/api-guide/authentication/#custom-authentication... but really, isn't very clear to me.It's very helpful to me... thanks :).
Ok,I found a way for get the auth token using email or username... This is the serializer:
class AuthCustomTokenSerializer(serializers.Serializer):
email_or_username = serializers.CharField()
password = serializers.CharField()
def validate(self, attrs):
email_or_username = attrs.get('email_or_username')
password = attrs.get('password')
if email_or_username and password:
# Check if user sent email
if validateEmail(email_or_username):
user_request = get_object_or_404(
User,
email=email_or_username,
)
email_or_username = user_request.username
user = authenticate(username=email_or_username, password=password)
if user:
if not user.is_active:
msg = _('User account is disabled.')
raise exceptions.ValidationError(msg)
else:
msg = _('Unable to log in with provided credentials.')
raise exceptions.ValidationError(msg)
else:
msg = _('Must include "email or username" and "password"')
raise exceptions.ValidationError(msg)
attrs['user'] = user
return attrs
In the email_or_username field, the user can send the email or the username, and using the function validateEmail(), we can check if the user is trying to login using email or username. Then, we can make the query for get the user instance if is valid, and authenticate it.
This is the view.
class ObtainAuthToken(APIView):
throttle_classes = ()
permission_classes = ()
parser_classes = (
parsers.FormParser,
parsers.MultiPartParser,
parsers.JSONParser,
)
renderer_classes = (renderers.JSONRenderer,)
def post(self, request):
serializer = AuthCustomTokenSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
token, created = Token.objects.get_or_create(user=user)
content = {
'token': unicode(token.key),
}
return Response(content)
and then:
curl --data "email_or_username=emailorusername&password=password" http://127.0.0.1:8000/api/my-api-token-auth/.
It's ready.
这篇关于Django休息框架:使用电子邮件替代用户名获取身份验证令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!