Django:一个视图的基本身份验证(避免中间件)

Django:一个视图的基本身份验证(避免中间件)

问题描述:

我需要为一个视图提供http-basic-authDjango:一个视图的基本身份验证(避免中间件)

我想避免修改中间件设置。

背景:这是一个由远程应用程序填充的视图。

该库可用于:https://github.com/hirokiky/django-basicauth

基本身份验证工具,用于Django的。

该文档展示了如何使用它:

应用装饰以CBVS

申请@basic_auth_requried装饰器基于类的意见,使用 django.utils.decorators.method_decorator。

来源:https://github.com/hirokiky/django-basicauth#applying-decorator-to-cbvs

当您执行基本身份验证请求时,您确实将凭据添加到Authorization标头中。在传输之前,这些证书是base64编码的,所以您需要在收到时解码它们。

下面的代码片段假定只有一个有效的用户名和密码:

import base64 

def my_view(request): 
    auth_header = request.META.get('HTTP_AUTHORIZATION', '') 
    token_type, _, credentials = auth_header.partition(' ') 

    expected = base64.b64encode(b'username:password').decode() 

    if token_type != 'Basic' or credentials != expected: 
     return HttpResponse(status=401) 

    # Your authenticated code here: 
    ... 

如果要比较的User模型的用户名和密码,尽量不要使用以下:

def my_view(request): 
    auth_header = request.META.get('HTTP_AUTHORIZATION', '') 
    token_type, _, credentials = auth_header.partition(' ') 

    username, password = base64.b64decode(credentials).split(':') 
    try: 
     user = User.objects.get(username=username) 
    except User.DoesNotExist: 
     return HttpResponse(status=401) 

    password_valid = user.check_password(password) 

    if token_type != 'Basic' or not password_valid: 
     return HttpResponse(status=401) 

    # Your authenticated code here: 
    ... 

请注意,后面的版本不是非常安全。乍一看,例如,我可以看到它容易受到timing attacks的影响。

+0

是的,这应该工作。但不知何故,如果我像这样解决问题,我认为自己在错误的轨道上。我有个人指导方针(对我来说)。一种是“不要写源代码来实现备份”,而下一个“不要写源代码来实现认证”。这已经被更多有才华的人解决了,并且已经有了几个月的测试。尽管如此,谢谢你的这个片段。我虽然实现这个是更多的代码。 – guettli

你可以尝试定制的装饰(似乎是推荐的方法herehere)而不是增加新的中间件:

my_app/decorators.py

import base64 

from django.http import HttpResponse 
from django.contrib.auth import authenticate 
from django.conf import settings 


def basicauth(function): 
    def wrap(request, *args, **kwargs): 
     if 'HTTP_AUTHORIZATION' in request.META: 
      auth = request.META['HTTP_AUTHORIZATION'].split() 
      if len(auth) == 2: 
       if auth[0].lower() == "basic": 
        uname, passwd = base64.b64decode(auth[1]).split(':') 
        user = authenticate(username=uname, password=passwd) 
        if user is not None and user.is_active: 
         request.user = user 
         return view(request, *args, **kwargs) 

     response = HttpResponse() 
     response.status_code = 401 
     response['WWW-Authenticate'] = 'Basic realm="{}"'.format(
      settings.BASIC_AUTH_REALM 
     ) 
     return response 

然后用它来装饰您的视图:

from my_app.decorators import basicauth 


@basicauth 
def my_view(request): 
    ...