Django Rest Framework Reference

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, AllowAny, and IsUserInOrg.

main.py

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.

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 object. If not, the request is rejected with a 401 status code.

from propelauth_django_rest_framework import init_auth

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

@api_view(['GET'])
@permission_classes([auth.IsUserAuthenticated])
def whoami(request):
    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.

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

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

request.propelauth_user

A per-request value that contains user information for the user making the request. It's set by one of IsUserAuthenticated, AllowAny, or IsUserInOrg.

This will always be set, unless you are using AllowAny and the request did not include an access token. See User for more information.


Authorization

IsUserInOrg

A Django permission that will verify that the request was made by a valid user and that the user is a member of the specified organization. This function will set the request.current_org object if the user is a member of the specified organization, otherwise it will return a 403 status code.

Typically, the organization id is passed in from the frontend as a path or query parameter. By default, it will look for org_id in the path parameters (by checking request.GET.get('org_id', '')), but you can override this by passing in a function to req_to_org_id.

@api_view(['GET'])
@permission_classes([auth.IsUserInOrg()])
def org_membership(request):
    message = f"You are in {request.propelauth_org.org_name}"
    return HttpResponse(message)

IsUserInOrgWithMinimumRole

Similar to IsUserInOrg, but will also verify that the user has at least the specified role in the organization.

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.IsUserInOrgWithMinimumRole("Admin")])
def admin_or_owner(request):
   message = f"You are an Admin or Owner of {request.propelauth_org.org_name}"
   return HttpResponse(message)

IsUserInOrgWithRole

Similar to IsUserInOrg, but will also verify that the user has the exact specified role in the organization.

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.IsUserInOrgWithRole("Admin")])
def admin_only(request):
   message = f"You are an Admin of {request.propelauth_org.org_name}"
   return HttpResponse(message)

IsUserInOrgWithPermission

Similar to IsUserInOrg, but will also verify that the user has the specified permission in the organization.

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.IsUserInOrgWithPermission("can_view_billing")])
def billing(request):
   message = f"You can view billing for {request.propelauth_org.org_name}"
   return HttpResponse(message)

IsUserInOrgWithAllPermissions

The batch version of IsUserInOrgWithAllPermissions, which will verify that the user has all of the specified permissions in the organization.

@api_view(['GET'])
@permission_classes([auth.IsUserInOrgWithAllPermissions(["can_view_billing", "can_view_orders"])])
def billing_and_orders(request):
   message = f"You can view billing and orders for {request.propelauth_org.org_name}"
   return HttpResponse(message)

request.propelauth_org

A per-request value that contains information about the organization the user is a member of. It's set by one of IsUserInOrg, IsUserInOrgWithMinimumRole, IsUserInOrgWithRole, IsUserInOrgWithPermission, or IsUserInOrgWithAllPermissions.

See OrgMemberInfo for more information.


User

The User object contains information about the user that made the request.

  • Name
    user_id
    Type
    string
    Description

    The unique id of the user.

  • Name
    org_id_to_org_member_info
    Type
    dict
    Description

    A dictionary mapping from organization id to OrgMemberInfo object.

  • Name
    email
    Type
    string
    Description

    The email of the user.

  • Name
    first_name
    Type
    string
    Description

    The first name of the user.

  • Name
    last_name
    Type
    string
    Description

    The last name of the user.

  • Name
    username
    Type
    string
    Description

    The username of the user.

  • Name
    properties
    Type
    dict
    Description

    A dictionary of custom properties associated with the user.

  • Name
    legacy_user_id
    Type
    string
    Description

    If the user was migrated using our Migration API, this will be the id of the user in the legacy system.

  • Name
    impersonator_user_id
    Type
    string
    Description

    If the user is being impersonated, this is id of the user that impersonated them.

  • Name
    active_org_id
    Type
    string | undefined
    Description

    Returns the ID of the Active Org, if the user has an Active Org set.

  • Name
    login_method
    Type
    object
    Description

    The method the user used to log in. Returns the Login Method Property.

  • Name
    is_impersonated()
    Type
    bool
    Description

    True if the user is being impersonated.

  • Name
    get_active_org()
    Type
    dict
    Description

    Returns the OrgMemberInfo of the Active Org, if the user has an Active Org set.

  • Name
    get_active_org_id()
    Type
    string
    Description

    Returns the ID of the Active Org, if the user has an Active Org set.

  • Name
    get_org(org_id)
    Type
    dict
    Description

    Returns the org member info for the org_id, if the user is in the org.

  • Name
    get_org_by_name(org_name)
    Type
    dict
    Description

    Returns the org member info for the org_name, if the user is in the org.

  • Name
    get_user_property(property_name)
    Description

    Returns the user property value, if it exists.

  • Name
    get_orgs()
    Type
    array
    Description

    Returns the orgs the user is in.

  • Name
    is_role_in_org(org_id, role)
    Type
    bool
    Description

    Returns true if the user is the role in the org.

  • Name
    is_at_least_role_in_org(org_id, role)
    Type
    bool
    Description

    Returns true if the user is at least the role in the org.

  • Name
    has_permission_in_org(org_id, permission)
    Type
    bool
    Description

    Returns true if the user has the permission in the org.

  • Name
    has_all_permissions_in_org(org_id, permissions)
    Type
    bool
    Description

    Returns true if the user has all the permissions in the org.


OrgMemberInfo

The OrgMemberInfo object contains information about the user's membership in an organization.

  • Name
    org_id
    Type
    string
    Description

    The unique id of the organization.

  • Name
    org_name
    Type
    string
    Description

    The name of the organization.

  • Name
    org_metadata
    Type
    object
    Description

    The metadata associated with the organization.

  • Name
    user_assigned_role
    Type
    string
    Description

    The role of the user in the organization.

  • Name
    user_inherited_roles_plus_current_role
    Type
    list[string]
    Description

    The role of the user within this organization plus each inherited role.

  • Name
    user_permissions
    Type
    list[string]
    Description

    A list of permissions the user has in the organization, based on their role.

  • Name
    user_is_role
    Type
    fn(role: string) -> bool
    Description

    A function that returns true if the user has the specified role in the organization.

  • Name
    user_is_at_least_role
    Type
    fn(role: string) -> bool
    Description

    A function that returns true if the user has at least the specified role in the organization.

  • Name
    user_has_permission
    Type
    fn(permission: string) -> bool
    Description

    A function that returns true if the user has the specified permission in the organization.

  • Name
    user_has_all_permissions
    Type
    fn(permissions: list[string]) -> bool
    Description

    A function that returns true if the user has all of the specified permissions in the organization.

  • Name
    org_role_structure
    Type
    string
    Description

    The role structure set for your project. See single and multi role per user for more information.

  • Name
    assigned_additional_roles
    Type
    list[string]
    Description

    If using multiple roles per user, returns an array of roles that the user belongs to. Excludes the user_assigned_role.


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.