Metadata-Version: 2.1
Name: django-database-postgresql-aad-backend
Version: 0.0.1
Summary: Django database backend for Azure Postgresql that adds AAD authentication
Home-page: https://github.com/younux/django-database-postgresql-aad-backend
Author: Younes
Author-email: b.younes.h@gmail.com
License: MIT
Download-URL: https://github.com/younux/django-database-postgresql-aad-backend/releases/download/v0.0.1/django_database_postgresql_aad_backend-v0.0.1.tar.gz
Keywords: Django,Database,Backend,Postgresql,Azure,AAD
Platform: UNKNOWN
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Build Tools
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Description-Content-Type: text/markdown
Requires-Dist: asgiref (==3.3.1)
Requires-Dist: azure-core (==1.10.0)
Requires-Dist: azure-identity (==1.5.0)
Requires-Dist: certifi (==2020.12.5)
Requires-Dist: cffi (==1.14.4)
Requires-Dist: chardet (==4.0.0)
Requires-Dist: cryptography (==3.3.1)
Requires-Dist: Django (==3.1.5)
Requires-Dist: idna (==2.10)
Requires-Dist: msal (==1.8.0)
Requires-Dist: msal-extensions (==0.3.0)
Requires-Dist: portalocker (==1.7.1)
Requires-Dist: psycopg2-binary (==2.8.6)
Requires-Dist: pycparser (==2.20)
Requires-Dist: PyJWT (==1.7.1)
Requires-Dist: pytz (==2020.5)
Requires-Dist: requests (==2.25.1)
Requires-Dist: six (==1.15.0)
Requires-Dist: sqlparse (==0.4.1)
Requires-Dist: urllib3 (==1.26.2)

# django_database_postgresql_aad_backend


django-database-postgresql-aad-backend is a django database backend that adds Azure active directory authentication for 
your Azure postgresql server. It uses the authentication as described in Azure documentation : 
[https://docs.microsoft.com/en-us/azure/postgresql/howto-configure-sign-in-aad-authentication](https://docs.microsoft.com/en-us/azure/postgresql/howto-configure-sign-in-aad-authentication).

This backend is based on Django postgresql backend (which it inherits from all its features) and adds AAD options in 
database settings as described bellow :


    DATABASES = {
        "default": {
            "ENGINE": "django_database_postgresql_aad_backend",
            "NAME": os.getenv("DB_NAME"),
            "HOST": os.getenv("DB_HOST"),
            "PORT": os.getenv("DB_PORT"),
            "USER": os.getenv("DB_USERNAME"),
            "PASSWORD": "",
            "OPTIONS": {
                "sslmode": "require"
            },
            "AAD_OPTIONS": {
                "SP_CLIENT_ID": os.getenv("DB_SP_CLIENT_ID"),
                "`SP_CLIENT_SECRET`": os.getenv("DB_SP_CLIENT_SECRET"),
                "`SP_TENANT_ID`": os.getenv("DB_SP_TENANT_ID"),
            }
        }
    }

To enable Azure Active directory authentication you need to :

   - Install `django_database_postgresql_aad_backend` using pip : 

    pip install django_database_postgresql_aad_backend

   - Add the backend to installed apps in `setting.py` 

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'rest_framework',
        'django_database_postgresql_aad_backend',
    ]

   - Provide `AAD_OPTIONS` dictionary inside database settings with the following keys : 

      - `SP_CLIENT_ID` (Required) The client ID of the service principal used to authenticate to Azure Active directory.
      - `SP_CLIENT_SECRET`: (Required) The client secret of the service principal used to authenticate to Azure active directory.
      - `SP_TENANT_ID`: (Required) The tenant ID of the service principal.
      - `REFRESH_BEFORE_MINUTES`: (Optional) This is used to specify how much time (in minutes) the token should be valid.
            Default value is 5.

If you don't provide `AAD_OPTIONS` dictionary, Azure active directory authentication will be disabled and the backend
will use the default user/password authentication method.


## Tests 

To run tests : 

    python3 -m venv venv
    source venv/bin/activate
    pip3 install -r requirements.txt
    coverage run --source django_database_postgresql_aad_backend -m unittest discover 
    coverage report

## Building package

To build package : 

    python3 -m venv venv
    source venv/bin/activate
    pip3 install -r requirements.txt
    python3 setup.py sdist bdist_wheel

## Publish package 

    twine upload --repository-url https://test.pypi.org/legacy/ dist/*


## Improvements

 - The token is fetched and stored in memory. An improvement would be to support a caching system to store token 
 and share it between multiple instances of the backend.
 - Add more test cases !


