2017-02-24 1285 views
1

我使用httpie来测试我的自定义验证Django的(使用TokenAuthentication):“non_field_errors”:“无法使用提供的凭据登录

http POST http://127.0.0.1:8000/api-token-auth/ username='username1' password='Password123' 

我确实创造了使用AbstractUser一个自定义的身份验证的对象。

使用TokenAuthentication,我跟着文档,然后在我的REST_FRAMEWORK设置添加我的自定义TokenAuthentication:

REST_FRAMEWORK = { 
'DEFAULT_AUTHENTICATION_CLASSES': (
    'regis.models.CustomAuthentication', 
    ) 
} 

并在我安装的应用程序中添加了rest_framework.authtoken

AUTHENTICATION_BACKEND如下:

AUTHENTICATION_BACKENDS = [ 'regis.models.CustomAuthentication' ] 

这里是我的自定义验证类:

class CustomAuthentication(authentication.TokenAuthentication): 
    def authenticate(self, request): 
     username = request.META.get('X_USERNAME') 
     print(username) 
     user_model = get_user_model() 
     if not username: 
      return None 
    try: 
     user = user_model.objects.get(username=username) 
    except User.DoesNotExist: 
     raise exceptions.AuthenticationFailed('No such user') 
    return (user, None) 

urls.py:

urlpatterns += [ 
    url(r'^api-token-auth/', views.obtain_auth_token), 

    ] 

我几乎继DRF文档(http://www.django-rest-framework.org/api-guide/authentication/#custom-authentication)。如果有任何其他信息需要解决这个问题,请让我知道,我会更新。任何帮助我失去的将是伟大的。

要添加:出于好奇,如果我有自定义用户,是否需要制作自定义身份验证系统?

UPDATE:

我刚刚删除的类上面,并且只是在我REST_FRAMEWORK设置添加rest_framework.authentication.TokenAuthentication。我仍然使用自定义身份验证来提取我的用户。

它看起来像这样(不打算格式化从VIM格式化代码,以便吸。):

class CustomAuthentication(object): 
def authenticate(self, email=None, password=None): 

    User = get_user_model() 
    try: 
     user = User.objects.get(email=email) 
    except User.DoesNotExist: 
     return None 
    if user.check_password(password): 
     return user 

    return None 

def get_user(self, user_id): 
    try: 
     user_model = get_user_model() 
     user = user_model.objects.get(pk=user_id) 
    except User.DoesNotExist: 
     return None 

我用这个Django文档创建代码:https://docs.djangoproject.com/en/1.10/topics/auth/customizing/

+0

只是为了回答您的好奇心:不,如果您使用的是自定义用户,则不需要自定义身份验证。我在工作中使用的构建具有自定义用户,并使用Django附带的默认ModelBackend。 :) – Neelik

+0

@Neelik ayyyee勾搭我在那里的工作! –

+0

也许当公司增长一点时:P我们现在仍然是一个小本地团体。 – Neelik

回答

0

好的我解决了它。在我的设置中,我只需要删除AUTHENTICATIONS_BACKEND。我以为我的自定义后端是不同的,只是记录用户和令牌认证后端工作得到该令牌。

1

如果您搜索在DRF代码错误字符串,你觉得这(在authtoken/serializers.py

from django.contrib.auth import authenticate 
... 

if username and password:                                                    
    user = authenticate(username=username, password=password)                                           

    if user:                                                       
     # From Django 1.10 onwards the `authenticate` call simply                                          
     # returns `None` for is_active=False users.                                              
     # (Assuming the default `ModelBackend` authentication backend.)                                         
     if not user.is_active:                                                   
      msg = _('User account is disabled.')                                              
      raise serializers.ValidationError(msg, code='authorization') 
    else:                                                      
     msg = _('Unable to log in with provided credentials.')                                           
     raise serializers.ValidationError(msg, code='authorization') 
... 

所以看起来这取决于Django的版本你使用,这两种凭据不正确,或者用户不活跃(对于Django> = 1.10)?

您是否尝试过使用这些凭证在管理员中手动登录以验证它们?

+0

我正在做iOS开发并使用电子邮件和密码登录,这很好。我去了shell,并询问用户的用户名,它是正确的。我只是检查用户是否处于活动状态,并显示“true”。在我的问题中,一切看起来都是正确的,还是我错过了一些我不知道的东西? –

+0

顺便说一句,是的我使用的是Django 1.10.2 –

+0

如果用户已经过身份验证,该怎么办? –