Skip to main content

A python authentication library

Project description

PropelAuth Python SDK

A python 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_py

Initialize

init_base_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 with validate_access_token_and_get_user.

from propelauth_py import init_base_auth

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

Protect API Routes

After initializing auth, you can verify access tokens by passing in the Authorization header (formatted Bearer TOKEN) to validate_access_token_and_get_user. You can see more information about the User object returned in User.

auth_header = # get authorization header in the form `Bearer {TOKEN}`
try:
   user = auth.validate_access_token_and_get_user(auth_header)
   print("Logged in as", user.user_id)
except UnauthorizedException:
   print("Invalid access token")

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 object.

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 object.

auth_header = # get authorization header in the form `Bearer {TOKEN}`
org_id = # get org id from request
try:
    user = auth.validate_access_token_and_get_user(auth_header)
    org = user.get_org(org_id)
    if org is None:
        # return 403 error
    print(f"You are in org {org.org_id}")
except UnauthorizedException:
    print("Invalid access token")

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 objects.

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

auth_header = # get authorization header in the form `Bearer {TOKEN}`
org_id = # get org id from request
try:
    user = auth.validate_access_token_and_get_user(auth_header)
    org = user.get_org(org_id)
    if (org is None) or (org.user_is_role("Owner") == False):
        # return 403 error
    print(f"You are an Owner in org {org.org_id}")
except UnauthorizedException:
    print("Invalid access token")

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 objects.

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.

auth_header = # get authorization header in the form `Bearer {TOKEN}`
org_id = # get org id from request
try:
    user = auth.validate_access_token_and_get_user(auth_header)
    org = user.get_org(org_id)
    if (org is None) or (org.user_has_permission("can_view_billing") == False):
        # return 403 error
    print(f"You can view billing information for org {org.org_id}")
except UnauthorizedException:
    print("Invalid access token")

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_py import init_base_auth

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

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

Logging Configuration

You can configure the logging behavior of the PropelAuth library to control whether exceptions are logged:

# When initializing the library:
auth = init_base_auth("YOUR_AUTH_URL", "YOUR_API_KEY", log_exceptions=False)

# Or after initialization:
auth.configure_logging(log_exceptions=False)

# Or without an Auth instance:
from propelauth_py import configure_logging
configure_logging(log_exceptions=False)

By default, exceptions are logged using Python's standard logging module. Setting log_exceptions=False will disable this behavior.

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_py-4.4.0.tar.gz (79.5 kB view details)

Uploaded Source

Built Distribution

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

propelauth_py-4.4.0-py3-none-any.whl (50.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: propelauth_py-4.4.0.tar.gz
  • Upload date:
  • Size: 79.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for propelauth_py-4.4.0.tar.gz
Algorithm Hash digest
SHA256 0767ff4765db9eeb71a4097f3e92a890e6cd53422fcfa813e0ed03a8e048b5b4
MD5 06b9352a1b603c80dc6951218d5dcf07
BLAKE2b-256 3490d60021e27dc545624a5bd9b1e251f698d422e802b7ed7a5ff87e21cfa571

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on PropelAuth/propelauth-py

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_py-4.4.0-py3-none-any.whl.

File metadata

  • Download URL: propelauth_py-4.4.0-py3-none-any.whl
  • Upload date:
  • Size: 50.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for propelauth_py-4.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 becad678dc9b42a3df3332dec692de93723ec9076fd8fffac2a0265b51577bae
MD5 670522e96a454ca13f863df1643c90e8
BLAKE2b-256 e710b330df852fc6776bb701218ed416a4a8aebf4308c521060568746d5ca018

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on PropelAuth/propelauth-py

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