Rust (warp) 如何丢弃未经授权的请求?

标签 rust rust-warp

假设我有一个函数来检查授权 header 是否有效以及身份验证是否正确。如何制作一个扭曲过滤器来丢弃所有带有无效 header 或虚假凭据的请求?

最佳答案

这是一个构建过滤器的函数示例,该过滤器完全执行此操作:

/// A warp filter that checks the authorization through API tokens.
/// The header `API_TOKEN_HEADER` should be present and valid otherwise the request is rejected.
pub async fn api_token_filter(
    context: SharedContext,
) -> impl Filter<Extract = (), Error = Rejection> + Clone {
    let with_context = warp::any().map(move || context.clone());
    warp::header::header(API_TOKEN_HEADER)
        .and(with_context)
        .and_then(authorize_token)
        .and(warp::any())
        .untuple_one()
}
在哪里:API_TOKEN_HEADER是您要检查的标题。authorize_token是一个带签名的函数
async fn authorize_token(token: String, context: SharedContext) -> Result<(), Rejection>
这实际上计算了身份验证。

关于Rust (warp) 如何丢弃未经授权的请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65383671/

相关文章:

rust - 处理拒绝时获取当前路径

rust - 如果外部包装箱明确要求静态生命周期,该怎么办?

rust - 有没有办法在 Warp 中作为过滤器的一部分进行验证?

rust - 如何解决 Rust 中缺少抽象类的问题?

rust - 为什么我会收到有关非详尽模式的错误消息?

rust - 在循环中/分配: expected type parameter `T` ,找到了 `&T`

methods - 翘曲的根路径示例?

rust - 使用可变引用覆盖变量时借用检查器错误

rust - 从互斥体 "borrowed value does not live long enough"中借用数据

rust - super 错误: invalid certificate: UnknownIssuer