4

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

4

1 回答 1

1

这是一个构建过滤器的函数示例,该过滤器正是这样做的:

/// 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>

这实际上计算了身份验证。

于 2020-12-21T07:25:32.803 回答