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 认证
12,451 每月下载量
用于 3 个crates (2直接)
36KB
286 行
actix-web-grants
为
actix-web
提供端点保护的授权扩展。
要检查用户对特定服务的访问权限,您可以使用内置的proc-macro
、AuthorityGuard
或手动方式。
该库还可以与第三方解决方案(如actix-web-httpauth
)集成。
如何使用
- 声明自己的权限提取器
最简单的方法是声明一个具有以下签名的函数(此类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中定义的提取器将中间件添加到您的应用程序中
App::new()
.wrap(GrantsMiddleware::with_extractor(extract))
步骤1和2可以被自定义中间件或与其他库的集成所替换。请参阅jwt-httpauth示例
- 以下示例展示了如何以任何方便的方式保护端点
proc-macro
方式保护示例
#[get("/secure")]
#[actix_web_grants::protect("OP_READ_SECURED_INFO")]
async fn macro_secured() -> HttpResponse {
HttpResponse::Ok().body("ADMIN_RESPONSE")
}
类似于ABAC的保护和自定义权限类型示例
以下是一个使用ty
和expr
属性的示例。但这些都是独立的功能。
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")
}
支持的 actix-web
版本
- 对于
actix-web-grants: 2.*
,支持的actix-web
版本是3.*
- 对于
actix-web-grants: 3.*
和4.*
,支持的actix-web
版本是4.*
依赖项
~14-25MB
~449K SLoC