Skip to main content

Build pypi versions license codecov

Authentication and authorization framework for Python apps

Basic framework to handle authentication and authorization in asynchronous Python applications.

Features:

  • strategy to implement authentication (who or what is using a service?)
  • strategy to implement authorization (is the acting identity authorized to do a certain action?)
  • support for dependency injection for classes handling authentication and authorization requirements
  • built-in support for JSON Web Tokens (JWTs) authentication

This library is freely inspired by authorization in ASP.NET Core; although its implementation is extremely different.

Installation

pip install guardpost

To install with support for JSON Web Tokens (JWTs) validation:

pip install guardpost[jwt]

Examples

For examples, refer to the examples folder.

Functions to validate JWTs

GuardPost includes functions to validate JSON Web Tokens (JWTs) and handle JSON Web Keys Sets (JWKS).

The built-in validator class can retrieve automatically JWKS from identity providers and handle automatically caching and keys rotation. Caching is useful to not incur in useless performance fees (e.g. downloading JWKS at each web request), and keys rotation is important because identity providers can periodically change the keys they use to sign JWTs.

To use these features, install to include additional dependencies:

pip install guardpost[jwt]

The following example shows how to use guardpost to validate tokens:

import asyncio
from guardpost.jwts import JWTValidator


async def main():
    validator = JWTValidator(
        authority="YOUR_AUTHORITY",
        valid_issuers=["YOUR_ISSUER_VALUE"],
        valid_audiences=["YOUR_AUDIENCE"],
    )

    # keys are fetched when necessary
    data = await validator.validate_jwt("YOUR_TOKEN")

    print(data)


asyncio.run(main())

An example value for authority, to validate access tokens issued by Entra ID could be: https://sts.windows.net/YOUR_TENANT_ID/.

GuardPost is used in BlackSheep and has been tested with:

  • Auth0
  • Entra ID
  • Azure Active Directory B2C
  • Okta

If you have doubts about authentication vs authorization...

Authentication answers the question: Who is the user who is initiating the action?, or more in general: Who is the user, or what is the service, that is initiating the action?.

Authorization answers the question: Is the user, or service, authorized to do something?.

Usually, to implement authorization, is necessary to have the context of the entity that is executing the action.

Usage in BlackSheep

guardpost is used in the BlackSheep web framework, to implement authentication and authorization strategies for request handlers.

To see how guardpost is used in blacksheep web framework, read:

Documentation

The documentation is available at https://www.neoteroi.dev/guardpost/.

Metadata

Release files for guardpost 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for guardpost 1.1.0
File Size Uploaded
guardpost-1.1.0.tar.gz 19.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for guardpost 1.1.0
File Interpreter ABI Platform
guardpost-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 42.3 kB

Release files / guardpost-1.1.0.tar.gz

Download URL guardpost-1.1.0.tar.gz
Size 19.3 kB
Tags Source
SHA-256 checksum
How to use checksums
17f200cca570484e18e23a2aadcc9b6ded31c51672b52999f278901d4f6fe56c
BLAKE2b-256 checksum
How to use checksums
0273f396968fe9e40753cb87d364dfe67651d72aaeb293f35f36a20a2dac1435
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / guardpost-1.1.0-py3-none-any.whl

Download URL guardpost-1.1.0-py3-none-any.whl
Size 23.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dc003dc11e6bfd0d858fac2a255b850365611835838b44497e51faed04c96d74
BLAKE2b-256 checksum
How to use checksums
cd34f5c334b0c74928b5e00ef081ebb1e5a71653d19f19a09f5e1c78c0954377
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page