Welcome to Django Rest Framework MSAL + JWT’s documentation!

Contents:

Django Rest Framework MSAL + JWT

https://badge.fury.io/py/drf-msal-jwt.svg https://travis-ci.org/narongdejsrn/drf-msal-jwt.svg?branch=master https://codecov.io/gh/narongdejsrn/drf-msal-jwt/branch/master/graph/badge.svg

This package allows user to authenticate using Microsoft Account in Django REST Framework.

This library rely on Django REST Framework and Django Rest Framework JWT to works properly. Please make sure you setup these packages successfully before using this package.

Documentation

The full documentation is at https://drf-msal-jwt.readthedocs.io.

Quickstart

Install Django Rest Framework MSAL + JWT:

pip install drf-msal-jwt

Add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'drf_msal_jwt',
    ...
)

Add Django Rest Framework MSAL + JWT’s URL patterns:

from drf_msal_jwt import urls as drf_msal_jwt_urls


urlpatterns = [
    ...
    url(r'^', include(drf_msal_jwt_urls)),
    ...
]

Config the settings.py

DEFAULTS = {
    'MSAL_CLIENT_ID': "{AZURE_AD_CLIENT_ID}",
    'MSAL_CLIENT_SECRET': "{AZURE_AD_CLIENT_SECRET}",
    'MSAL_AUTHORITY_URL': 'https://login.microsoftonline.com/common/',
    'MSAL_REDIRECT_URL': "{AZURE_AD_REDIRECT_URL}",
    'MSAL_SCOPES': ["User.ReadBasic.All"],
    'MSAL_USER_HANDLER': 'django.contrib.auth.models.User',
    'MSAL_ALLOW_DOMAINS': ['*'],
    'MSAL_CHECK_STATE': True
}

Features

  • [API] for generating Microsoft Login URL
  • [API] for logging/creating user based on Authorization Code, and generate JWT token

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

Deployment

Bumb version & deploy

bumpversion <major/minor/patch>
python setup.py publish

Installation

At the command line:

$ easy_install drf-msal-jwt

Or, if you have virtualenvwrapper installed:

$ mkvirtualenv drf-msal-jwt
$ pip install drf-msal-jwt

Usage

This library rely on Django REST Framework and Django Rest Framework JWT to works properly. Please make sure you setup these packages successfully before using this package.

To use Django Rest Framework MSAL + JWT in a project, add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'drf_msal_jwt',
    ...
)

Before using this package, you’ll need to register your application with the Microsoft Identity Platform

If you already register then config the settings.py in your app accordingly

DEFAULTS = {
    'MSAL_CLIENT_ID': "{AZURE_AD_CLIENT_ID}",
    'MSAL_CLIENT_SECRET': "{AZURE_AD_CLIENT_SECRET}",
    'MSAL_REDIRECT_URL': "{AZURE_AD_REDIRECT_URL}",
    'MSAL_AUTHORITY_URL': 'https://login.microsoftonline.com/common/',
    'MSAL_SCOPES': ["User.ReadBasic.All"],
    'MSAL_USER_HANDLER': 'django.contrib.auth.models.User',
    'MSAL_ALLOW_DOMAINS': ['*'],
    'MSAL_CHECK_STATE': True
}
  • MSAL_CLIENT_ID: Your app Client ID

  • MSAL_CLIENT_SECRET: Your app Client Secret

  • MSAL_REDIRECT_URL: Redirect URL when the user logged in successfully

  • MSAL_AUTHORITY_URL (optional): Your app authority URL

  • MSAL_SCOPES (optional): scope that you request from the user (User.ReadBasic.All) currently required

  • MSAL_ALLOW_DOMAINS (optional): Limit the domain that the user can use to sign in, * to allows all domains

  • MSAL_CHECK_STATE (optional): Check state using session

    Be sure configured permission in azure portal according to permission request in MSAL_SCOPES variable.

Add Django Rest Framework MSAL + JWT’s URL patterns:

from drf_msal_jwt import urls as drf_msal_jwt_urls


urlpatterns = [
    ...
    url(r'^', include(drf_msal_jwt_urls)),
    ...
]

Finally, your client can connect to the backend using these url

Get Login URL

GET /msal/login_urls?state={state_uuid}

Retrieve a login url

If state_uuid is set, then it will generate login_url with given state instead. You can leave it blank, and the app will generate a random uuid

Example response:

{
    "login_url": "https://login.microsoftonline.com/...."
}

Login using Code

POST /msal/login_with_code

Login using authorization code from callback page

The content of body

{
    "code": "code from callback url params",
    "state": "state from callback url params"
}

Example response:

{
    "token": "JWT_token"
}

Contributing

Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.

You can contribute in many ways:

Types of Contributions

Report Bugs

Report bugs at https://github.com/narongdejsrn/drf-msal-jwt/issues.

If you are reporting a bug, please include:

  • Your operating system name and version.
  • Any details about your local setup that might be helpful in troubleshooting.
  • Detailed steps to reproduce the bug.

Fix Bugs

Look through the GitHub issues for bugs. Anything tagged with “bug” is open to whoever wants to implement it.

Implement Features

Look through the GitHub issues for features. Anything tagged with “feature” is open to whoever wants to implement it.

Write Documentation

Django Rest Framework MSAL + JWT could always use more documentation, whether as part of the official Django Rest Framework MSAL + JWT docs, in docstrings, or even on the web in blog posts, articles, and such.

Submit Feedback

The best way to send feedback is to file an issue at https://github.com/narongdejsrn/drf-msal-jwt/issues.

If you are proposing a feature:

  • Explain in detail how it would work.
  • Keep the scope as narrow as possible, to make it easier to implement.
  • Remember that this is a volunteer-driven project, and that contributions are welcome :)

Get Started!

Ready to contribute? Here’s how to set up drf-msal-jwt for local development.

  1. Fork the drf-msal-jwt repo on GitHub.

  2. Clone your fork locally:

    $ git clone git@github.com:your_name_here/drf-msal-jwt.git
    
  3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:

    $ mkvirtualenv drf-msal-jwt
    $ cd drf-msal-jwt/
    $ python setup.py develop
    
  4. Create a branch for local development:

    $ git checkout -b name-of-your-bugfix-or-feature
    

    Now you can make your changes locally.

  5. When you’re done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:

    $ flake8 drf_msal_jwt tests
    $ python setup.py test
    $ tox
    

    To get flake8 and tox, just pip install them into your virtualenv.

  6. Commit your changes and push your branch to GitHub:

    $ git add .
    $ git commit -m "Your detailed description of your changes."
    $ git push origin name-of-your-bugfix-or-feature
    
  7. Submit a pull request through the GitHub website.

Pull Request Guidelines

Before you submit a pull request, check that it meets these guidelines:

  1. The pull request should include tests.
  2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst.
  3. The pull request should work for Python 3+, and for PyPy. Check https://travis-ci.org/narongdejsrn/drf-msal-jwt/pull_requests and make sure that the tests pass for all supported Python versions.

Tips

To run a subset of tests:

$ python -m unittest tests.test_drf_msal_jwt

Credits

Development Lead

Contributors

None yet. Why not be the first?