Skip to main content

A library for quickly creating authentication using JWT and Cookies

Project description

FastAPI EasyAuth

What is this?

This library quickly and easily creates authentication using JWT and Cookies. You can also use easyauth to identify an active user


Using

First, we need to import the Auth and Jwt classes from easyauth

from fastapi_easyauth import Auth, Jwt, ALGORITHM

After that, we create instances of classes

jwt = Jwt(
    secret = "SECRET", # The secret key for generating tokens and decoding them. Keep the token secret
    algorithm = ALGORITHM.HS256 # The encryption algorithm
)

auth = Auth(
    cookie_name = "user" # The Name Of The Cookie File
    jwt = jwt
    expires = exp.EXPIRES_30_DAYS # Cookie lifetime
)

Great, everything is ready. Now we can create tokens and decode them. Also check if the user is active.

from fastapi import FastAPI, Request, Response, Depends
from fastapi_easyauth import Auth, Jwt, ALGORITHM, exp

from pydantic import BaseModel

class User(BaseModel):
    name: str
    password: str

app = FastAPI()

jwt = Jwt(
    secret = "SECRET",
    algorithm = ALGORITHM.HS256
)

auth = Auth(
    cookie_name = "user"
    jwt = jwt
    expires = exp.EXPIRES_30_DAYS
)

@app.post('/log')
def log(user: User, response: Response):
    token = jwt.create_token(user)
    auth.save_token_in_cookie(response, token)
    return {'status': 200}


@app.get('/active')
def active(request: Request, response: Response, user: User = Depends(auth.active_user))
    return user

There are two auxiliary functions hash_password and not_authorized

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-easyauth-0.0.3.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

fastapi_easyauth-0.0.3-py3-none-any.whl (4.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page