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.1.tar.gz
(4.0 kB
view hashes)
Built Distribution
Close
Hashes for fastapi_easyauth-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2281cc43d0a18a82def9ea9bcbb830b601ea283645415d504be2da026aad8eb6 |
|
MD5 | d93270f984e5b25c59c47b93c297ffef |
|
BLAKE2b-256 | 855006f2efcab660f9c7a4db37dd85ca7ee85483e01f946be524540badfe4e21 |