要求認証情報へのアクセス

Authentication​ Injectable は、以下のインターフェースを提供します。

  • 他のポリシーによって消費される認証データを伝播します。

  • 別のポリシーで設定済みの認証データをコンシュームします。

データを共有するために、​Authentication​ は ​AuthenticationHandler​ という trait を実装します。

pub trait AuthenticationHandler {
    fn authentication(&self) -> Option<AuthenticationData>;
    fn set_authentication(&self, authentication: Option<&AuthenticationData>);
}

AuthenticationData​ 構造体には、以下の認証データが含まれます。

pub struct AuthenticationData {
    pub principal: Option<String>,
    pub client_id: Option<String>,
    pub client_name: Option<String>,
    pub properties: Value,
}

たとえば、次のコードは ​Authentication​ データを読み込み、​client_id​ と ​client_name​ を上書きして変更します。

async fn request_filter(state: RequestState, authentication: Authentication) -> Flow<()> {
    let state = state.into_headers_state().await;
    let header_handler = state.handler();

    let auth = authentication.authentication().unwrap_or_default();

    let properties = auth.properties.as_object().cloned().unwrap_or_default();

    let client_id = header_handler
        .header("custom_client_id_header")
        .unwrap_or_default();
    let client_name = header_handler
        .header("custom_client_name_header")
        .unwrap_or_default();

    let auth = AuthenticationData::new(
        auth.principal,
        Some(client_id),
        Some(client_name),
        properties
    );

    authentication.set_authentication(Some(&auth));

    Flow::Continue(())
}

#[entrypoint]
async fn configure(launcher: Launcher) -> Result<()> {
    let filter = on_request(|rs, auth| request_filter(rs, auth));
    launcher.launch(filter).await?;
    Ok(())
}