在django中使用哈希密码验证函数不是原始密码 [英] authenticate function in django using hashed password not the raw one

查看:516
本文介绍了在django中使用哈希密码验证函数不是原始密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用opensx(它在django上运行),用户将从其他站点重定向到此处,并从那里获取散列密码。
验证(用户名,密码)除了原始密码像龙不是散列的一个,

所以我需要使用带有散列密码的authenticate()所以我可以得到.backend属性并继续我的生活。



当我使用 login(request,user) code>没有验证方法。出现此错误:

  request.session [BACKEND_SESSION_KEY] = user.backend 
AttributeError:'User'对象没有属性'后端'

所以我需要使用authenticate函数在我的用户中获得.backend属性对象。
$ b

user = authenticate(username = username,password = password)是authenticate函数的格式,
这里的密码是一个原始密码,比如abc,我拥有的是一个散列密码(这就是这个abc密码存储在db中的方式)。



我现在被卡住了,有没有一种方法可以在django中使用哈希密码进行身份验证和登录? 打开edX使用 ratelimitbackend.backends.RateLimitModelBackend 进行认证,正如我们可以在设置。此后端需要使用未经哈希处理的密码进行身份验证。



如果您希望基于其散列密码对用户进行身份验证,则需要创建一个新的身份验证后端,如前所述在 django文档中。



我建议你从Django ModelBackend中得到一些启示,如 django.contrib.auth.backends



您看到的错误与缺少后端属性是我以前经历过的。在 impersonate_user 查看FUN(一个Open edX项目)这就是我们解决这个问题的方法(注意视图函数源代码中的注释):

  user = get_object_or_404(用户名,用户名=用户名,is_superuser = False,is_active = True)
user.backend =无
login(request,user)


I am working on openedx(which runs on django) and the user will be redirected here from other site and i'm being given the hashed password from there. Authenticate(username,password) excepts raw password like "dragon" not the hashed one,

So I need to use the authenticate() with the hashed password so that I can get the ".backend" attribute and move on with my life.

When I use login(request,user) without the authenticate method. this error appears:

request.session[BACKEND_SESSION_KEY] = user.backend
AttributeError: 'User' object has no attribute 'backend'

So I need to use the authenticate function to get that .backend attribute in my user object.

user = authenticate(username=username, password=password) is the format of the authenticate function, the password here is a raw password like "abc", what I have is a hashed password (which is the way this "abc" password will be stored in db).

I am stuck now, is there a way to authenticate and login using hashed passwords in django?

解决方案

Open edX uses the ratelimitbackend.backends.RateLimitModelBackend for authentication, as we can see in the settings. This backend requires the un-hashed password for authentication.

If you wish to authenticate a user based on its hashed password, you need to create a new authentication backend, as described in the django documentation.

I suggest you draw some inspiration from the Django ModelBackend, as implemented in django.contrib.auth.backends.

The error you see relative to the missing backend attribute is something that I have experienced before. In the impersonate_user view of FUN (an Open edX project) this is how we solve this problem (note the comment inside the source code of the view function):

user = get_object_or_404(User, username=username, is_superuser=False, is_active=True)
user.backend = None
login(request, user)

这篇关于在django中使用哈希密码验证函数不是原始密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆