即使凭据正确,也无法使用curl和django JWT登录

时间:2018-04-25 21:44:10

标签: django curl jwt django-rest-auth django-custom-user

我在django中有一个自定义用户模型:

class UserManager(BaseUserManager):
    def create_User(self, email, name, companyname=None, userphoto=None,
                    userphotokey=None, signupvalidatestring=None, is_active=False, phone=None, jobtitle=None,
                    isclient=False, issuitsviewer=False, issuitsadministrator=False, issuitssuperuser=False,
                    password=None):
        if not email:
            raise ValueError('Users must have an email address')
        if not name:
            raise ValueError('Users must have a name')
        user = self.model(email = self.normalize_email(email),
                          name = name,
                          companyname= companyname,
                          userphoto= userphoto,
                          userphotokey=userphotokey,
                          signupvaildatestring=signupvalidatestring,
                          is_active=is_active,
                          phone=phone,
                          jobtitle=jobtitle,
                          isclient=isclient,
                          issuitsviewer = issuitsviewer,
                          issuitsadministrator=issuitsadministrator,
                          isuitssuperuser=issuitssuperuser)
        if not password:
            user.set_unusable_password()
        else:
            user.set_password(password)
            user.is_active = True
            user.save(using=self._db)
            return user

    def create_superuser(self):
        pass

# permissions table



class STUser(AbstractBaseUser):
    email = models.EmailField(unique=True)
    name = models.CharField(max_length=255)
    companyname = models.CharField(max_length=200, blank=True, null=True)
    userphoto = models.CharField(max_length=200, blank=True, null=True)
    userphotokey = models.CharField(max_length=200, blank=True, null=True)
    signupvaildatestring = models.CharField(max_length=200, blank=True, null=True)
    is_active = models.BooleanField(default=False)
    phone = models.CharField(max_length=10, null=True, blank=True)
    jobtitle = models.CharField(max_length=100, null=True, blank=True)
    isclient = models.BooleanField(default=False)
    issuitsviewer = models.BooleanField(default=False)
    issuitsadministrator = models.BooleanField(default=False)
    issuitssuperuser = models.BooleanField(default=False)
    # password field function is provided by AbstractBaseUser

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name']
    EMAIL_FIELD = 'email'

class VenuePermissions(models.Model):
    user = models.ForeignKey(STUser, on_delete=models.CASCADE)
    venue = models.ForeignKey(Venue, on_delete=models.CASCADE, blank=True, null=True)
    isvenueviewer = models.BooleanField(default=False)
    isvenueeventplanner = models.BooleanField(default=False)
    isvenueadministrator = models.BooleanField(default=False)
    receiverfp = models.BooleanField(default=False)

我有jwt auth课程:

#REST Framework
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',

我有url端点:

from rest_framework_jwt.views import obtain_jwt_token
url(r'^api-token-auth/', obtain_jwt_token),

当我在shell中创建这样的用户时:

>>> from STuser.models import STUser
>>> user = STUser(email='blah@gmail.com',name='Christopher Jakob',password='zaq11qaz')
>>> user.save()

然后运行卷曲帖子:

curl -X POST -d "email=blah@gmail.com&password=zaq11qaz" http://127.0.0.1:8000/api-token-auth/

并尝试了JSON实现

curl -X POST -H "Content-Type: application/json" -d '{"email":"blah@gmail.com","password":"zaq11qaz"}' http://127.0.0.1:8000/api-token-auth/

我也试过wget:

wget --post-data="email=christopher.m.jakob@gmail.com&password=zaq11qaz" http://127.0.0.1:8000/api-token-auth /

我收到以下错误:

{"non_field_errors":["Unable to log in with provided credentials."]}
"POST /api-token-auth/ HTTP/1.1" 400 68

但是,我提交的emailpassword信息是数据库中的内容。

我不确定这里发生了什么。我想知道是不是我的经理?但我想,如果用户对象的情况根本不会被添加。如您所见,我正在使用自定义用户模型。

这是我正在使用的资源:

https://getblimp.github.io/django-rest-framework-jwt/

可能有助于我调查的问题: Django (using TokenAuthentication): "non_field_errors": "Unable to log in with provided credentials?

这个问题也可能提供帮助:调查一下。 JSON Web Token for Django REST won't authenticate to user database

我正在使用的软件包中的相关代码从第22行开始 https://github.com/GetBlimp/django-rest-framework-jwt/blob/master/rest_framework_jwt/serializers.py

1 个答案:

答案 0 :(得分:0)

Stackoverflow帮助有时会被击中或错过。

我得到了它的工作

首先。我不得不更新我的自定义管理器中的代码,它不正确:

Xpos=[["","",""],
  ["","",""],
  [1,2,3],
  [2,3,4],
  [0,0,0],
  [0,0,0]]

Xpos1 = []
for inner_list in Xpos:
    if 0 in inner_list or "" in inner_list:
        pass
    else:
        Xpos1.append(inner_list)

print Xpos1

然后我不仅要像我一样创建一个class UserManager(BaseUserManager): def create_user(self, email, name, password ,**extraarguments): if not email: raise ValueError('Users must have an email address') if not name: raise ValueError('Users must have a name') if not password: raise ValueError('User must have a password') user = self.model(email = self.normalize_email(email), name = name, **extraarguments) user.set_password(password) user.is_active = True user.save() return user def create_superuser(self, email, name, password, **extraarguments): if not email: raise ValueError('Users must have an email address') if not name: raise ValueError('Users must have a name') if not password: raise ValueError('Users must have a password') user = self.model(email = self.normalize_email(email), name = name, **extraarguments) user.set_password(password) user.is_active = True user.save() return user 对象,而且要在管理器中调用STUser函数。

所以在shell中

create_user

然后发出新的卷曲请求

>>>from STuser.models import STUser
>>> user = STUser.objects.create_user('bobby@test.com','chris','zaq11qaz')
>>> exit()

看看发生了什么...

curl -X POST -d "email=bobby@test.c
om&password=zaq11qaz" http://127.0.0.1:8000/api-token-auth/

soooo很酷。

拥抱和亲吻