V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
xianmofeng3
V2EX  ›  Python

各位大佬, 推荐一个 fastapi 用户权限库

  •  
  •   xianmofeng3 · 11 天前 · 1042 次点击
    现在我已经使用了 jwt 生成了一个 token , 我想在解析 token 的时候,顺便校验这个用户或者背后的用户组, 有没有请求这个接口的权限, 我看过 casbin , 好想有点麻烦, 最好就是类似于 django 的那种
    13 条回复    2024-05-17 11:24:57 +08:00
    fushall
        1
    fushall  
       11 天前   ❤️ 1
    不知道楼主老哥是不是用 RBAC 那种的权限管理。

    这种的话手写就行了,代码其实没有很多,用了 ORM 直接连表查询就行

    RBAC ,其实就是角色表,权限表,权限角色表,再来个用户角色表,四个表,把你系统的用户 ID 和角色放在用户角色表就行。
    GeekGao
        2
    GeekGao  
       11 天前
    如果只是校验用户和组匹配,很简单,自己撸就行了
    GeekGao
        3
    GeekGao  
       11 天前
    ```python

    from fastapi import Depends, FastAPI, HTTPException, status
    from fastapi.security import OAuth2PasswordBearer
    import jwt

    app = FastAPI()

    oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

    def get_current_user(token: str = Depends(oauth2_scheme)):
    try:
    payload = jwt.decode(token, "your_secret_key", algorithms=["HS256"])
    user_id = payload.get("sub")
    if user_id is None:
    raise credentials_exception
    # 在这里,你可以检查用户是否存在以及用户所属的组是否存在
    # 例如:user = get_user_from_db(user_id)
    # if user is None or not user.group_exists:
    # raise credentials_exception
    except jwt.PyJWTError:
    raise credentials_exception
    return user_id

    @app.get("/users/me")
    async def read_users_me(user_id: str = Depends(get_current_user)):
    return {"user_id": user_id}

    ```
    xianmofeng3
        4
    xianmofeng3  
    OP
       10 天前 via iPhone
    @fushall 是的老哥,主要是 我不太想在每一个方法都这样联查, 代码有点冗余,或者还有没有更好的方案不过还是谢谢你的回答
    xianmofeng3
        5
    xianmofeng3  
    OP
       10 天前 via iPhone
    @GeekGao 我懂你意思的 我也是想过这么实现的, 我可能更想做成 user /add_tags put 这样的形式
    xianmofeng3
        6
    xianmofeng3  
    OP
       10 天前 via iPhone
    @fushall @GeekGao 我看到了有一个叫 fastapi_authz 的项目 两位老哥也可以看看
    xianmofeng3
        7
    xianmofeng3  
    OP
       10 天前 via iPhone
    @fushall @GeekGao 我看到了有一个剪 fastapi_authz 的 github 项目 两位老哥有空也可以看一下
    misoomang
        8
    misoomang  
       9 天前
    casbin 是否可以参考下,主要是整理 rbac 模型数据,剩下的可以通过 middleware 的方式进行权限管控
    xianmofeng3
        9
    xianmofeng3  
    OP
       9 天前 via iPhone
    @misoomang 好的老哥,除了 casbin 还有别的做法吗?就真的只能联查了吗
    xianmofeng3
        11
    xianmofeng3  
    OP
       6 天前 via iPhone
    @gray0 好的老哥! 我先看看! 你真的是我的及时雨
    xianmofeng3
        12
    xianmofeng3  
    OP
       6 天前 via iPhone
    @gray0 另外还想请教一个问题 就是 sqlalchemy async_create_engine 和 PG 数据库一起使用的时候是不是不能设置 schema
    gray0
        13
    gray0  
       6 天前
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4627 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 09:50 · PVG 17:50 · LAX 02:50 · JFK 05:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.