Reusable code for authentication in Python backends using FastAPI
Project description
SMAC: FastAPI Auth
This project contains the reusable code for authentication in Python backends using FastAPI. It leverages Cognito User Pools and Secrets Manager for authentication.
Usage
TBD
Authentication of Service Principals (Automation)
The authentication of service principals is done leveraging AWS Secrets Manager. The service principal needs to provide a token that and an identifier. The name identifier is used to retrieve the secret from Secrets Manager. The token is validated against the secret. The secret in Secrets Manager needs to be a JSON object with the following structure:
{
"token": "Secret Token",
"id": "Actual principal identifier of the service"
}
Configuration (Environment Variables)
Configuration is done through environment variables. The following table lists the environment variables that can be used to configure the authentication.
Variable | Description | Default |
---|---|---|
SMAC__AUTH__USER_ID |
The claim name which contains the unique user identifier. | email |
SMAC__AUTH__PROVIDER |
The provider name. | NONE |
SMAC__AUTH__PROVIDER_COGNITO__CHECK_EXPIRATION |
Whether to check the expiration of the token. | True |
SMAC__AUTH__PROVIDER_COGNITO__JWT_HEADER_PREFIX |
The prefix for the JWT header. | Bearer |
SMAC__AUTH__PROVIDER_COGNITO__JWT_HEADER_NAME |
The name of the JWT header. | Authorization |
SMAC__AUTH__PROVIDER_COGNITO__USERPOOLS__PRIMARY__REGION |
The region of the primary user pool. | None |
SMAC__AUTH__PROVIDER_COGNITO__USERPOOLS__PRIMARY__USERPOOL_ID |
The ID of the primary user pool. | None |
SMAC__AUTH__PROVIDER_COGNITO__USERPOOLS__PRIMARY__APP_CLIENT_ID |
The app client ID of the primary user pool. | None |
SMAC__AUTH__PROVIDER_COGNITO__USERPOOLS__PRIMARY__DOMAIN |
The domain name of the primary user pool. | None |
SMAC__AUTH__PROVIDER_COGNITO__USERPOOLS__SECONDARY__REGION |
The region of the secondary user pool. | None |
SMAC__AUTH__PROVIDER_COGNITO__USERPOOLS__SECONDARY__USERPOOL_ID |
The ID of the secondary user pool. | None |
SMAC__AUTH__PROVIDER_COGNITO__USERPOOLS__SECONDARY__APP_CLIENT_ID |
The app client ID of the secondary user pool. | None |
SMAC__AUTH__PROVIDER_COGNITO__USERPOOLS__SECONDARY__DOMAIN |
The domain name of the secondary user pool. | None |
SMAC__AUTH__AUTOMATION_PRINCIPALS__SECRETS_MANAGER__REGION |
The region of the Secrets Manager. | None |
SMAC__AUTH__AUTOMATION_PRINCIPALS__SECRETS_MANAGER__PROFILE |
The AWS credentials profile to authenticate with. | None |
SMAC__AUTH__AUTOMATION_PRINCIPALS__SECRETS_MANAGER__COMMON_PREFIX |
The common prefix for the secrets in the Secrets Manager. | "" |
SMAC__AUTH__AUTOMATION_PRINCIPALS__ID_HEADER_NAME |
The name of the header containing the automation principal name. | SMAC-Principal |
SMAC__AUTH__AUTOMATION_PRINCIPALS__TOKEN_HEADER_NAME |
The name of the header containing the automation principal token. | SMAC-Token |
Note: The secondary user pool is entirely optional. If it is configured, keep in mind that the SMAC__AUTH__USER_ID
cannot be set to sub
as the user pools create the sub claim automatically. It is advised to use the email address as the user identifier in this case so that the application logic does not need to differentiate between the primary and secondary user pools.
Development
Setup
-
Install Poetry
-
Install dependencies:
poetry config virtualenvs.in-project true poetry install --with dev # Install pre-commit hooks poetry run pre-commit install
-
Run tests:
poetry run pytest --cov .
Releases
Releases are done automatically using python-semantic-release.
Refer to the CHANGELOG for the release history.
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 smac_fastapi_auth-0.2.1.tar.gz
.
File metadata
- Download URL: smac_fastapi_auth-0.2.1.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a6ed8a6f3f4fe84faf37a23f06ec1d6fc87688f047b11fb72ede2eba865904c |
|
MD5 | 689d7c2d364ff70ca5215d12d749ca80 |
|
BLAKE2b-256 | 60fe7b9aa06ce99508e7d4e7a609886e5027ac78a9967de92b9ee72b2f8a3358 |
File details
Details for the file smac_fastapi_auth-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: smac_fastapi_auth-0.2.1-py3-none-any.whl
- Upload date:
- Size: 14.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f24b2c20f12c30a6852893da77ab0db7a582dc7056ddd62a06432936f628d386 |
|
MD5 | d2fc6f5d9c76469696f9334492bb1bed |
|
BLAKE2b-256 | 7fd86a9fa1804a1e479bf6f0c3fe79374eefbfd96c806cc155bada3e1f9298d5 |