简体   繁体   English

django用户身份验证,无需输入密码

[英]django user auth without typing password

I want a feature workflows as follows: 我希望功能工作流程如下:

1) user login from web page ( typing user name/password),and auth success, then we can got user's password from database(not from web page), we will use it later. 1)用户从网页上登录(键入用户名/密码),并进行身份验证成功,那么我们可以从数据库(而不是网页)中获取用户的密码,以后再使用。

2) user confirm start a small bot service that provide by us, once user confirm that, then the service will execute and callback 3) since the bot service is another independent app, so it have to use user account callback login action (auth) and record information under the user account. 2)用户确认启动由我们提供的小型bot服务,一旦用户确认,该服务便会执行并回调3)由于bot服务是另一个独立的应用程序,因此它必须使用用户帐户回调登录操作(auth)并在用户帐户下记录信息。

my question is, while I use the user's account login in bot service app, it failed, since the password has been hash. 我的问题是,当我在bot服务应用中使用用户的帐户登录时,由于密码已被哈希处理,因此登录失败。

is there any way solve this issue ? 有什么办法解决这个问题?

I trace django source code , 我跟踪django源代码,

djanofrom django.contrib import auth

auth.login(request,authenticate)

seem no other way solve this issue unless modify the source code, I meaning add new function in framework? 除非修改源代码,否则似乎没有其他方法可以解决此问题,我的意思是在框架中添加新功能? but obviously, it is not best idea 但显然,这不是最好的主意

anyone give tips on this issue, thanks 任何人都可以给这个问题的提示,谢谢

You should write a custom auth backend. 您应该编写一个自定义身份验证后端。

https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#writing-an-authentication-backend https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#writing-an-authentication-backend

from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User

class PasswordlessAuthBackend(ModelBackend):
    def authenticate(self, username=None):
        try:
            return User.objects.get(username=username)
        except User.DoesNotExist:
            return None

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

then add this to your settings.py 然后将其添加到您的settings.py

AUTHENTICATION_BACKENDS = (
    # ... your other backends
    'yourapp.auth_backend.PasswordlessAuthBackend',
)

after that you can login in your views with 之后,您可以使用

user = authenticate(username=user.username)
login(request, user)

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

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