sos-database 0.17.5

Database backend for the Save Our Secrets SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use crate::{
    entity::{FolderEntity, FolderRecord, SecretRow},
    Error, Result,
};
use async_sqlite::{
    rusqlite::{
        Connection, Error as SqlError, OptionalExtension, Row, Transaction,
    },
    Client,
};
use sos_core::{AccountId, PublicIdentity, UtcDateTime, VaultCommit};
use sos_vault::Vault;
use sql_query_builder as sql;
use std::ops::Deref;

use super::FolderRow;

/// Account row from the database.
#[doc(hidden)]
#[derive(Debug, Default)]
pub struct AccountRow {
    /// Row identifier.
    pub row_id: i64,
    /// RFC3339 date and time.
    created_at: String,
    /// RFC3339 date and time.
    modified_at: String,
    /// Account identifier.
    identifier: String,
    /// Account name.
    name: String,
}

impl AccountRow {
    /// Create an account row for insertion.
    pub fn new_insert(account_id: &AccountId, name: String) -> Result<Self> {
        Ok(AccountRow {
            identifier: account_id.to_string(),
            name,
            created_at: UtcDateTime::default().to_rfc3339()?,
            modified_at: UtcDateTime::default().to_rfc3339()?,
            ..Default::default()
        })
    }
}

impl<'a> TryFrom<&Row<'a>> for AccountRow {
    type Error = SqlError;
    fn try_from(row: &Row<'a>) -> std::result::Result<Self, Self::Error> {
        Ok(AccountRow {
            row_id: row.get(0)?,
            created_at: row.get(1)?,
            modified_at: row.get(2)?,
            identifier: row.get(3)?,
            name: row.get(4)?,
        })
    }
}

/// Account record from the database.
#[derive(Debug)]
pub struct AccountRecord {
    /// Row identifier.
    pub row_id: i64,
    /// Created date and time.
    pub created_at: UtcDateTime,
    /// Modified date and time.
    pub modified_at: UtcDateTime,
    /// Account identity.
    pub identity: PublicIdentity,
}

impl TryFrom<AccountRow> for AccountRecord {
    type Error = Error;

    fn try_from(value: AccountRow) -> std::result::Result<Self, Self::Error> {
        let created_at = UtcDateTime::parse_rfc3339(&value.created_at)?;
        let modified_at = UtcDateTime::parse_rfc3339(&value.modified_at)?;
        let account_id: AccountId = value.identifier.parse()?;
        Ok(AccountRecord {
            row_id: value.row_id,
            created_at,
            modified_at,
            identity: PublicIdentity::new(account_id, value.name),
        })
    }
}

/// Account entity.
pub struct AccountEntity<'conn, C>
where
    C: Deref<Target = Connection>,
{
    conn: &'conn C,
}

impl<'conn> AccountEntity<'conn, Box<Connection>> {
    /// Liat all accounts.
    pub async fn list_all_accounts(
        client: &Client,
    ) -> Result<Vec<AccountRecord>> {
        let account_rows = client
            .conn_and_then(move |conn| {
                let account = AccountEntity::new(&conn);
                account.list_accounts()
            })
            .await?;

        let mut accounts = Vec::new();
        for row in account_rows {
            accounts.push(row.try_into()?);
        }
        Ok(accounts)
    }

    /// Find an account and login folder.
    pub async fn find_account_with_login(
        client: &Client,
        account_id: &AccountId,
    ) -> Result<(AccountRecord, FolderRecord)> {
        let (account, folder_row) =
            Self::find_account_with_login_optional(client, account_id)
                .await?;

        let account_id = account.row_id;
        Ok((
            account,
            folder_row.ok_or_else(|| Error::NoLoginFolder(account_id))?,
        ))
    }

    /// Find an account and optional login folder.
    pub async fn find_account_with_login_optional(
        client: &Client,
        account_id: &AccountId,
    ) -> Result<(AccountRecord, Option<FolderRecord>)> {
        let account_id = *account_id;
        let (account_row, folder_row) = client
            .conn_and_then(move |conn| {
                let account = AccountEntity::new(&conn);
                let account_row = account.find_one(&account_id)?;
                let folders = FolderEntity::new(&conn);
                let folder_row =
                    folders.find_login_folder_optional(account_row.row_id)?;
                Ok::<_, Error>((account_row, folder_row))
            })
            .await?;

        let login_folder = if let Some(folder_row) = folder_row {
            Some(FolderRecord::from_row(folder_row).await?)
        } else {
            None
        };
        Ok((account_row.try_into()?, login_folder))
    }
}

impl<'conn> AccountEntity<'conn, Transaction<'conn>> {
    /// Upsert the login folder.
    pub async fn upsert_login_folder(
        client: &Client,
        account_id: &AccountId,
        vault: &Vault,
    ) -> Result<(AccountRecord, i64)> {
        // Check if we already have a login folder
        let (account, folder) =
            AccountEntity::find_account_with_login_optional(
                client, account_id,
            )
            .await?;

        // TODO: folder creation and join should be merged into a single
        // TODO: transaction

        // Create or update the folder and secrets
        let (folder_row_id, _) = FolderEntity::upsert_folder_and_secrets(
            client,
            account.row_id,
            vault,
        )
        .await?;

        let account_row_id = account.row_id;

        // Update or insert the join
        if folder.is_some() {
            client
                .conn(move |conn| {
                    let account_entity = AccountEntity::new(&conn);
                    account_entity
                        .update_login_folder(account_row_id, folder_row_id)
                })
                .await?;
        } else {
            client
                .conn(move |conn| {
                    let account_entity = AccountEntity::new(&conn);
                    account_entity
                        .insert_login_folder(account_row_id, folder_row_id)
                })
                .await?;
        }

        Ok((account, folder_row_id))
    }

    /// Replace the login folder.
    pub async fn replace_login_folder(
        client: &mut Client,
        account_id: &AccountId,
        vault: &Vault,
    ) -> Result<()> {
        // Check if we already have a login folder
        let (account, login_folder) =
            AccountEntity::find_account_with_login(client, account_id)
                .await?;

        let login_folder_id = *login_folder.summary.id();
        let new_login_folder = FolderRow::new_insert(vault).await?;

        let mut secret_rows = Vec::new();
        for (secret_id, commit) in vault.iter() {
            let VaultCommit(commit, entry) = commit;
            secret_rows.push(SecretRow::new(secret_id, commit, entry).await?);
        }

        client
            .conn_mut_and_then(move |conn| {
                let tx = conn.transaction()?;
                let account_entity = AccountEntity::new(&tx);
                let folder_entity = FolderEntity::new(&tx);

                // Delete the old folder
                folder_entity.delete_folder(&login_folder_id)?;

                // Create the new folder
                let folder_row_id = folder_entity
                    .insert_folder(account.row_id, &new_login_folder)?;

                // Insert the secrets
                folder_entity.insert_folder_secrets(
                    folder_row_id,
                    secret_rows.as_slice(),
                )?;

                // Update the join
                account_entity
                    .update_login_folder(account.row_id, folder_row_id)?;

                tx.commit()?;
                Ok::<_, Error>(())
            })
            .await?;

        Ok(())
    }
}

impl<'conn, C> AccountEntity<'conn, C>
where
    C: Deref<Target = Connection>,
{
    /// Create a new account entity.
    pub fn new(conn: &'conn C) -> Self {
        Self { conn }
    }

    fn account_select_columns(&self, sql: sql::Select) -> sql::Select {
        sql.select(
            r#"
                account_id,
                created_at,
                modified_at,
                identifier,
                name
            "#,
        )
    }

    /// Find an account in the database.
    pub fn find_one(
        &self,
        account_id: &AccountId,
    ) -> std::result::Result<AccountRow, SqlError> {
        let query = self
            .account_select_columns(sql::Select::new())
            .from("accounts")
            .where_clause("identifier = ?1");
        let mut stmt = self.conn.prepare_cached(&query.as_string())?;
        stmt.query_row([account_id.to_string()], |row| row.try_into())
    }

    /// Find an optional account in the database.
    pub fn find_optional(
        &self,
        account_id: &AccountId,
    ) -> std::result::Result<Option<AccountRow>, SqlError> {
        let query = self
            .account_select_columns(sql::Select::new())
            .from("accounts")
            .where_clause("identifier = ?1");
        let mut stmt = self.conn.prepare_cached(&query.as_string())?;
        stmt.query_row([account_id.to_string()], |row| row.try_into())
            .optional()
    }

    /// List accounts.
    pub fn list_accounts(&self) -> Result<Vec<AccountRow>> {
        let query = self
            .account_select_columns(sql::Select::new())
            .from("accounts");

        let mut stmt = self.conn.prepare_cached(&query.as_string())?;

        fn convert_row(row: &Row<'_>) -> Result<AccountRow> {
            Ok(row.try_into()?)
        }

        let rows = stmt.query_and_then([], convert_row)?;
        let mut accounts = Vec::new();
        for row in rows {
            accounts.push(row?);
        }
        Ok(accounts)
    }

    /// Create the account entity in the database.
    pub fn insert(
        &self,
        row: &AccountRow,
    ) -> std::result::Result<i64, SqlError> {
        let query = sql::Insert::new()
            .insert_into(
                "accounts (created_at, modified_at, identifier, name)",
            )
            .values("(?1, ?2, ?3, ?4)");
        self.conn.execute(
            &query.as_string(),
            (
                &row.created_at,
                &row.modified_at,
                &row.identifier,
                &row.name,
            ),
        )?;
        Ok(self.conn.last_insert_rowid())
    }

    /// Create the join for the account login folder.
    pub fn insert_login_folder(
        &self,
        account_id: i64,
        folder_id: i64,
    ) -> std::result::Result<i64, SqlError> {
        let query = sql::Insert::new()
            .insert_into("account_login_folder (account_id, folder_id)")
            .values("(?1, ?2)");
        self.conn
            .execute(&query.as_string(), [account_id, folder_id])?;
        Ok(self.conn.last_insert_rowid())
    }

    /// Update the join for an account login folder.
    pub fn update_login_folder(
        &self,
        account_id: i64,
        folder_id: i64,
    ) -> std::result::Result<(), SqlError> {
        let query = sql::Update::new()
            .update("account_login_folder")
            .set("folder_id = ?2")
            .where_clause("account_id = ?1");
        self.conn
            .execute(&query.as_string(), [account_id, folder_id])?;
        Ok(())
    }

    /// Create the join for the account device folder.
    pub fn insert_device_folder(
        &self,
        account_id: i64,
        folder_id: i64,
    ) -> std::result::Result<i64, SqlError> {
        let query = sql::Insert::new()
            .insert_into("account_device_folder (account_id, folder_id)")
            .values("(?1, ?2)");
        self.conn
            .execute(&query.as_string(), [account_id, folder_id])?;
        Ok(self.conn.last_insert_rowid())
    }

    /// Rename the account.
    pub fn rename_account(&self, account_id: i64, name: &str) -> Result<()> {
        let modified_at = UtcDateTime::default().to_rfc3339()?;
        let query = sql::Update::new()
            .update("accounts")
            .set("name = ?1, modified_at = ?2")
            .where_clause("account_id = ?3");
        let mut stmt = self.conn.prepare_cached(&query.as_string())?;
        stmt.execute((name, modified_at, account_id))?;
        Ok(())
    }

    /// Delete the account from the database.
    pub fn delete_account(
        &self,
        account_id: &AccountId,
    ) -> std::result::Result<(), SqlError> {
        let query = sql::Delete::new()
            .delete_from("accounts")
            .where_clause("identifier = ?1");
        self.conn
            .execute(&query.as_string(), [account_id.to_string()])?;
        Ok(())
    }
}