Skip to main content

fastapi-cloudauth supports simple integration between FastAPI and cloud authentication services (AWS Cognito, Auth0).

Project description

FastAPI Cloud Auth

Tests codecov PyPI version

fastapi-cloudauth supports simple integration between FastAPI and cloud authentication services (AWS Cognito, Auth0). This standardize the interface for some authentication services.

Features

  • Verify access/id token
  • Authenticate permission based on scope (or groups) within access token
  • Get login user info (name, email, etc.) within ID token
  • Dependency injection for verification/getting user, powered by FastAPI
  • Support for:

Requirements

Python 3.6+

Install

$ pip install fastapi-cloudauth

Example (AWS Cognito)

Pre-requirement

  • Check region and userPoolID of AWS Cognito that you manage to
  • Create a user assigned read:users permission in AWS Cognito
  • Get Access/ID token for the created user

NOTE: access token is valid for verification and scope-based authentication. ID token is valid for verification and getting user info.

Create it

Create a file main.py with:

import os
from fastapi import FastAPI, Depends
from fastapi_cloudauth.cognito import Cognito, CognitoCurrentUser, CognitoClaims

app = FastAPI()
auth = Cognito(region=os.environ["REGION"], userPoolId=os.environ["USERPOOLID"])


@app.get("/", dependencies=[Depends(auth.scope("read:users"))])
def secure():
    # access token is valid
    return "Hello"


get_current_user = CognitoCurrentUser(
    region=os.environ["REGION"], userPoolId=os.environ["USERPOOLID"]
)


@app.get("/user/")
def secure_user(current_user: CognitoClaims = Depends(get_current_user)):
    # ID token is valid
    return f"Hello, {current_user.username}"

Run the server with:

$ uvicorn main:app

INFO:     Started server process [15332]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Interactive API Doc

Go to http://127.0.0.1:8000/docs.

You will see the automatic interactive API documentation (provided by Swagger UI).

Authorize :unlock: button can be available at the endpoints injected dependency.

You can put token and try endpoint interactively.

Swagger UI

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

fastapi-cloudauth-0.1.3.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

fastapi_cloudauth-0.1.3-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-cloudauth-0.1.3.tar.gz.

File metadata

  • Download URL: fastapi-cloudauth-0.1.3.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.7.9 Linux/5.4.0-1031-azure

File hashes

Hashes for fastapi-cloudauth-0.1.3.tar.gz
Algorithm Hash digest
SHA256 bb2094590115a466459f4230ab9630c32613d8e465d4e91fff2727a15d8ae712
MD5 1cf6ab832e0d73330d5d6bed278c52ac
BLAKE2b-256 1ebc7bcd26ec8979225b8b5d22c6e8e20419cfbc6edf6d56e070f0a5c58d33f8

See more details on using hashes here.

File details

Details for the file fastapi_cloudauth-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: fastapi_cloudauth-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.7.9 Linux/5.4.0-1031-azure

File hashes

Hashes for fastapi_cloudauth-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f8364557a39e7a13a7cf1a5dc2d66ee9c3796c61afa581ce9e0aff2cd4ed0709
MD5 c0161205030b1bce94800f758879df63
BLAKE2b-256 0503f7a7a3aee5e238770be15c35b20bfb05938e125c747b9bc8fdfde195e139

See more details on using hashes here.

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