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 =
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 =
org_id =
try:
user = auth.validate_access_token_and_get_user(auth_header)
org = user.get_org(org_id)
if org is None:
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.
auth_header =
org_id =
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):
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 =
org_id =
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):
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:
auth = init_base_auth("YOUR_AUTH_URL", "YOUR_API_KEY", log_exceptions=False)
auth.configure_logging(log_exceptions=False)
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