Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

管理员鉴权模块重构 #162

Open
Goodjooy opened this issue Aug 8, 2023 · 0 comments
Open

管理员鉴权模块重构 #162

Goodjooy opened this issue Aug 8, 2023 · 0 comments

Comments

@Goodjooy
Copy link
Contributor

Goodjooy commented Aug 8, 2023

现状

在当前状态下,鉴权功能由以下几个部分共同提供

  • libs\crypto_str : 提供密码的加密和验证抽象
  • src\middleware\authorize: 提供鉴权中间件
  • src\utils\user_authorize: 提供鉴权相关的功能支持,包括用户权限分级、鉴权配置初始化内容
  • persisitence\dao\admin : 定义管理员相关的数据库操作
  • persistence\sql-models\src\admin_user : 定义管理相关的数据库模型

其中数据库相关的定义与操作已经集成到 persistence 即持久化模块中。
但是其他部分的功能,却分散在四处

解决

同时,由于鉴权模块由以下几个部分组成

  • 中间件: 用于挂载在特定handle/router 下,以提供鉴权功能
  • Extractor: 通过 Extension 中转,为handler 提供获取已鉴权用户的信息功能

看起来,鉴权模块似乎可以归入中间件当中,但是个人认为用户鉴权是具有特殊功能的组件,应该独立模块。

当前的鉴权相关的依赖树大概如下

┌──────────────────────────┐          ┌────────────┐
│                          │          │            │
│ presisitence/model_admin │          │ crypto_str │
│                          │          │            │
└───────┬────────┬─────────┘          └───┬────────┘
        │        │                        │
        │    ┌───┼────────────────────────┘
        │    │   │
        │    │   └────────────────┐
        │    │                    │
        │    │      ┌─────────┐   │   ┌─────────┐
        │    │      │         │   │   │         │
  ┌─────▼────▼──────▼──┐   ┌──┴───▼───▼─┐   ┌───┴────────┐
  │                    │   │            │   │            │
  │ handlers/user_auth │   │ middleware │   │ validation │
  │                    │   │            │   │            │
  └────────────────────┘   └────────────┘   └────────────┘

handlers/user_auth 部分创建用户时会使用crypto_str 中的加密算法供 presisitence/model_admin 相关接口使用。
middleware 鉴权时,会先使用 Validation 获取请求中的用户token, 然后使用presisitence/model_admin 获取对应用户信息,并比对密码版本,进行鉴权。

能否将鉴权部分合并到一起,成为一个crate,将以上的依赖关系改成下面这样

                                 ┌──────────────────┐
                                 │    Authorize     │
                                 │  ┌────────────┐  │
┌──────────────────────────┐     │  │            │  │
│                          │     │  │ validation │  │
│ presisitence/model_admin │     │  │            │  │
│                          │     │  └────────────┘  │
└───────┬──────────────────┘     │                  │
        │                        │  ┌────────────┐  │
        │                        │  │            │  │
        │       ┌────────────────┤  │ middleware │  │
        │       │                │  │            │  │
        │       │                │  └────────────┘  │
  ┌─────▼───────▼──────┐         │                  │
  │                    │         │  ┌────────────┐  │
  │ handlers/user_auth │         │  │            │  │
  │                    │         │  │ crypto_str │  │
  └────────────────────┘         │  │            │  │
                                 │  └────────────┘  │
                                 │                  │
                                 │                  │
                                 └──────────────────┘

将鉴权相关的代码进行一定包装,统一暴露给handlers,同时从跟项目中独立出来,便于后续的serve 代码迁移工作

还可以对密码加密流程提供一定包装,简化密码加密过程(raw -> md5 ->...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant