Django REST Framework (DRF) SDK for Axioms
Project description
axioms-drf-py
Axioms Python client for Django Rest Framework (DRF). Secure your DRF APIs using Axioms Authentication and Authorization.
Prerequisite
- Python 3.7+
- An Axioms client which can obtain access token after user's authentication and authorization and include in
Authorization
header of all API request sent to Python/Flask application server.
Install SDK
Install axioms-drf-py
in you DRF API project,
pip install axioms-drf-py
Basic usage
Add .env
file
Create a .env
file in your main Django app and add following configs,
AXIOMS_DOMAIN=<your-axioms-slug>.axioms.io
AXIOMS_AUDIENCE=<your-axioms-resource-identifier>
URL_LIB_SSL_IGNORE=True
Load Config
In your Django project settings.py
,
import os
import environ
env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
)
# reading .env file
environ.Env.read_env()
AXIOMS_DOMAIN=env('AXIOMS_DOMAIN')
AXIOMS_AUDIENCE=env('AXIOMS_AUDIENCE')
URL_LIB_SSL_IGNORE=env('URL_LIB_SSL_IGNORE')
Guard API Views
Use authentication and permission classes to guard you API views.
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from axioms_drf.authentication import AccessTokenAuthentication
from axioms_drf.permissions import AccessScopePermission
class Private(APIView):
authentication_classes = [AccessTokenAuthentication]
permission_classes = (AccessScopePermission,)
access_token_scopes = ['openid', 'profile'] # noqa
def get(self, request, format=None):
return Response({'message': 'All good. You are authenticated!'}, status=status.HTTP_200_OK)
Authentication classes
AccessTokenAuthentication
AccessTokenAuthentication
checks if access token passed in Authorization header in API call is valid or not. It check signature, time, and audience validity using public for your tenant.- Once access token is verified user is authenticated and claims in tokens can be used to perform permission, role, scope checks.
Permission classes
AccessScopePermission
AccessScopePermission
requires additional attributeaccess_token_scopes
on view class.access_token_scopes
accepts an array of strings asconditional OR
where each string represent a scope (role or permission).- If any scope provided in
access_token_scopes
is matched with one in access token, SDK will allow access (hence why scopes areconditional OR
)
For more details please check our sample-python-drf
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
axioms-drf-py-0.0.1.tar.gz
(7.8 kB
view hashes)
Built Distribution
Close
Hashes for axioms_drf_py-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fb60b10a707088a50ecc405be48c3104494ae8947b80867385ca6755966bf43 |
|
MD5 | 33a5a11334252d50fbab2d29897bace6 |
|
BLAKE2b-256 | 51336fe42f1377b84d4d8debeb17020b78bc007f14352bb56db1392581f8768e |