Skip to main content

A library for managing authentication in Django Rest Framework

Project description

PropelAuth Django Rest Framework Library

A Django Rest Framework library for managing authentication, backed by PropelAuth.

PropelAuth makes it easy to add authentication and authorization to your B2B/multi-tenant application.

Your frontend gets a beautiful, safe, and customizable login screen. Your backend gets easy authorization with just a few lines of code. You get an easy-to-use dashboard to config and manage everything.

Documentation

  • Full reference this library is here
  • Getting started guides for PropelAuth are here

Installation

pip install propelauth-django-rest-framework

Initialize

init_auth performs a one-time initialization of the library. It will verify your api_key is correct and fetch the metadata needed to verify access tokens in IsUserAuthenticated and AllowAny.

from propelauth_django_rest_framework import init_auth

auth = init_auth("YOUR_AUTH_URL", "YOUR_API_KEY")

Protect API Routes

Protecting an API route is as simple as adding a Django permission to the route.

None of the Django permissions make a external request to PropelAuth. They all are verified locally using the access token provided in the request, making it very fast.

IsUserAuthenticated

A Django permission that will verify the request was made by a valid user. If a valid access token is provided, it will set request.propelauth_user to be a User Class. If not, the request is rejected with a 401 status code. While not required, you can use the RequiredRequest Class to get full type support.

Function-based views

from propelauth_django_rest_framework import init_auth, RequiredRequest

auth = init_auth("YOUR_AUTH_URL", "YOUR_API_KEY")

@api_view(['GET'])
@permission_classes([auth.IsUserAuthenticated])
def whoami(request: RequiredRequest):
    return HttpResponse(request.propelauth_user.user_id)

Class-based views

class WhoAmIView(APIView):
    permission_classes = [auth.IsUserAuthenticated]

    def get(self, request: RequiredRequest):
        return HttpResponse(request.propelauth_user.user_id)

AllowAny

Similar to IsUserAuthenticated, except if an access token is missing or invalid, the request is allowed to continue, but request.propelauth_user will be None. While not required, you can use the OptionalRequest Class to get full type support.

from propelauth_django_rest_framework import OptionalRequest

class OptionalUserView(APIView):
    permission_classes = [auth.AllowAny]

    def get(self, request: OptionalRequest):
        if request.propelauth_user is None:
            return HttpResponse("none")
        return HttpResponse(request.propelauth_user.user_id)

Authorization / Organizations

You can also verify which organizations the user is in, and which roles and permissions they have in each organization all through the User Class.

Check Org Membership

Verify that the request was made by a valid user and that the user is a member of the specified organization. This can be done using the User Class.

from propelauth_django_rest_framework import RequiredRequest

@api_view(['GET'])
@permission_classes([auth.IsUserAuthenticated])
def org_membership(request: RequiredRequest, org_id):
    org = request.propelauth_user.get_org(org_id)
    if org is None:
        # return 403 error
    return Response(f"You are in org {org.org_name}")

Check Org Membership and Role

Similar to checking org membership, but will also verify that the user has a specific Role in the organization. This can be done using either the User or OrgMemberInfo classes.

A user has a Role within an organization. By default, the available roles are Owner, Admin, or Member, but these can be configured. These roles are also hierarchical, so Owner > Admin > Member.

## Assuming a Role structure of Owner => Admin => Member

@api_view(['GET'])
@permission_classes([auth.IsUserAuthenticated])
def org_owner(request: RequiredRequest, org_id):
    org = request.propelauth_user.get_org(org_id)
    if (org is None) or (org.user_is_role("Owner") == False):
        # return 403 error
    return Response(f"You are an Owner in org {org.org_name}")

Check Org Membership and Permission

Similar to checking org membership, but will also verify that the user has the specified permission in the organization. This can be done using either the User or OrgMemberInfo classes.

Permissions are arbitrary strings associated with a role. For example, can_view_billing, ProductA::CanCreate, and ReadOnly are all valid permissions. You can create these permissions in the PropelAuth dashboard.

@api_view(['GET'])
@permission_classes([auth.IsUserAuthenticated])
def org_billing(request: RequiredRequest, org_id):
    org = request.propelauth_user.get_org(org_id)
    if (org is None) or (org.user_has_permission("can_view_billing") == False):
        # return 403 error
    return Response(f"You can view billing information for org {org.org_name}")

Calling Backend APIs

You can also use the library to call the PropelAuth APIs directly, allowing you to fetch users, create orgs, and a lot more. See the API Reference for more information.

from propelauth_django_rest_framework import init_auth

auth = init_auth("YOUR_AUTH_URL", "YOUR_API_KEY")

magic_link = auth.create_magic_link(email="test@example.com")

Questions?

Feel free to reach out at support@propelauth.com

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

propelauth_django_rest_framework-4.4.0.tar.gz (53.6 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file propelauth_django_rest_framework-4.4.0.tar.gz.

File metadata

File hashes

Hashes for propelauth_django_rest_framework-4.4.0.tar.gz
Algorithm Hash digest
SHA256 5210fa45edf9befbefd8ebb8f5748babd18ebcc137913faefb61e593f5301109
MD5 8a78eaa2b82d9e211d7d10c7e7f8c263
BLAKE2b-256 a364ced2e45f0d4d896f93be5702951620d16d23b89864bf809c454a6bb282ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for propelauth_django_rest_framework-4.4.0.tar.gz:

Publisher: publish.yml on PropelAuth/propelauth-django-rest-framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file propelauth_django_rest_framework-4.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for propelauth_django_rest_framework-4.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 85e8e3ca4da0602bf29246791d5eb28bf814faec53af0cdc5f47a7ee61a7d4ef
MD5 a14da78511a6fa729cd523395f97825a
BLAKE2b-256 af5f9f900a8a6d74e69234dcbeba624f74f7bd64dad60303a2817300ee805e56

See more details on using hashes here.

Provenance

The following attestation bundles were made for propelauth_django_rest_framework-4.4.0-py3-none-any.whl:

Publisher: publish.yml on PropelAuth/propelauth-django-rest-framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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