Skip to main content

SMAC: FastAPI Auth

GitHub Workflow

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

  1. Install Poetry

  2. Install dependencies:

    poetry config virtualenvs.in-project true
    poetry install --with dev
    
    # Install pre-commit hooks
    poetry run pre-commit install
    
  3. Run tests:

    poetry run pytest --cov .
    

Releases

Releases are done automatically using python-semantic-release.

Refer to the CHANGELOG for the release history.

Note that minor releases are done, if the underlying fastapi version changes. This is to ensure that the package is compatible with the latest fastapi version and to address any security vulnerabilities.

Metadata

Release files for smac-fastapi-auth 0.4.1

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

Source distribution (sdist)

Source distribution for smac-fastapi-auth 0.4.1
File Size Uploaded Provenance Actions
smac_fastapi_auth-0.4.1.tar.gz 12.1 kB With provenance Details for smac_fastapi_auth-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for smac-fastapi-auth 0.4.1
File Interpreter ABI Platform Actions
smac_fastapi_auth-0.4.1-py3-none-any.whl Python 3 none any Details for smac_fastapi_auth-0.4.1-py3-none-any.whl

Total release size: 27.0 kB

Release files / smac_fastapi_auth-0.4.1.tar.gz

Download URL smac_fastapi_auth-0.4.1.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
1c313c72353b8eb38f9a5c15ec498b9a82bedade5b9d9283afdfe7e8a8aaf953
BLAKE2b-256 checksum
How to use checksums
19399312aa64e3e5bf2defafea144717838710ab989eee8de1b6c8078dab2315
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 29, 2024.

Transparency log

Release files / smac_fastapi_auth-0.4.1-py3-none-any.whl

Download URL smac_fastapi_auth-0.4.1-py3-none-any.whl
Size 14.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dffa140ab4720058e8e5e1b7b81795905a25bc903fa66b067dfccfebc6b81633
BLAKE2b-256 checksum
How to use checksums
66065fb4072b3f88fdcd535c19674f0ab1521820a63d5bb40af2c7367ea23a06
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 29, 2024.

Transparency log

Security for smac-fastapi-auth 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for smac_fastapi_auth-0.4.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 29, 2024.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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