identity_create.rs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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::Parser;
  5. use data_encoding::HEXLOWER;
  6. use libthreema::{
  7. cli::{CommonConfig, CommonConfigOptions},
  8. common::ClientInfo,
  9. csp_e2e::identity::create::{
  10. CreateIdentityContext, CreateIdentityLoop, CreateIdentityResponse, CreateIdentityResult,
  11. CreateIdentityTask,
  12. },
  13. https::cli::https_client_builder,
  14. utils::logging::init_stderr_logging,
  15. };
  16. use tracing::Level;
  17. #[derive(Parser)]
  18. #[command()]
  19. struct CreateIdentityCommand {
  20. #[command(flatten)]
  21. config: CommonConfigOptions,
  22. }
  23. async fn run_create_identity(
  24. http_client: reqwest::Client,
  25. context: CreateIdentityContext,
  26. ) -> anyhow::Result<CreateIdentityResult> {
  27. let mut task = CreateIdentityTask::new();
  28. loop {
  29. match task.poll(&context)? {
  30. CreateIdentityLoop::Instruction(instruction) => {
  31. let result = instruction.request.send(&http_client).await;
  32. task.response(CreateIdentityResponse { result })?;
  33. },
  34. CreateIdentityLoop::Done(result) => return Ok(result),
  35. }
  36. }
  37. }
  38. #[tokio::main]
  39. async fn main() -> anyhow::Result<()> {
  40. // Configure logging
  41. init_stderr_logging(Level::TRACE);
  42. // Create HTTP client
  43. let http_client = https_client_builder().build()?;
  44. // Parse arguments for command
  45. let arguments = CreateIdentityCommand::parse();
  46. let config = CommonConfig::from_options(&http_client, arguments.config).await?;
  47. // Create identity
  48. let CreateIdentityResult {
  49. identity,
  50. client_key,
  51. server_group,
  52. } = run_create_identity(
  53. http_client,
  54. CreateIdentityContext {
  55. client_info: ClientInfo::Libthreema,
  56. config: config.config,
  57. flavor: config.flavor,
  58. },
  59. )
  60. .await?;
  61. println!("--threema-id {identity}");
  62. println!("--client-key {}", HEXLOWER.encode(client_key.as_bytes()));
  63. println!("--csp-server-group {server_group}");
  64. Ok(())
  65. }
  66. #[test]
  67. fn verify_cli() {
  68. use clap::CommandFactory;
  69. CreateIdentityCommand::command().debug_assert();
  70. }