Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Threema/libthreema/lib/examples/bin/   (Social Medium ©)  Datei vom 26.9.2026 mit Größe 5 kB image not shown  

Quelle  remote_secret.rs   Sprache: Rust

 

//! Example for creating a new Threema ID.
#![expect(unused_crate_dependencies, reason = "Example triggered false positive")]
#![expect(clippy::print_stdout, reason = "Examples are allowed to print")]

use clap::{ArgAction, Parser, Subcommand};
use libthreema::{
    cli::config::{MinimalIdentityConfig, MinimalIdentityConfigOptions},
    common::keys::{RemoteSecretAuthenticationToken, RemoteSecretHash},
    https::cli::https_client_builder,
    remote_secret::{
        monitor::{
            RemoteSecretMonitorContext, RemoteSecretMonitorInstruction, RemoteSecretMonitorProtocol,
            RemoteSecretMonitorResponse,
        },
        setup::{
            RemoteSecretSetupContext, RemoteSecretSetupResponse,
            create::{RemoteSecretCreateLoop, RemoteSecretCreateResult, RemoteSecretCreateTask},
            delete::{RemoteSecretDeleteLoop, RemoteSecretDeleteTask},
        },
    },
    utils::logging::init_stderr_logging,
};
use libthreema_dependencies::data_encoding::HEXLOWER_PERMISSIVE;
use tokio::time;
use tracing::{Level, debug, info};

#[derive(Parser)]
#[command()]
struct RemoteSecretCommand {
    #[command(flatten)]
    config: MinimalIdentityConfigOptions,

    #[command(subcommand)]
    command: Commands,

    /// Turn on debugging information.
    /// Use -v -vv to control log level.
    #[arg(short, action = ArgAction::Count)]
    verbose: u8,
}

#[derive(Subcommand)]
enum Commands {
    Create,

    Delete {
        #[arg(long, value_parser = RemoteSecretAuthenticationToken::from_hex)]
        remote_secret_authentication_token: RemoteSecretAuthenticationToken,
    },

    Monitor {
        #[arg(long, value_parser = RemoteSecretAuthenticationToken::from_hex)]
        remote_secret_authentication_token: RemoteSecretAuthenticationToken,

        #[arg(long, value_parser = RemoteSecretHash::from_hex)]
        remote_secret_hash: RemoteSecretHash,
    },
}

async fn run_create_remote_secret(
    http_client: reqwest::Client,
    context: RemoteSecretSetupContext,
) -> anyhow::Result<RemoteSecretCreateResult> {
    let mut task = RemoteSecretCreateTask::new(context);
    loop {
        match task.poll()? {
            RemoteSecretCreateLoop::Instruction(instruction) => {
                let result = instruction.request.send(&http_client).await;
                task.response(RemoteSecretSetupResponse { result })?;
            },

            RemoteSecretCreateLoop::Done(result) => return Ok(result),
        }
    }
}

async fn run_delete_remote_secret(
    http_client: reqwest::Client,
    context: RemoteSecretSetupContext,
    remote_secret_authentication_token: RemoteSecretAuthenticationToken,
) -> anyhow::Result<()> {
    let mut task = RemoteSecretDeleteTask::new(context, remote_secret_authentication_token);
    loop {
        match task.poll()? {
            RemoteSecretDeleteLoop::Instruction(instruction) => {
                let result = instruction.request.send(&http_client).await;
                task.response(RemoteSecretSetupResponse { result })?;
            },

            RemoteSecretDeleteLoop::Done(()) => return Ok(()),
        }
    }
}

async fn run_monitor_remote_secret(
    http_client: reqwest::Client,
    context: RemoteSecretMonitorContext,
) -> anyhow::Result<()> {
    let mut protocol = RemoteSecretMonitorProtocol::new(context);
    loop {
        match protocol.poll()? {
            RemoteSecretMonitorInstruction::Request(https_request) => {
                let result = https_request.send(&http_client).await;
                protocol.response(RemoteSecretMonitorResponse { result })?;
            },

            RemoteSecretMonitorInstruction::Schedule {
                timeout,
                remote_secret,
            } => {
                if let Some(remote_secret) = remote_secret {
                    info!(
                        remote_secret = HEXLOWER_PERMISSIVE.encode(&remote_secret.0),
                        remote_secret_hash = ?remote_secret.derive_hash(),
                        "Retrieved remote secret",
                    );
                }
                debug!(?timeout, "Scheduling another poll");
                time::sleep(timeout).await;
            },
        }
    }
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Parse arguments for command
    let arguments = RemoteSecretCommand::parse();

    // Configure logging
    let log_level = match arguments.verbose {
        1 => Level::DEBUG,
        2 => Level::TRACE,
        _ => Level::INFO,
    };
    init_stderr_logging(log_level);

    // Create HTTP client
    let http_client = https_client_builder().build()?;
    let config = MinimalIdentityConfig::from_options(&http_client, arguments.config).await?;
    match arguments.command {
        Commands::Create => {
            let context = config.remote_secret_setup_context()?;
            let result = run_create_remote_secret(http_client, context).await?;
            println!(
                "--remote-secret-authentication-token {}",
                HEXLOWER_PERMISSIVE.encode(&result.remote_secret_authentication_token.0)
            );
            println!(
                "--remote-secret-hash {}",
                HEXLOWER_PERMISSIVE.encode(&result.remote_secret.derive_hash().0)
            );
        },

        Commands::Delete {
            remote_secret_authentication_token,
        } => {
            let context = config.remote_secret_setup_context()?;
            run_delete_remote_secret(http_client, context, remote_secret_authentication_token).await?;
        },

        Commands::Monitor {
            remote_secret_authentication_token,
            remote_secret_hash,
        } => {
            let context =
                config.remote_secret_monitor_context(remote_secret_authentication_token, remote_secret_hash);
            run_monitor_remote_secret(http_client, context).await?;
        },
    }
    Ok(())
}

#[test]
fn verify_cli() {
    use clap::CommandFactory;
    RemoteSecretCommand::command().debug_assert();
}

Messung
CHZG
828810090

¤ Dauer der Verarbeitung: 0.3 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover


Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.