Skip to main content

A library for managing authentication in Flask

Project description

PropelAuth Flask SDK

A Flask 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_flask

Initialize

init_auth performs a one-time initialization of the library. This verifies your api_key and fetches the metadata needed to verify access tokens in require_user and optional_user.

from propelauth_flask 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 decorator to the route.

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

require_user

A decorator that will verify the request was made by a valid user. If a valid access token is provided, it will return a User Class. If not, the request is rejected with a 401 status code.

from flask import Flask
from propelauth_flask import init_auth, current_user

app = Flask(__name__)
auth = init_auth("YOUR_AUTH_URL", "YOUR_API_KEY")

@app.route("/api/whoami")
@auth.require_user
def who_am_i():
    """This route is protected, current_user is always set"""
    return {"user_id": current_user.user_id}

optional_user

Similar to require_user, except if an access token is missing or invalid, the request is allowed to continue, but current_user.exists() will be False.

from flask import Flask
from propelauth_flask import init_auth, current_user

app = Flask(__name__)
auth = init_auth("YOUR_AUTH_URL", "YOUR_API_KEY")

@app.route("/api/whoami_optional")
@auth.optional_user
def who_am_i_optional():
    if current_user.exists():
        return {"user_id": current_user.user_id}
    return {}

current_user

A per-request value that contains user information for the user making the request. It's set by one of require_user or optional_user.

It has all the fields on the User class, as well as an exists() method that returns True if the user exists. The only time exists() will return False is if you are using optional_user and no valid access token was provided.

If you want to take advantage of type support, you can import the User class to define a new user variable.

from flask import Flask
from propelauth_flask import init_auth, current_user, User

app = Flask(__name__)
auth = init_auth("YOUR_AUTH_URL", "YOUR_API_KEY")

@app.route("/api/whoami")
@auth.require_user
def who_am_i():
    user: User = current_user.user
    return {"user_id": 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.

@app.route("/api/org/<org_id>", methods=['GET'])
@auth.require_user
def org_membership(org_id):
    org = current_user.get_org(org_id)
    if org == None:
        # Return a 403 error, e.g.: return "Forbidden", 403
    return 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

@app.route("/api/org/<org_id>", methods=['GET'])
@auth.require_user
def org_owner(org_id):
    org = current_user.get_org(org_id)
    if (org == None) or (org.user_is_role("Owner") == False):
        # return 403 error
    return f"You are 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.

@app.route("/api/org/<org_id>", methods=['GET'])
@auth.require_user
def org_billing(org_id):
    org = current_user.get_org(org_id)
    if (org == None) or (org.user_has_permission("can_view_billing") == False):
        # return 403 error
    return 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_flask 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_flask-4.4.0.tar.gz (63.6 kB view details)

Uploaded Source

Built Distribution

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

propelauth_flask-4.4.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for propelauth_flask-4.4.0.tar.gz
Algorithm Hash digest
SHA256 d83ccd4a413da2c9dba5cdf8d67e7ca068d49d6785d06e77faa6ea26b670bcb6
MD5 bc9a9e6379aa02c7ed625cc23087f181
BLAKE2b-256 4c50695710a3ec6cee6d47fb638298fd2140254feb2de6acd0528a717ff58012

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on PropelAuth/propelauth-flask

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

File metadata

File hashes

Hashes for propelauth_flask-4.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1bd76aa1ecb0aad769dac91b3e6ff79346d0559c67af8ab5d1165306c55db464
MD5 f39ceed24f535ceb974d24ca831242b0
BLAKE2b-256 d4f86aaf4afdc44a92fa74652fd14b0762cce2a3eff4bfe7e6f3f0d548e42919

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on PropelAuth/propelauth-flask

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