#![cfg(feature = "client")]
use anyhow::Result;
use async_trait::async_trait;
use heddle_cli_args::{
AgentTemplateArg, AuthCommands, AuthTrustCommands, CliContext, IdentityCommands,
};
use crate::hosted_runtime::{
auth_requests::{AuthCommand, AuthTrustCommand},
device_flow::AgentTemplate,
identity::cmd_identity,
whoami::cmd_whoami,
};
#[async_trait]
pub trait HostedExtensions: Send + Sync {
async fn auth(&self, ctx: &(dyn CliContext + 'static), command: AuthCommands) -> Result<()>;
async fn identity(
&self,
ctx: &(dyn CliContext + 'static),
command: IdentityCommands,
) -> Result<()>;
async fn whoami(&self, ctx: &(dyn CliContext + 'static), server: Option<String>) -> Result<()>;
}
pub struct EnabledHostedExtensions;
#[async_trait]
impl HostedExtensions for EnabledHostedExtensions {
async fn auth(&self, ctx: &(dyn CliContext + 'static), command: AuthCommands) -> Result<()> {
let command = auth_command(command);
crate::hosted_runtime::auth::cmd_auth(ctx, command).await
}
async fn whoami(&self, ctx: &(dyn CliContext + 'static), server: Option<String>) -> Result<()> {
cmd_whoami(ctx, server).await
}
async fn identity(
&self,
ctx: &(dyn CliContext + 'static),
command: IdentityCommands,
) -> Result<()> {
cmd_identity(ctx, command).await
}
}
fn auth_command(command: AuthCommands) -> AuthCommand {
match command {
AuthCommands::Login {
server,
open_browser,
credential,
} => AuthCommand::Login {
server,
open_browser,
credential,
},
AuthCommands::Logout { server } => AuthCommand::Logout { server },
AuthCommands::Status { server } => AuthCommand::Status { server },
AuthCommands::Trust { command } => AuthCommand::Trust {
command: match command {
AuthTrustCommands::Show(args) => AuthTrustCommand::Show {
server: args.server,
},
AuthTrustCommands::Replace(args) => AuthTrustCommand::Replace {
server: args.server,
expected_current_public_key: args.expect_current_public_key,
key_id: args.key_id,
public_key: args.public_key,
},
},
},
AuthCommands::DeriveAgent {
server,
agent_id,
ttl_secs,
scopes,
allowed_operations,
template,
out,
} => AuthCommand::DeriveAgent {
server,
agent_id,
ttl_secs,
scopes,
allowed_operations,
template: template.map(agent_template),
out,
},
AuthCommands::CreateServiceToken {
name,
namespace,
server,
out,
} => AuthCommand::CreateServiceToken {
name,
namespace,
server,
out,
},
}
}
fn agent_template(template: AgentTemplateArg) -> AgentTemplate {
match template {
AgentTemplateArg::Reviewer => AgentTemplate::Reviewer,
AgentTemplateArg::Contributor => AgentTemplate::Contributor,
AgentTemplateArg::CiLanding => AgentTemplate::CiLanding,
}
}