#proxy #basic-auth #jwt #hyper #authentication #http #http-basic

hyper-auth-proxy

一个简单的认证反向代理,通过 Redis 存储进行基本认证

3 个版本

0.1.2 2022 年 3 月 17 日
0.1.1 2022 年 3 月 12 日
0.1.0 2022 年 3 月 12 日

#9 in #json-web-token

AGPL-3.0-or-later

62KB
367

hyper-auth-proxy CircleCI

一个代理,从 JWT 令牌和 Redis 会话凭据进行 http 基本认证

schema

用法

基于 hyper-reverse-proxy 的小型认证代理,可用于为后端服务添加基本认证头,而无需在 Web 上将凭据进行 base64 编码。

它将使用 JWK 令牌密钥 sid 字段在 Redis 实例中查找凭据。JWT 令牌从 Authorization cookie 中读取。凭据存储在 json 中

{ "credentials": "dXNlcjp0ZXN0" }

它们可以直接使用,或者凭据可以编码(例如使用 AES)。

如果没有编码的凭据,代理将带有 Authorization 头的请求

Authorization: Basic dXNlcjp0ZXN0

主函数应包含一个 tokio 主部分并调用 run_service 函数。

示例

use hyper_auth_proxy::{run_service, ProxyConfig};

#[tokio::main]
async fn main() {
    let (_tx, rx) = tokio::sync::oneshot::channel::<()>();
    let config = ProxyConfig::default();
    let server = run_service(config.clone(), rx).await;
    println!("Running auth proxy on {:?} with backend {:?}", config.address, config.back_uri);
    if let Err(e) = server.await {
        eprintln!("server error: {}", e);
    }
}

代理配置包含以下参数

 use std::net::SocketAddr;
 struct ProxyConfig {
    pub jwt_key: String,
    pub credentials_key: String,
    pub back_uri: String,
    pub redis_uri: String,
    pub address: SocketAddr,
}

日志记录 && 调试

它使用日志 API,因此 例如 使用 env_logger 可以用以下方式启动:

$ RUST_LOG=debug hyper-auth-proxy

并且你应该有如下日志

[2022-03-16T12:51:26Z INFO  my_auth_proxy] Running auth proxy on 127.0.0.1:3000 with backend "http://backend"
[2022-03-16T12:51:33Z DEBUG hyper_auth_proxy] cannot find auth cookie: no cookies header
[2022-03-16T12:53:21Z DEBUG hyper_auth_proxy] cannot find auth cookie: no auth cookie
[2022-03-16T12:53:35Z DEBUG hyper_auth_proxy] cannot decode jwt token: cannot decode jwt token (No claims component found in token string)

依赖项

~15–28MB
~407K SLoC