class BasicAuthMixin(object): def dispatch(self, request, *args, **kwargs): if 'HTTP_AUTHORIZATION' in request.META: auth = request.META['HTTP_AUTHORIZATION'].split() if len(auth) == 2: # NOTE: Support for only basic authentication if auth[0].lower() == "basic": uname, passwd = base64.b64decode(auth[1]).split(':') user = authenticate(username=uname, password=passwd) if user is not None: if user.is_active: login(request, user) request.user = user return super(BasicAuthMixin, self).dispatch( request, *args, **kwargs ) return HttpResponse('Unauthorized', status=401)