
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
This is a login/password authentication module that can be quickly and easily integrated into your project. JWT token is used as the authentication method. It is recommended to use the module in small projects and pet projects.
To add the module to your project, you need to:
get_user
method in auth_manager
auth_manager
if they differ from yoursMore detailed installation and configuration instructions can be found in the Installation
section.
Based on AuthX
Install module
pip install fastapi-lite-auth
The module is currently being prepared for publishing to PyPI
.
In the folder containing your API routers (usually routers/
), create a file named auth.py
.
Import the necessary components and create the API router:
auth.py
:
from fastapi import APIRouter
from fastapi_lite_auth import auth_config, auth_router, auth_manager
auth_config.authx_ready()
router = APIRouter()
router.include_router(
router=auth_router
)
The auth_config.authx_ready()
function configures the AuthX object. You should call it after modifying any auth_config
settings.
To make the module work, you need to do a few things:
The model is an ORM-oriented class. The schema is a class that describes the data the API will return.
By default, they look like this:
from pydantic import BaseModel
class BasicGetUserSchema(BaseModel):
id: int
name: str
username: str
email: str
class BasicUserModel:
id: int
name: str
email: str
username: str
password: str
Example of overriding the user model and schema:
from pydantic import BaseModel
from fastapi_lite_auth import auth_config
class CustomGetUserSchema(BaseModel):
id: int
full_name: str
phone: str
username: str
email: str
passport_number: str
insurance_number: str
class CustomUserModel:
id: int
full_name: str
phone: str
username: str
email: str
passport_number: str
insurance_number: str
password: str
auth_config.models_config.UserModel = CustomUserModel
auth_config.schemas_config.GetUserSchema = CustomGetUserSchema
This is a function that should find a user record in your database using the field used as login. Requirements:
login
argument of type str
None
if not foundExample override:
import sqlite3
from fastapi_lite_auth import auth_config, auth_manager
def get_user_by_login(login: str | None = None) -> auth_config.models_config.UserModel | None:
conn = sqlite3.connect("./db.db")
select = conn.execute(f"SELECT * FROM user WHERE email = ?", (login,))
res = select.fetchone()
if res is None:
return None
user_model = auth_config.models_config.UserModel()
user_model.id = res[0]
user_model.full_name = res[1]
user_model.username = res[2]
user_model.email = res[3]
user_model.password = res[4]
return user_model
auth_manager.get_user = get_user_by_login
By default, username
and password
fields are used.
Example configuration:
from fastapi_lite_auth import auth_config
auth_config.login_config.login_field_name = "email"
auth_config.login_config.password_field_name = "password"
auth_config.authx_ready()
The authentication token is stored in a cookie. When making a request to the server, it must be sent in the Credentials
HTTP header.
To retrieve the user from the JWT token, you need to specify a dependency in the route function:
from fastapi import APIRouter, Depends
from fastapi_lite_auth import current_user
router = APIRouter()
@router.get(path="/me")
async def get_user(user = Depends(current_user)):
return {"user": user}
The current_user
dependency returns an instance of the GetUserSchema
class-schema with the authenticated user's data, which is configured in section 3.1.
How it works:
Credentials
headerget_user
function, which is configured in section 3.2.This function is used to hash the incoming password for comparison. Requirements:
data
argument of type str
str
hashExample:
from hashlib import sha256
from fastapi_lite_auth import auth_manager
def hash(data: str) -> str:
return sha256(data.encode()).hexdigest()
auth_manager.hash = hash
The secret key is used to sign the JWT token. It should be stored in environment variables. By default, it’s generated from the current datetime.
Example override:
import os
from fastapi_lite_auth import auth_config
auth_config.token_config.secret_key = os.getenv("AUTH_SECRET")
auth_config.authx_ready()
Here’s how to configure cookies and their default values:
from fastapi_lite_auth import auth_config
auth_config.cookie_config.cookie_name = "auth_token"
auth_config.cookie_config.cookie_httponly = False
auth_config.cookie_config.cookie_secure = False
auth_config.cookie_config.cookie_samesite = "lax"
auth_config.cookie_config.cookie_max_age = 3600
auth_config.cookie_config.cookie_path = "/"
auth_config.cookie_config.cookie_domain = None
auth_config.cookie_config.cookie_expires = None
auth_config.authx_ready()
You can check out the example app in the example
directory.
Its configuration is described in example/api/routers/auth.py
.
pip install -r requirements.txt
or
pip install "fastapi[standard]"
pip install authx
python -m example.api.main
or
python3 -m example.api.main
FAQs
Simple login/password authentication module for FastAPI project.
We found that fastapi-lite-auth demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.