Skip to main content

FastAPI authentication package.

Project description

FastAPI auth library.

It`s simple to connect to your project. Just make user_data verification, and library will manage JWT-tokens.

Installation

Install package with pip:

pip install fastapi-authtools

...or with poetry:

poetry add fastapi-authtools

Usage

You can you it with JWT-token (default in you headers).

from fastapi import FastAPI, Request, Body

from fastapi_authtools import AuthManager, login_required
from fastapi_authtools.models import UsernamePasswordToken, UserModel


app = FastAPI()

# JWT token settings
SECRET_KEY = 'secretSERCRET007'
EXPIRE_MINUTES = 60 * 40
ALGORITHM = "HS256"

# create login manager
auth_manager = AuthManager(
    app=app,
    secret_key=SECRET_KEY,
    algorithm=ALGORITHM,
    expire_minutes=EXPIRE_MINUTES
)

# now you can use login_manager directly or py adding it to the application statement
# it`s comfortable while dealing with APIRouters
app.state.auth_manager = auth_manager


@app.get("/")
@login_required  # make this endpoint allowed only for authenticated users
async def homepage(request: Request):
    current_user = request.user
    return {"current_user": current_user}


@app.post("/auth/token", status_code=201)
async def get_access_token(request: Request, user_data: UsernamePasswordToken = Body()):
    # ... here goes db user verification and getting user information
    # user = get_login_user(user_data)
    user = UserModel(
        email="suslanchikmol@gmail.con",
        username="michael7nightingale"
    )
    token = request.app.state.auth_manager.create_token(user)
    return {"access_token": token}

But you can still use cookies to save token, just define user_cookies as True when initialize AuthManager.

To use templates and form data you should install jinja2 and python-multipart.

from fastapi import FastAPI, Request
from fastapi.responses import RedirectResponse
from fastapi.templating import Jinja2Templates

from fastapi_authtools import AuthManager, login_required
from fastapi_authtools.models import UserModel


app = FastAPI()
templates = Jinja2Templates(directory="templates")

# JWT token settings
SECRET_KEY = 'secretSERCRET007'
EXPIRE_MINUTES = 60 * 40
ALGORITHM = "HS256"

# create login manager
auth_manager = AuthManager(
    app=app,
    use_cookies=True,
    secret_key=SECRET_KEY,
    algorithm=ALGORITHM,
    expire_minutes=EXPIRE_MINUTES
)

# now you can use login_manager directly or py adding it to the application statement
# it`s comfortable while dealing with APIRouters
app.state.auth_manager = auth_manager


@app.get("/")
@login_required  # make this endpoint allowed only for authenticated users
async def homepage(request: Request):
    return templates.TemplateResponse(
        name="homepage.html",
        context={"request": request, "current_user": request.user}
    )


@app.get('/login')
async def login_get(request: Request):
    return templates.TemplateResponse(
        name='login.html',
        context={"request": request}
    )


@app.post("/login", status_code=201)
async def login_post(request: Request):
    user_data = await request.form()
    # ... here goes db user verification and getting user information
    # user = get_login_user(user_data)
    user = UserModel(
        email="suslanchikmol@gmail.con",
        username="michael7nightingale"
    )
    response = RedirectResponse(app.url_path_for("homepage"), status_code=303)
    app.state.auth_manager.login(response, user)
    return response

Auth manager adds authentication middleware to your application instance and uses authentication backends to treat token and request user instance.

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_authtools-0.6.2.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

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

fastapi_authtools-0.6.2-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_authtools-0.6.2.tar.gz.

File metadata

  • Download URL: fastapi_authtools-0.6.2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0rc1

File hashes

Hashes for fastapi_authtools-0.6.2.tar.gz
Algorithm Hash digest
SHA256 f7da20f8436fb70252f0648b207263e5b74487bfd58bd86733cb56e429e30af6
MD5 0f70a02d3b884de654669d4942ee39a5
BLAKE2b-256 4778d62c01eb198d4c21f2426fb76d905ca805ecd48266b54fbbc17f4272a973

See more details on using hashes here.

File details

Details for the file fastapi_authtools-0.6.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_authtools-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2d2471b685da123245ecff03a15aa0bf549b3ebb59f5af483ae90613065f0eec
MD5 05c6b8e9416f4d2db94184e016fee5f3
BLAKE2b-256 3f317e8da9405861d9299f9f4a4d5a82ddf8130da1b926a13568b4cbd3310cb9

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