remote_secret.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //! Example for creating a new Threema ID.
  2. #![expect(unused_crate_dependencies, reason = "Example triggered false positive")]
  3. #![expect(clippy::print_stdout, reason = "Examples are allowed to print")]
  4. use clap::{ArgAction, Parser, Subcommand};
  5. use libthreema::{
  6. cli::config::{MinimalIdentityConfig, MinimalIdentityConfigOptions},
  7. common::keys::{RemoteSecretAuthenticationToken, RemoteSecretHash},
  8. https::cli::https_client_builder,
  9. remote_secret::{
  10. monitor::{
  11. RemoteSecretMonitorContext, RemoteSecretMonitorInstruction, RemoteSecretMonitorProtocol,
  12. RemoteSecretMonitorResponse,
  13. },
  14. setup::{
  15. RemoteSecretSetupContext, RemoteSecretSetupResponse,
  16. create::{RemoteSecretCreateLoop, RemoteSecretCreateResult, RemoteSecretCreateTask},
  17. delete::{RemoteSecretDeleteLoop, RemoteSecretDeleteTask},
  18. },
  19. },
  20. utils::logging::init_stderr_logging,
  21. };
  22. use libthreema_dependencies::data_encoding::HEXLOWER_PERMISSIVE;
  23. use tokio::time;
  24. use tracing::{Level, debug, info};
  25. #[derive(Parser)]
  26. #[command()]
  27. struct RemoteSecretCommand {
  28. #[command(flatten)]
  29. config: MinimalIdentityConfigOptions,
  30. #[command(subcommand)]
  31. command: Commands,
  32. /// Turn on debugging information.
  33. /// Use -v -vv to control log level.
  34. #[arg(short, action = ArgAction::Count)]
  35. verbose: u8,
  36. }
  37. #[derive(Subcommand)]
  38. enum Commands {
  39. Create,
  40. Delete {
  41. #[arg(long, value_parser = RemoteSecretAuthenticationToken::from_hex)]
  42. remote_secret_authentication_token: RemoteSecretAuthenticationToken,
  43. },
  44. Monitor {
  45. #[arg(long, value_parser = RemoteSecretAuthenticationToken::from_hex)]
  46. remote_secret_authentication_token: RemoteSecretAuthenticationToken,
  47. #[arg(long, value_parser = RemoteSecretHash::from_hex)]
  48. remote_secret_hash: RemoteSecretHash,
  49. },
  50. }
  51. async fn run_create_remote_secret(
  52. http_client: reqwest::Client,
  53. context: RemoteSecretSetupContext,
  54. ) -> anyhow::Result<RemoteSecretCreateResult> {
  55. let mut task = RemoteSecretCreateTask::new(context);
  56. loop {
  57. match task.poll()? {
  58. RemoteSecretCreateLoop::Instruction(instruction) => {
  59. let result = instruction.request.send(&http_client).await;
  60. task.response(RemoteSecretSetupResponse { result })?;
  61. },
  62. RemoteSecretCreateLoop::Done(result) => return Ok(result),
  63. }
  64. }
  65. }
  66. async fn run_delete_remote_secret(
  67. http_client: reqwest::Client,
  68. context: RemoteSecretSetupContext,
  69. remote_secret_authentication_token: RemoteSecretAuthenticationToken,
  70. ) -> anyhow::Result<()> {
  71. let mut task = RemoteSecretDeleteTask::new(context, remote_secret_authentication_token);
  72. loop {
  73. match task.poll()? {
  74. RemoteSecretDeleteLoop::Instruction(instruction) => {
  75. let result = instruction.request.send(&http_client).await;
  76. task.response(RemoteSecretSetupResponse { result })?;
  77. },
  78. RemoteSecretDeleteLoop::Done(()) => return Ok(()),
  79. }
  80. }
  81. }
  82. async fn run_monitor_remote_secret(
  83. http_client: reqwest::Client,
  84. context: RemoteSecretMonitorContext,
  85. ) -> anyhow::Result<()> {
  86. let mut protocol = RemoteSecretMonitorProtocol::new(context);
  87. loop {
  88. match protocol.poll()? {
  89. RemoteSecretMonitorInstruction::Request(https_request) => {
  90. let result = https_request.send(&http_client).await;
  91. protocol.response(RemoteSecretMonitorResponse { result })?;
  92. },
  93. RemoteSecretMonitorInstruction::Schedule {
  94. timeout,
  95. remote_secret,
  96. } => {
  97. if let Some(remote_secret) = remote_secret {
  98. info!(
  99. remote_secret = HEXLOWER_PERMISSIVE.encode(&remote_secret.0),
  100. remote_secret_hash = ?remote_secret.derive_hash(),
  101. "Retrieved remote secret",
  102. );
  103. }
  104. debug!(?timeout, "Scheduling another poll");
  105. time::sleep(timeout).await;
  106. },
  107. }
  108. }
  109. }
  110. #[tokio::main]
  111. async fn main() -> anyhow::Result<()> {
  112. // Parse arguments for command
  113. let arguments = RemoteSecretCommand::parse();
  114. // Configure logging
  115. let log_level = match arguments.verbose {
  116. 1 => Level::DEBUG,
  117. 2 => Level::TRACE,
  118. _ => Level::INFO,
  119. };
  120. init_stderr_logging(log_level);
  121. // Create HTTP client
  122. let http_client = https_client_builder().build()?;
  123. let config = MinimalIdentityConfig::from_options(&http_client, arguments.config).await?;
  124. match arguments.command {
  125. Commands::Create => {
  126. let context = config.remote_secret_setup_context()?;
  127. let result = run_create_remote_secret(http_client, context).await?;
  128. println!(
  129. "--remote-secret-authentication-token {}",
  130. HEXLOWER_PERMISSIVE.encode(&result.remote_secret_authentication_token.0)
  131. );
  132. println!(
  133. "--remote-secret-hash {}",
  134. HEXLOWER_PERMISSIVE.encode(&result.remote_secret.derive_hash().0)
  135. );
  136. },
  137. Commands::Delete {
  138. remote_secret_authentication_token,
  139. } => {
  140. let context = config.remote_secret_setup_context()?;
  141. run_delete_remote_secret(http_client, context, remote_secret_authentication_token).await?;
  142. },
  143. Commands::Monitor {
  144. remote_secret_authentication_token,
  145. remote_secret_hash,
  146. } => {
  147. let context =
  148. config.remote_secret_monitor_context(remote_secret_authentication_token, remote_secret_hash);
  149. run_monitor_remote_secret(http_client, context).await?;
  150. },
  151. }
  152. Ok(())
  153. }
  154. #[test]
  155. fn verify_cli() {
  156. use clap::CommandFactory;
  157. RemoteSecretCommand::command().debug_assert();
  158. }