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
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file propelauth_py-4.3.0.tar.gz.
File metadata
- Download URL: propelauth_py-4.3.0.tar.gz
- Upload date:
- Size: 37.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8802fe9deba2bc4329201863a0f7533ad8b22e7ce46736b10101cfa5dab741c1
|
|
| MD5 |
a10b0b8bf0fbe60c16d9802ea2165200
|
|
| BLAKE2b-256 |
d9fe488bfad309b1e418200df1993fcd056f0c6f8326fde2be1949cfe6cd5588
|