Skip to main content

Reusable code for authentication in Python backends using FastAPI

Project description

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.

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

smac_fastapi_auth-0.4.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

smac_fastapi_auth-0.4.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file smac_fastapi_auth-0.4.0.tar.gz.

File metadata

  • Download URL: smac_fastapi_auth-0.4.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for smac_fastapi_auth-0.4.0.tar.gz
Algorithm Hash digest
SHA256 3be433db5936341b700cfc8f1331d2e9280f5563c4a9b81302199de95efdfa59
MD5 8f16f614e19480bac3c9901d6eb669d3
BLAKE2b-256 168530de3b14cd414d11da5c9f76cf2c40e9bd09da1adf6156bda32f077714ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for smac_fastapi_auth-0.4.0.tar.gz:

Publisher: release.yaml on SMK1085/smac-fastapi-auth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file smac_fastapi_auth-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for smac_fastapi_auth-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 96433789aaf46534811911d63e7dfec5c3c00c791a02a8bcde7a7961aa3b6973
MD5 c2f39854fa8f4a1b39b9f15e9a565f7c
BLAKE2b-256 dbd87cd9d245f60ded97e3db0274c28722c5c3047e598e9e560ad2286d2496c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for smac_fastapi_auth-0.4.0-py3-none-any.whl:

Publisher: release.yaml on SMK1085/smac-fastapi-auth

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