Django Code Snippets

Powered by PythonAnywhere and Django 1.8.6

Django BasicAuthMixin()

Date Added: Nov. 11, 2015, 8:18 p.m.

Author: django-snippets

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)


Tags: cpython, ipython, mongo-db, pymongo, pyston, python

Return to List