Framework to handle authentication and authorization.
Project description
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
Azure Active Directory could be: https://sts.windows.net/YOUR_TENANT_ID/
.
GuardPost is used in BlackSheep and has been tested with:
- Auth0
- Azure Active Directory
- 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
Under construction. 🚧
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
Built Distribution
File details
Details for the file guardpost-1.0.2.tar.gz
.
File metadata
- Download URL: guardpost-1.0.2.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4566616c1bc01c148275ed8d1cd56f7dffeced490c7d8c599c90293308e55c94 |
|
MD5 | 54782dc71c910b73e61dbeedaec799a0 |
|
BLAKE2b-256 | dca8f90fe39e852c1645bb5a844d28c7833e0904b4451dbc0106b3449e8ff704 |
File details
Details for the file guardpost-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: guardpost-1.0.2-py3-none-any.whl
- Upload date:
- Size: 16.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7b7e5c61f776b055c7f4ee382ab78149bfe66f367f0e187574c3664e1740579 |
|
MD5 | a4540fd4a0b99d33e18deb9110a45b8c |
|
BLAKE2b-256 | aacdb3574737b27491f26d1058806d12ee7131e6e8476990961a2ea80512c313 |