1use anyhow::Result;
2use clap::{Parser, Subcommand};
3use matrix_sdk::{
4 authentication::matrix::MatrixSession,
5 encryption::secret_storage::SecretStore,
6 ruma::{events::secret::request::SecretName, OwnedDeviceId, OwnedUserId},
7 AuthSession, Client, SessionMeta, SessionTokens,
8};
9use url::Url;
1011/// A command line example showcasing how the secret storage support works in
12/// the Matrix Rust SDK.
13///
14/// Secret storage is an account data backed encrypted key/value store. You can
15/// put or get secrets from the store.
16#[derive(Parser, Debug)]
17struct Cli {
18/// The homeserver to connect to.
19#[clap(value_parser)]
20homeserver: Url,
2122/// The user ID that should be used to restore the session.
23#[clap(value_parser)]
24user_id: OwnedUserId,
2526/// The user name that should be used for the login.
27#[clap(value_parser)]
28device_id: OwnedDeviceId,
2930/// The password that should be used for the login.
31#[clap(value_parser)]
32access_token: String,
3334/// Set the proxy that should be used for the connection.
35#[clap(short, long)]
36proxy: Option<Url>,
3738/// Enable verbose logging output.
39#[clap(short, long, action)]
40verbose: bool,
4142/// The secret storage key, this key will be used to open the secret-store.
43#[clap(long, action)]
44secret_store_key: String,
4546/// The sub-command to run.
47#[command(subcommand)]
48command: Commands,
49}
5051#[derive(Debug, Subcommand)]
52enum Commands {
53/// Retrieve a secret from the homeserver.
54GetSecret { secret_name: SecretName },
55/// Upload a secret to the homeserver.
56SetSecret { secret_name: SecretName, secret: String },
57/// Import all known and specced secrets from the secret store into the
58 /// local database.
59 ///
60 /// **Note**: This command won't strictly do the right thing, as we are
61 /// reusing a device ID and access token from a different device. It will
62 /// import the secrets correctly, but it will sign device keys which don't
63 /// belong to the provided device ID.
64ImportKnownSecrets,
65}
6667async fn get_secret(secret_store: SecretStore, secret_name: SecretName) -> Result<()> {
68let secret = secret_store.get_secret(secret_name.to_owned()).await?;
6970if let Some(secret) = secret {
71println!("Secret: {secret}");
72 } else {
73println!("No secret with the name {secret_name} found")
74 }
7576Ok(())
77}
7879async fn set_secret(
80 secret_store: SecretStore,
81 secret_name: SecretName,
82 secret: &str,
83) -> Result<()> {
84 secret_store.put_secret(secret_name.to_owned(), secret).await?;
8586println!("Secret {secret_name} was successfully encrypted and stored on the homeserver");
8788Ok(())
89}
9091async fn import_known_secrets(client: Client, secret_store: SecretStore) -> Result<()> {
92 secret_store.import_secrets().await?;
9394let status = client
95 .encryption()
96 .cross_signing_status()
97 .await
98.expect("We should be able to get our cross-signing status");
99100if status.is_complete() {
101println!("Successfully imported all the cross-signing keys");
102 } else {
103eprintln!("Couldn't import all the cross-signing keys: {status:?}");
104 }
105106Ok(())
107}
108109async fn restore_client(cli: &Cli) -> Result<Client> {
110let builder = Client::builder().homeserver_url(&cli.homeserver);
111112let builder = if let Some(proxy) = cli.proxy.as_ref() { builder.proxy(proxy) } else { builder };
113let client = builder.build().await?;
114115// TODO: We should be able to get the device id from `/whoami`.
116let session = AuthSession::Matrix(MatrixSession {
117 meta: SessionMeta { user_id: cli.user_id.to_owned(), device_id: cli.device_id.to_owned() },
118 tokens: SessionTokens { access_token: cli.access_token.to_owned(), refresh_token: None },
119 });
120121 client.restore_session(session).await?;
122123Ok(client)
124}
125126#[tokio::main]
127async fn main() -> Result<()> {
128let cli = Cli::parse();
129130if cli.verbose {
131 tracing_subscriber::fmt::init();
132 }
133134let client = restore_client(&cli).await?;
135let secret_store =
136 client.encryption().secret_storage().open_secret_store(&cli.secret_store_key).await?;
137138match cli.command {
139 Commands::GetSecret { secret_name } => get_secret(secret_store, secret_name).await,
140 Commands::SetSecret { secret_name, secret } => {
141 set_secret(secret_store, secret_name, &secret).await
142}
143 Commands::ImportKnownSecrets => import_known_secrets(client, secret_store).await,
144 }
145}