#运行时 #iam #protobuf #生成 #服务 #客户端 #凭证

iam-runtime-rs

生成protobuf以集成和实现iam-runtime服务

3个不稳定版本

0.4.1 2024年6月20日
0.4.0 2024年6月20日
0.3.0 2024年6月21日

#295身份验证

Download history 273/week @ 2024-06-15 112/week @ 2024-06-22 17/week @ 2024-06-29 132/week @ 2024-07-06 57/week @ 2024-07-13 115/week @ 2024-07-20 282/week @ 2024-07-27

每月586次下载

Apache-2.0

54KB
1K SLoC

iam-runtime-rs

包含从 iam-runtime protos 生成的protobuf的crate。

示例

use anyhow::{Error, Result};
use tokio::net::UnixStream;
use tonic::transport::{Endpoint, Uri};
use tower::service_fn;

use iam_runtime_rs::iam_runtime::{
    authentication_client::AuthenticationClient, authorization_client::AuthorizationClient,
    AccessRequestAction, CheckAccessRequest, ValidateCredentialRequest,
};

async fn do_auth(token: String) -> Result<(), Error> {
    let channel = Endpoint::try_from(format!("http://[::]:50051/{}", "/tmp/iam_runtime.sock"))?
        .connect_with_connector(service_fn(|u: Uri| {
            UnixStream::connect(String::from(u.path()))
        }))
        .await?;

    let mut authn_client = AuthenticationClient::new(channel.clone());
    let mut authz_client = AuthorizationClient::new(channel);

    let request = tonic::Request::new(ValidateCredentialRequest {
        credential: token.clone(),
    });

    let resp = authn_client
        .validate_credential(request)
        .await?
        .into_inner();

    if resp.result == 1 {
        return Err(Error::msg("invalid token"));
    };

    let action = AccessRequestAction {
        action: String::from("some-action"),
        resource_id: String::from("some-resource"),
    };

    let request = tonic::Request::new(CheckAccessRequest {
        credential: token,
        actions: vec![action],
    });

    let resp = authz_client.check_access(request).await?.into_inner();
    if resp.result == 1 {
        return Err(Error::msg("access denied"));
    }

    Ok(())
}

依赖

~4–6MB
~101K SLoC