百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
F

fastapi-user-auth

> 后端框架
开源

FastAPI-User-Auth 是一个简单而强大的 FastAPI 用户 RBAC 身份验证和授权库。基于 FastAPI-Amis-Admin 并提供自由扩展的功能。

391 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

FastAPI-User-Auth 是一个简单而强大的 FastAPI 用户 RBAC 身份验证和授权库。基于 FastAPI-Amis-Admin 并提供自由扩展的功能。

简体中文 | English

Project Introduction

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.

Permission types

  • Page Permissions: Control whether the user can access a certain menu page. If it is not accessible, the menu will not be displayed, and all routes under the page will be inaccessible.
  • Action Permission: Controls whether the user can perform an action and whether the button is displayed. For example: add, update, delete, etc.
  • Field permissions: Control whether users can operate a certain field. For example: list display fields, filter fields, add fields, update fields, etc.
  • Data permissions: Control the range of data that users can operate. For example: they can only operate the data they created, only the data of the last 7 days, etc.

Installation

bash
pip install fastapi-user-auth

Simple example

…

Authentication methods

Decorator

  • Recommended scenario: Single route. Supports synchronous/asynchronous routing.
…

Dependencies (recommended)

  • Recommended scenarios: single route, route set, FastAPI application.
…

Middleware

  • Recommended scenario: FastAPI application
python
app = FastAPI()
# Attach `request.auth` and `request.user` objects before each request is processed under the app application
auth.backend.attach_middleware(app)

Call directly

  • Recommended scenario: non-routing method
python
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 None

Token Storage Backend

fastapi-user-auth supports multiple token storage methods. The default is: DbTokenStore, it is recommended to customize it to: JwtTokenStore

JwtTokenStore

  • pip install fastapi-user-auth[jwt]
…

DbTokenStore

python
# Create an auth object using `DbTokenStore`
from fastapi_user_auth.auth.backends.db import DbTokenStore

auth = Auth(
    db=db,
    token_store=DbTokenStore(db=db)
)

RedisTokenStore

  • pip install fastapi-user-auth[redis]
python
# 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'))
)

RBAC model

This system adopts the Casbin RBAC model and runs a role-based priority strategy.

  • Permissions can be assigned to roles or directly to users.
  • Users can have multiple roles.
  • A role can have multiple sub-roles.
  • The permission policy owned by the user has a higher priority than the permission policy of the role it owns.
mermaid
flowchart LR
    User -. m:n .-> Role
    User -. m:n .-> CasbinRule
    Role -. m:n .-> Role
    Role -. m:n .-> CasbinRule 

Advanced expansion

Extend the User model

…

Extend the Role model

python
from 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="是否激活")

Customize User Auth App default management class

The default management classes can be overridden and replaced through inheritance. For example: UserLoginFormAdmin, UserRegFormAdmin, UserInfoFormAdmin, UserAdmin,RoleAdmin

…

ModelAdmin permission control

Field permissions

  • 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.

python
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"],
    }

Data permissions

  • Inherit the AuthSelectModelAdmin class to achieve data permission control. By assigning user and role permissions in the background.
  • select_permisions: Specify permissions to query data.
…

Interface preview

  • 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:

License Agreement

  • 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

Thanks to the following developers for their contributions to FastAPI-User-Auth:

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Pythonfastapifastapi-adminfastapi-amis-adminfastapi-auth

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架