Skip to main content

A simple python library to authenticate users with github in Django applications.

Project description

https://img.shields.io/pypi/v/django_rest_github_oauth.svg https://img.shields.io/travis/mabdullahadeel/django-rest-github-oauth.svg Documentation Status Updates

A simple python library to authenticate users with github in Django applications.

Requirements

  • Python (3.7, 3.8, 3.9)

  • Django (2.x, 3.x, 4.x)

  • Django REST Framework (3.10, 3.11, 3.12)

Setup

Install the package in your python environment using pip.

pip install django-rest-github-oauth

For detailed information regarding the installation, see the installation guide.

Then your django project must be configured to use the library. For that include it in INSTALLED_APPS of your settings.py. Besides that, you will have to include the auth handler app in installed apps.

Currently, django-rest-github-oauth supports the following authentication backends:

Using JWT

INSTALLED_APPS = [
    # ...
    'rest_framework_simplejwt',
    'django_rest_github_oauth',
    # ...
]

Using Token Authentication

INSTALLED_APPS = [
    # ...
    'rest_framework.authtoken',
    'django_rest_github_oauth',
    # ...
]

Then add the following to your settings.py:

GITHUB_AUTH_KEY = "<your_github_app_key>"
GITHUB_AUTH_SECRET = "<your_github_app_secret>"
GITHUB_AUTH_USE_JWT = True    # False if you're using token based authentication

GITHUB_AUTH_CALLBACK_URL = "http://localhost:3000/auth/success/"    # url of the frontend handling redirects from github
GITHUB_AUTH_ALLOWED_REDIRECT_URIS = [
        GITHUB_AUTH_CALLBACK_URL
    ]

Then add the following to you main urls.py file.

urlpatterns = [
  # ...
  path('admin/', admin.site.urls),
  path('auth/github/', include('django_rest_github_oauth.urls')),
  # ...
]

Run migrations

python manage.py migrate

That’s all you have to do on the backend.

Usage

To get authorizaition_uri, make a GET request to the following url:

http://localhost:8000/auth/github?redirect_uri=http://localhost:3000/auth/success/

This will return a payload of the form

{
    "data": {
        "authorization_uri": "https://github.com/login/oauth/authorize?client_id=shlf898f7dsfsd0f90wer9fs&redirect_uri=http://localhost:3000/auth/success/&state=dac7944888d140e19280&response_type=code&scope=user:email,read:user"
         },
    "message": "success",
    "error": false
}

Redirect your user to authorization_uri.

Then, after the user has authorized your app, they will be redirected to the GITHUB_AUTH_CALLBACK_URL you specified with two query parameters:

  • code

  • state

In your frontend javascript, read those query parameters. Here is a quick snippet how you can achieve that.

const query = new URLSearchParams(window.location.search);
const code = query.get("code");
const state = query.get("state");

Then make a POST request to the following url with code and state in the request body:

http://localhost:8000/auth/github

The reuturn payload will have user informations and appropriate tokens.

Here is a snippet how you can make call using axios.

const query = new URLSearchParams(window.location.search);
const code = query.get("code");
const state = query.get("state");

const details = {
  code: code,
  state: state,
};

const url = "http://127.0.0.1:8000/auth/github/";

axios({
  method: "post",
  url: url,
  data: details,
  })
  .then((response) => {
    console.log(response)
    // login the user and save token for further request to the backend
  })
  .catch((err) => console.log(err));

History

0.1.2 (2022-02-26)

  • Added github_user_created signal.

0.1.1 (2022-02-04)

  • Minor updates.

    • No explicit makemigrations required.

0.1.0 (2022-02-02)

  • First release on PyPI.

    • Authentication with github.

    • Create user account with github.

    • Track state.

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-rest-github-oauth-0.1.2.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_rest_github_oauth-0.1.2-py2.py3-none-any.whl (13.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-rest-github-oauth-0.1.2.tar.gz.

File metadata

  • Download URL: django-rest-github-oauth-0.1.2.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for django-rest-github-oauth-0.1.2.tar.gz
Algorithm Hash digest
SHA256 673b74f2d82995b1b0dcc69d3a8529ac6407a51b5f9d0551d84a274dd3e409d6
MD5 14b2ea1911417b3684b0007f5aa72715
BLAKE2b-256 11ed15504d4f6c8c22d05cfd26fb3a33da8be71ba572a1d86990101b9eacd516

See more details on using hashes here.

File details

Details for the file django_rest_github_oauth-0.1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: django_rest_github_oauth-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for django_rest_github_oauth-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a6be0f4e14567f1fc4ca027926ef99e68d4da5f4ffe85a11003f9a2d96b6c4f6
MD5 d93f4d94bee5aed25e2741881ab1fe15
BLAKE2b-256 c7d4c8169131b999f910cfb5cf2790076d09871d47862eba2b5381d56fd9d053

See more details on using hashes here.

Supported by

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