#安全 #授权 #权限 #web-api #actix

actix-web-grants

为保护您的端点,扩展actix-web的授权

28个版本 (15个稳定版)

4.1.1 2024年5月29日
4.1.0 2024年3月31日
4.0.3 2023年12月8日
4.0.1 2023年11月30日
0.1.6 2021年1月17日

#23 in 认证

Download history 3857/week @ 2024-05-03 4312/week @ 2024-05-10 4078/week @ 2024-05-17 4524/week @ 2024-05-24 3331/week @ 2024-05-31 3638/week @ 2024-06-07 3878/week @ 2024-06-14 3479/week @ 2024-06-21 3479/week @ 2024-06-28 3939/week @ 2024-07-05 3731/week @ 2024-07-12 3368/week @ 2024-07-19 4274/week @ 2024-07-26 3247/week @ 2024-08-02 2392/week @ 2024-08-09 1964/week @ 2024-08-16

12,451 每月下载量
用于 3 个crates (2直接)

MIT/Apache

36KB
286

actix-web-grants

actix-web-grants

actix-web提供端点保护的授权扩展。

Crates.io Downloads Badge crates.io Documentation Apache 2.0 or MIT licensed

要检查用户对特定服务的访问权限,您可以使用内置的proc-macroAuthorityGuard或手动方式。

该库还可以与第三方解决方案(如actix-web-httpauth)集成。

如何使用

  1. 声明自己的权限提取器

最简单的方法是声明一个具有以下签名的函数(此类Fn已经实现了)

use actix_web::{dev::ServiceRequest, Error};

// You can use custom type instead of String
async fn extract(req: &ServiceRequest) -> Result<HashSet<String>, Error>
  1. 使用步骤1中定义的提取器将中间件添加到您的应用程序中
App::new()
    .wrap(GrantsMiddleware::with_extractor(extract))

步骤1和2可以被自定义中间件或与其他库的集成所替换。请参阅jwt-httpauth示例

  1. 以下示例展示了如何以任何方便的方式保护端点

proc-macro方式保护示例

#[get("/secure")]
#[actix_web_grants::protect("OP_READ_SECURED_INFO")]
async fn macro_secured() -> HttpResponse {
    HttpResponse::Ok().body("ADMIN_RESPONSE")
}
类似于ABAC的保护和自定义权限类型示例

以下是一个使用tyexpr属性的示例。但这些都是独立的功能。

expr允许您根据函数参数在宏中包含一些检查,它可以与权限通过使用all/any组合。

ty允许您为权限使用自定义类型(然后中间件需要配置)。请参阅enum-role示例

use enums::Role::{self, ADMIN};
use dto::User;

#[get("/info/{user_id}")]
#[actix_web_grants::protect("ADMIN", expr = "user_id.into_inner() == user.id", ty = "Role")]
async fn macro_secured(user_id: web::Path<i32>, user: web::Data<User>) -> HttpResponse {
    HttpResponse::Ok().body("some secured response")
}

#[post("/info/{user_id}")]
#[actix_web_grants::protect(any("ADMIN", expr = "user.is_super_user()"), ty = "Role")]
async fn admin_or_super_user(user_id: web::Path<i32>, user: web::Data<User>) -> HttpResponse {
    HttpResponse::Ok().body("some secured response")
}

Guard方式保护示例

use actix_web_grants::{AuthorityGuard, GrantsMiddleware};

App::new()
    .wrap(GrantsMiddleware::with_extractor(extract))
    .service(web::resource("/admin")
            .to(|| async { HttpResponse::Ok().finish() })
            .guard(AuthorityGuard::new("ROLE_ADMIN".to_string())))
    .service(web::resource("/admin") // fallback endpoint if you want to return a 403 HTTP code 
            .to(|| async { HttpResponse::Forbidden().finish() }))
使用Guard的`Scope`自定义回退端点示例

由于Guard仅适用于路由,如果用户没有权限,它将返回404 HTTP状态码。但您可以像这样覆盖此行为

use actix_web_grants::{AuthorityGuard, GrantsMiddleware};
use actix_web::http::header;

App::new()
    .wrap(GrantsMiddleware::with_extractor(extract))
    .service(web::scope("/admin")
        .guard(AuthorityGuard::new("ROLE_ADMIN_ACCESS".to_string()))
        .service(web::resource("/users")
            .to(|| async { HttpResponse::Ok().finish() }))
    ).service(
        web::resource("/admin{regex:$|/.*?}").to(|| async { 
            HttpResponse::TemporaryRedirect().append_header((header::LOCATION, "/login")).finish()
        }))

Guard 允许您进入 Scope(意味着您具有 "ROLE_ADMIN_ACCESS"),重定向将无法访问。即使您请求 /admin/some_undefined_page

注意: regex 是包含传递链接的 Path 变量。

手动保护方式的示例

use actix_web_grants::authorities::{AuthDetails, AuthoritiesCheck};

async fn manual_secure(details: AuthDetails) -> HttpResponse {
    if details.has_authority(ROLE_ADMIN) {
        return HttpResponse::Ok().body("ADMIN_RESPONSE");
    }
    HttpResponse::Ok().body("OTHER_RESPONSE")
}

您可以在 Git 仓库目录和 文档 中找到更多 示例

支持的 actix-web 版本

  • 对于 actix-web-grants: 2.*,支持的 actix-web 版本是 3.*
  • 对于 actix-web-grants: 3.*4.*,支持的 actix-web 版本是 4.*

依赖项

~14-25MB
~449K SLoC