FastAPI-User-Auth 是一个简单而强大的 FastAPI 用户 RBAC 身份验证和授权库。基于 FastAPI-Amis-Admin 并提供自由扩展的功能。
FastAPI-User-Auth 是一个简单而强大的 FastAPI 用户 RBAC 身份验证和授权库。基于 FastAPI-Amis-Admin 并提供自由扩展的功能。
FastAPI-User-Auth
FastAPI-User-Auth is a simple and powerful FastAPI user authentication and authorization library based on Casbin.
Based on FastAPI-Amis-Admin and provides a freely extensible visual management interface.Source code · Online demo · Documentation · can't open the document ?
FastAPI-User-Auth is a API based on FastAPI-Amis-Admin
The application plug-in is deeply integrated with FastAPI-Amis-Admin to provide user authentication and authorization.
Casbin-based RBAC permission management supports multiple verification methods, multiple databases, and multiple granularity permission controls.
pip install fastapi-user-auth………app = FastAPI()
# Attach `request.auth` and `request.user` objects before each request is processed under the app application
auth.backend.attach_middleware(app)from fastapi_user_auth.auth.models import User
async def get_request_user(request: Request) -> Optional[User]:
# user= await auth.get_current_user(request)
if await auth.requires('admin', response=False)(request):
return request.user
else:
return Nonefastapi-user-auth supports multiple token storage methods. The default is: DbTokenStore, it is recommended to customize it to: JwtTokenStore
pip install fastapi-user-auth[jwt]…# Create an auth object using `DbTokenStore`
from fastapi_user_auth.auth.backends.db import DbTokenStore
auth = Auth(
db=db,
token_store=DbTokenStore(db=db)
)pip install fastapi-user-auth[redis] # Use `RedisTokenStore` to create an auth object
from fastapi_user_auth.auth.backends.redis import RedisTokenStore
from redis.asyncio import Redis
auth = Auth(
db=db,
token_store=RedisTokenStore(redis=Redis.from_url('redis://localhost?db=0'))
)This system adopts the Casbin RBAC model and runs a role-based priority strategy.
flowchart LR
User -. m:n .-> Role
User -. m:n .-> CasbinRule
Role -. m:n .-> Role
Role -. m:n .-> CasbinRule User model…Role modelfrom fastapi_amis_admin.models.fields import Field
from fastapi_user_auth.auth.models import Role
# Customize `Role` model, inherit `Role`;
class MyRole(Role, table=True):
icon: str = Field(None, title='图标')
is_active: bool = Field(default=True, title="是否激活")
User Auth App default management classThe default management classes can be overridden and replaced through inheritance.
For example: UserLoginFormAdmin, UserRegFormAdmin, UserInfoFormAdmin,
UserAdmin,RoleAdmin
…Inherit the AutoField ModelAdmin class to achieve field permission control. By assigning user and role permissions in the background.
perm_fields_exclude: Specify fields that do not require permission control.
from fastapi_user_auth.mixins.admin import AuthFieldModelAdmin
from fastapi_amis_admin.amis import PageSchema
from fastapi_amis_admin.admin import FieldPermEnum
class AuthFieldArticleAdmin(AuthFieldModelAdmin):
page_schema = PageSchema(label="文章管理")
model = Article
# Specify fields that do not need permission control.
perm_fields_exclude = {
FieldPermEnum.CREATE: ["title", "description", "content"],
}AuthSelectModelAdmin class to achieve data permission control. By assigning user and role permissions in the background.select_permisions: Specify permissions to query data.…Open http://127.0.0.1:8000/admin/auth/form/login in your browser:
Open http://127.0.0.1:8000/admin/ in your browser:
Open http://127.0.0.1:8000/admin/docs in your browser:
fastapi-amis-admin is open source and free to use based on Apache2.0 and can be used for commercial purposes for free, but please clearly display the copyright information about FastAPI-Amis-Admin in the display interface.Thanks to the following developers for their contributions to FastAPI-User-Auth:
暂无开放 Issues,或尚未同步最近议题。