Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for locked/encrypted accounts to jsonrpc api #6141

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions deltachat-jsonrpc/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,20 @@ impl CommandApi {
// Account Management
// ---------------------------------------------

/// Adds a new account.
async fn add_account(&self) -> Result<u32> {
self.accounts.write().await.add_account().await
}

/// Adds a new closed/encrypted account.
///
/// To use it it must be opened first with `open`.
/// The first call to `open` after the creation will set the passphrase.
/// It can later be changed with `change_passphrase`.
async fn add_closed_account(&self) -> Result<u32> {
self.accounts.write().await.add_closed_account().await
}

/// Imports/migrated an existing account from a database path into this account manager.
/// Returns the ID of new account.
async fn migrate_account(&self, path_to_db: String) -> Result<u32> {
Expand Down Expand Up @@ -337,6 +347,36 @@ impl CommandApi {
ctx.is_configured().await
}

/// Checks if the database of the context is open (encrypted accounts are closed unless opened with their passphrase).
///
/// Closed accounts need to be opened with `open(account_id: u32, passphrase: String)`
async fn is_open(&self, account_id: u32) -> Result<bool> {
let ctx = self.get_context(account_id).await?;
Ok(ctx.is_open().await)
}

/// Changes the passphrase on the open database.
///
/// Existing database must already be encrypted and the passphrase cannot be empty.
///
/// It is impossible to encrypt unencrypted database with this method and vice versa.
async fn change_passphrase(&self, account_id: u32, passphrase: String) -> Result<()> {
let ctx = self.get_context(account_id).await?;
ctx.change_passphrase(passphrase).await
}

/// Opens the database with the given passphrase.
///
/// This can only be used on closed context, such as created by `add_closed_account`.
/// If the database is new, this operation sets the database passphrase.
/// For existing databases the passphrase should be the one used to encrypt the database the first time.
///
/// Returns true if passphrase is correct, false is passphrase is not correct. Fails on other errors.
async fn open(&self, account_id: u32, passphrase: String) -> Result<bool> {
let ctx = self.get_context(account_id).await?;
ctx.open(passphrase).await
}

/// Get system info for an account.
async fn get_info(&self, account_id: u32) -> Result<BTreeMap<&'static str, String>> {
let ctx = self.get_context(account_id).await?;
Expand Down
5 changes: 5 additions & 0 deletions deltachat-jsonrpc/src/api/types/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ pub enum Account {
},
#[serde(rename_all = "camelCase")]
Unconfigured { id: u32 },
#[serde(rename_all = "camelCase")]
Locked { id: u32 },
}

impl Account {
pub async fn from_context(ctx: &deltachat::context::Context, id: u32) -> Result<Self> {
if !ctx.is_open().await {
return Ok(Account::Locked { id });
}
if ctx.is_configured().await? {
let display_name = ctx.get_config(Config::Displayname).await?;
let addr = ctx.get_config(Config::Addr).await?;
Expand Down
Loading