简体   繁体   English

Django:旧版(非auth_user)表的加密密码

[英]Django: Encrypted password to legacy (not auth_user) table

Django noob. Django新手。 Exported and imported tables from MySQL to PostgreSQL. 从MySQL导出和导入表到PostgreSQL。 The table for clients and customers is called users . 客户和客户的表称为users

Trying to update the password for the test account to what Django uses for encryption using python manage.py shell . 尝试将测试帐户的密码更新为Django使用python manage.py shell进行加密的密码。 Tried the following which obviously wouldn't work because the hashing algorithm isn't being imported in: 尝试了以下操作,由于未将哈希算法导入其中,因此显然不起作用:

from account.models import Users
user = Users.objects.filter(pk=2)
user.set_password('new_password')

Errors because there is no attribute 'set_password' . 由于no attribute 'set_password'而导致错误。

Fine. 精细。 Try what all the tutorials say: 尝试所有教程所说的内容:

from django.contrib.auth.models import User
User.objects.filter(pk=2)
Queryset []
User.objects.all()

Then it returns the only user in auth_user table. 然后,它返回auth_user表中的唯一用户。

So basically, how do I use Django's encryption against the legacy users table? 因此,基本上,如何对旧users表使用Django的加密?

It appears that in Django clients and customer accounts go in auth_user table? 看来在Django客户和客户帐户中进入auth_user表? I thought of that table as more for users who should have access to /admin and that clients/customers who use the app go in a different table. 我认为该表对应该有权访问/admin用户以及使用该应用程序的客户/客户位于另一个表中的作用更大。

EDIT 1: 编辑1:

The legacy table had the passwords stored with an admin defined salt (I believe Django uses a unique algorithm for each user or something like that so that is why I mention that the salt was the same for everyone) + user password that sent through the hashing algorithm (SHA256 I think). 旧表的密码与管理员定义的盐一起存储(我相信Django为每个用户或类似的东西使用独特的算法,所以这就是为什么我提到盐对每个人都是相同的)+通过哈希发送的用户密码算法(我认为是SHA256)。 So not plain text. 所以不是纯文本。

I cleared out everyones passwords and was just going to make new ones. 我清除了每个人的密码,然后将其设为新密码。 Since the old table used one hash and Django uses and entirely different one, I didn't want to mess with decrypting and re-encrypting under the Django hash. 由于旧表使用的是一个哈希值,而Django使用的是完全不同的哈希值,因此我不想在Django哈希值下搞乱解密和重新加密。 Just start from scratch. 从头开始。

Also, I would like to keep using the users table for clients/customers and keep the auth_user for users of /admin ... unless my thinking is wrong on this. 另外,我想继续为客户/客户使用users表,为/admin用户保留auth_user ...除非我的想法是错误的。

Well you haven't mentioned what your legacy table looks like, this answer assumes that it has the basic username, password and email fields. 好吧,您没有提到旧表的外观,该答案假定它具有基本的用户名,密码和电子邮件字段。 if you are storing passwords in plain text at the moment it can be hashed but if you are using some kind of third party solution, each user will have to reset his/her password. 如果您现在以纯文本形式存储密码,则可以对其进行哈希处理,但是如果您使用某种第三方解决方案,则每个用户都必须重置其密码。

import account.models import User as LegacyUser
import django.contrib.auth.models User

users = []
for u in LegacyUser.objects.all():
    new_user = User(username=u.user, email=u.email)
    new_user.set_password(u.password)
    users.append(new_user)

User.objects.bulk_create(users)

the above is if you want to salvage passwords, if you don't just do this: 以上是您是否想挽回密码,如果您不只是这样做:

INSERT INTO auth_user(username,password,email) SELECT username, password, email FROM account_user

in psql or pgadmin or whatever. 在psql或pgadmin或其他中。 This should be much faster. 这应该快得多。

Also, I would like to keep using the users table for clients/customers and keep the auth_user for users of /admin... unless my thinking is wrong on this. 另外,我想继续为客户/客户使用users表,为/ admin用户保留auth_user ...除非我的想法是错误的。

Sorry to say your thinking is wrong on this. 很抱歉,您对此的想法是错误的。 There is no need for two tables because the django.contrib.auth.User table has two fields is_staff and is_superuser specifically designed to deal with this situation. 不需要两个表,因为django.contrib.auth.User表具有两个字段is_staffis_superuseris_superuser专门设计用于处理这种情况。

Alternatively, you can ditch the django.contrib.auth.models.User model completely and use your user model as the default. 或者,您可以完全放弃django.contrib.auth.models.User模型,并使用您的用户模型作为默认模型。 See Specifying a custom user model having two models is just duplicating code and work and potentially introducing security problems 请参阅指定具有两个模型的自定义用户模型只是复制代码和工作,并可能引入安全性问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM