【问题标题】:How can I reuse raw user's password in django?如何在 django 中重用原始用户的密码?
【发布时间】:2011-07-05 04:56:15
【问题描述】:

当用户登录时,他提供了他的姓名和原始密码,这些密码经过哈希处理并与 db 的值进行比较。


def login(request):
    username = request.POST['username']
    password = request.POST['password']
    user = auth.authenticate(username=username, password=password)
    if user is not None and user.is_active:
        # user is active
        auth.login(request, user)
        # relink to right page
        return HttpResponseRedirect("/account/loggedin/")
    else:
        # error page
        return HttpResponseRedirect("/account/invalid/")

或者我可以使用:


@login_required
def index(request):
    if request.user.is_authenticated():
        return render_to_response('polls/index.html', {'sessionDic' : request.session})
    else:
        #some stuff

问题是:一旦用户登录,以下请求仅包含经过检查的 cookie,用户无需再次输入其凭据。


但是,我需要在每种方法中的 View 中都有原始用户密码才能登录 linux 用户并以该用户身份执行一些 linux 程序。例如su程序用于切换ritgh linux用户:


def ssh_command (user, password, command):
    child = pexpect.spawn('su -l %s -c \'%s\'' % (user, command))
    i = child.expect([pexpect.TIMEOUT, pexpect.EOF, 'Password: '])
    if i == 0: # Timeout
        print 'ERROR!'
        print 'su can\'t be executed:'
        print child.before, child.after
        return None
    if i == 1: # EOF
        print 'ERROR'
        print 'EOF error'
        print child.before, child.after
        return None
    child.sendline(password)
    return child

def main ():
    user = 'test'
    password = 'test'
    child = ssh_command (user, password, 'curl habrahabr.ru | wc -c')
    child.expect(pexpect.EOF)
    print child.before
    print child.after
    print child.match

如何存储原始用户密码并将其替换为所需的功能?

【问题讨论】:

    标签: python django passwords


    【解决方案1】:

    您可以将其存储在登录视图功能的会话数据中。至少它会随着会话而死。如果某些黑客获得了数据库访问权限,另一种选择是将其存储在数据库字段中,这将是可怕的。至少如果黑客在会话中使用密码获得数据库访问权限,他们只会获得当前会话的纯文本密码。确保适当地超时会话,或鼓励用户注销并在注销时删除会话数据。

    【讨论】:

      【解决方案2】:

      您需要访问明文密码。理想情况下,您将存储它并重新生成哈希以进行身份​​验证。为了安全起见,您还应该使用站点密码加密存储它。我自己有implemented this,但不是为Django。您将不得不重新编写 Django 的身份验证代码来实现这一点。

      【讨论】:

        【解决方案3】:

        这是另一个想法。不需要对 su 进行密码验证。而是使用 /etc/sudoers 来允许您的 Web 服务器用户以其他用户的身份运行。通过这种方式,您还可以限制可以运行的命令 - 您当前的视图是否可以防止将内容注入命令行?

        这样您就不需要保留用户密码,您只需为一个用户名 (wwwuser) 提供所需的权限。 Django 已经从登录中确定了用户是谁,所以我认为给它足够的权限来作为该用户做某事是没有问题的。

        【讨论】:

          猜你喜欢
          • 2019-04-07
          • 1970-01-01
          • 2011-02-16
          • 2017-05-10
          • 1970-01-01
          • 2014-08-24
          • 2020-02-11
          • 2020-05-21
          • 2019-12-21
          相关资源
          最近更新 更多