Skip to main content

Django Auth0 authentication background

Project description

https://codecov.io/gh/imanhodjaev/django-auth0/branch/master/graph/badge.svg https://travis-ci.org/imanhodjaev/django-auth0.png?branch=master Code Health

Django Auth0 authentication backend

Quickstart

  • Install $ pip install django-auth0

  • Add django_auth0 to INSTALLED_APPS

  • Add django_auth0.auth_backend.Auth0Backend to AUTHENTICATION_BACKENDS

AUTHENTICATION_BACKENDS = [
    "django_auth0.auth_backend.Auth0Backend",
    "django.contrib.auth.backends.ModelBackend"
]
  • Add django_auth0.context_processors.auth0 to CONTEXT_PROCESSORS so necessary template context will be provided

  • Include callback urls

Update AUTH0_CALLBACK_URL in settings.py to the following if want to use default authentication handler

  • Add Auth0 client side JavaScript and initialize it

Options:

  1. AUTH0_CLIENT_ID - Auth0 client app id,

  2. AUTH0_SECRET - Auth0 app secret,

  3. AUTH0_DOMAIN - Auth0 subdomain YOU_APP.auth0.com.

  4. AUTH0_CALLBACK_URL - Auth0 callback url is full url to your callback view like https://YOUR_DOMAIN/CALLBACK

  5. AUTH0_SUCCESS_URL - Url to redirect once you login successfully

Overriding callback view Default callback view looks like this so you can always write your own and set AUTH0_CALLBACK_URL to your custom view it should be url name.

def process_login(request):
    """
    Default handler to login user
    :param request: HttpRequest
    """
    code = request.GET.get('code', '')
    json_header = {'content-type': 'application/json'}
    token_url = 'https://%s/oauth/token' % settings.AUTH0_DOMAIN

    token_payload = {
        'client_id': settings.AUTH0_CLIENT_ID,
        'client_secret': settings.AUTH0_SECRET,
        'redirect_uri': reverse(settings.AUTH0_CALLBACK_URL),
        'code': code,
        'grant_type': 'authorization_code'
    }

    token_info = requests.post(token_url,
                               data=json.dumps(token_payload),
                               headers=json_header).json()

    url = 'https://%s/userinfo?access_token=%s'
    user_url = url % (settings.AUTH0_DOMAIN, token_info['access_token'])
    user_info = requests.get(user_url).json()

    # We're saving all user information into the session
    request.session['profile'] = user_info
    user = authenticate(**user_info)

    if user:
        login(request, user)
        return redirect(settings.AUTH0_SUCCESS_URL)

    return HttpResponse(status=400)

Sample application at https://github.com/imanhodjaev/auth0-sample

TODO

  • Improve tests

  • Add Auth0 user profile model

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements-test.txt
(myenv) $ python runtests.py

Credits

Tools used in rendering this package:

History

0.0.2 (2016-02-01)

  • Update documentation

0.0.1 (2016-02-01)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-auth0-0.0.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

django_auth0-0.0.2-py2.py3-none-any.whl (9.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-auth0-0.0.2.tar.gz.

File metadata

File hashes

Hashes for django-auth0-0.0.2.tar.gz
Algorithm Hash digest
SHA256 5801d492c1f0830279303028401e739d6ba3017afc3058021c449b2d1851be82
MD5 24a5e41785a486dc3f051e050b7b1f86
BLAKE2b-256 98b669380336e1217a3557733b3203285a01bc22bbb30d584f780932c66ae77f

See more details on using hashes here.

File details

Details for the file django_auth0-0.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_auth0-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 50d294ad1be3b2f7b4c22a11d28bb942697acf61e3f21f4a7c48c1f1a856511e
MD5 a3711a0acd69fbe16f6d11ff24d52776
BLAKE2b-256 2dbdd9b49354f38712bba12a2f5b687741f3204c17745da5410d527ff4820426

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page