sos-migrate 0.17.1

Import and export 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! Parser for the Dashlane CSV zip export.

use async_trait::async_trait;
use serde::Deserialize;
use sos_core::{crypto::AccessKey, UtcDateTime};
use sos_vault::{secret::IdentityKind, Vault};
use sos_vfs as vfs;
use std::{
    collections::HashSet,
    io::Cursor,
    path::{Path, PathBuf},
};
use time::{Date, Month};
use url::Url;
use vcard4::{property::DeliveryAddress, Uri, VcardBuilder};

use async_zip::tokio::read::seek::ZipFileReader;
use tokio::io::{AsyncBufRead, AsyncSeek, BufReader};

use super::{
    GenericContactRecord, GenericCsvConvert, GenericCsvEntry,
    GenericIdRecord, GenericNoteRecord, GenericPasswordRecord,
    GenericPaymentRecord, UNTITLED,
};
use crate::{import::read_csv_records, Convert, Result};

/// Record used to deserialize dashlane CSV files.
#[derive(Debug)]
pub enum DashlaneRecord {
    /// Password login.
    Password(DashlanePasswordRecord),
    /// Secure note.
    Note(DashlaneNoteRecord),
    /// Identity record.
    Id(DashlaneIdRecord),
    /// Payment record.
    Payment(DashlanePaymentRecord),
    /// Contact record.
    Contact(DashlaneContactRecord),
}

impl From<DashlaneRecord> for GenericCsvEntry {
    fn from(value: DashlaneRecord) -> Self {
        match value {
            DashlaneRecord::Password(record) => {
                GenericCsvEntry::Password(record.into())
            }
            DashlaneRecord::Note(record) => {
                GenericCsvEntry::Note(record.into())
            }
            DashlaneRecord::Id(record) => GenericCsvEntry::Id(record.into()),
            DashlaneRecord::Payment(record) => {
                GenericCsvEntry::Payment(record.into())
            }
            DashlaneRecord::Contact(record) => {
                GenericCsvEntry::Contact(Box::new(record.into()))
            }
        }
    }
}

/// Record for an entry in a Dashlane notes CSV export.
#[derive(Debug, Deserialize)]
pub struct DashlaneNoteRecord {
    /// The title of the entry.
    pub title: String,
    /// The note for the entry.
    pub note: String,
}

impl From<DashlaneNoteRecord> for DashlaneRecord {
    fn from(value: DashlaneNoteRecord) -> Self {
        Self::Note(value)
    }
}

impl From<DashlaneNoteRecord> for GenericNoteRecord {
    fn from(value: DashlaneNoteRecord) -> Self {
        let label = if value.title.is_empty() {
            UNTITLED.to_owned()
        } else {
            value.title
        };
        Self {
            label,
            text: value.note,
            tags: None,
            note: None,
        }
    }
}

/// Record for an entry in a Dashlane id CSV export.
#[derive(Debug, Deserialize)]
pub struct DashlaneIdRecord {
    /// The type of the entry.
    #[serde(rename = "type")]
    pub kind: String,
    /// The number for the entry.
    pub number: String,
    /// The name for the entry.
    pub name: String,
    /// The issue date for the entry.
    pub issue_date: String,
    /// The expiration date for the entry.
    pub expiration_date: String,
    /// The place of issue for the entry.
    pub place_of_issue: String,
    /// The state for the entry.
    pub state: String,
}

impl From<DashlaneIdRecord> for DashlaneRecord {
    fn from(value: DashlaneIdRecord) -> Self {
        Self::Id(value)
    }
}

impl From<DashlaneIdRecord> for GenericIdRecord {
    fn from(value: DashlaneIdRecord) -> Self {
        let label = if value.name.is_empty() {
            UNTITLED.to_owned()
        } else {
            value.name
        };

        let id_kind = match &value.kind[..] {
            "card" => IdentityKind::IdCard,
            "passport" => IdentityKind::Passport,
            "license" => IdentityKind::DriverLicense,
            "social_security" => IdentityKind::SocialSecurity,
            "tax_number" => IdentityKind::TaxNumber,
            _ => {
                panic!("unsupported type of id {}", value.kind);
            }
        };

        let issue_place =
            if !value.state.is_empty() && !value.place_of_issue.is_empty() {
                format!("{}, {}", value.state, value.place_of_issue)
            } else {
                value.place_of_issue
            };

        let issue_place = if !issue_place.is_empty() {
            Some(issue_place)
        } else {
            None
        };

        let issue_date = if !value.issue_date.is_empty() {
            match UtcDateTime::parse_simple_date(&value.issue_date) {
                Ok(date) => Some(date),
                Err(_) => None,
            }
        } else {
            None
        };

        let expiration_date = if !value.expiration_date.is_empty() {
            match UtcDateTime::parse_simple_date(&value.expiration_date) {
                Ok(date) => Some(date),
                Err(_) => None,
            }
        } else {
            None
        };

        Self {
            label,
            id_kind,
            number: value.number,
            issue_place,
            issue_date,
            expiration_date,
            tags: None,
            note: None,
        }
    }
}

/// Record for an entry in a Dashlane id CSV export.
#[derive(Debug, Deserialize)]
pub struct DashlanePaymentRecord {
    /// The type of the entry.
    #[serde(rename = "type")]
    pub kind: String,
    /// The account name for the entry.
    pub account_name: String,
    /// The account holder for the entry.
    pub account_holder: String,
    /// The account number for the entry.
    pub account_number: String,
    /// The routing number for the entry.
    pub routing_number: String,
    /// The CC number for the entry.
    pub cc_number: String,
    /// The CVV code for the entry.
    pub code: String,
    /// The expiration month for the entry.
    pub expiration_month: String,
    /// The expiration year for the entry.
    pub expiration_year: String,
    /// The country for the entry.
    pub country: String,
    /// The note for the entry.
    pub note: String,
}

impl From<DashlanePaymentRecord> for DashlaneRecord {
    fn from(value: DashlanePaymentRecord) -> Self {
        Self::Payment(value)
    }
}

impl From<DashlanePaymentRecord> for GenericPaymentRecord {
    fn from(value: DashlanePaymentRecord) -> Self {
        let label = if value.account_name.is_empty() {
            UNTITLED.to_owned()
        } else {
            value.account_name
        };

        let expiration = if let (Ok(month), Ok(year)) = (
            value.expiration_month.parse::<u8>(),
            value.expiration_year.parse::<i32>(),
        ) {
            if let Ok(month) = Month::try_from(month) {
                UtcDateTime::from_calendar_date(year, month, 1).ok()
            } else {
                None
            }
        } else {
            None
        };

        let note = if !value.note.is_empty() {
            Some(value.note)
        } else {
            None
        };

        match &value.kind[..] {
            "bank" => GenericPaymentRecord::BankAccount {
                label,
                account_holder: value.account_holder,
                account_number: value.account_number,
                routing_number: value.routing_number,
                country: value.country,
                note,
                tags: None,
            },
            "payment_card" => GenericPaymentRecord::Card {
                label,
                number: value.cc_number,
                code: value.code,
                expiration,
                country: value.country,
                note,
                tags: None,
            },
            _ => panic!("unexpected payment type {}", value.kind),
        }
    }
}

/// Record for an entry in a Dashlane passwords CSV export.
#[derive(Debug, Deserialize)]
pub struct DashlanePasswordRecord {
    /// The title of the entry.
    pub title: String,
    /// The URL of the entry.
    pub url: Option<Url>,
    /// The username for the entry.
    pub username: String,
    /// The password for the entry.
    pub password: String,
    /// The note for the entry.
    pub note: String,
    /// The category for the entry.
    pub category: String,
    /// The OTP secret for the entry.
    #[serde(rename = "otpSecret")]
    pub otp_secret: String,
}

impl From<DashlanePasswordRecord> for DashlaneRecord {
    fn from(value: DashlanePasswordRecord) -> Self {
        Self::Password(value)
    }
}

impl From<DashlanePasswordRecord> for GenericPasswordRecord {
    fn from(value: DashlanePasswordRecord) -> Self {
        let label = if value.title.is_empty() {
            UNTITLED.to_owned()
        } else {
            value.title
        };

        let tags = if !value.category.is_empty() {
            let mut tags = HashSet::new();
            tags.insert(value.category);
            Some(tags)
        } else {
            None
        };

        let note = if !value.note.is_empty() {
            Some(value.note)
        } else {
            None
        };

        let url = if let Some(url) = value.url {
            vec![url]
        } else {
            vec![]
        };

        Self {
            label,
            url,
            username: value.username,
            password: value.password,
            otp_auth: None,
            tags,
            note,
        }
    }
}

/// Record for an entry in a Dashlane personalInfo CSV export.
///
/// Fields that are currently not handled:
///
/// * login
/// * place_of_birth
/// * email_type
/// * address_door_code
///
#[derive(Debug, Deserialize)]
pub struct DashlaneContactRecord {
    /// The item name of the entry.
    pub item_name: String,
    /// The title.
    pub title: String,
    /// The first name.
    pub first_name: String,
    /// The middle name.
    pub middle_name: String,
    /// The last name.
    pub last_name: String,

    /// The address.
    pub address: String,
    /// The city.
    pub city: String,
    /// The state.
    pub state: String,
    /// The country.
    pub country: String,
    /// The postal code.
    pub zip: String,

    /// Address recipient.
    pub address_recipient: String,
    /// Address apartment.
    pub address_apartment: String,
    /// Address floor.
    pub address_floor: String,
    /// Address building.
    pub address_building: String,

    /// The phone number.
    pub phone_number: String,
    /// An email address.
    pub email: String,
    /// A website URL.
    pub url: String,

    /// A date of birth.
    pub date_of_birth: String,

    /// A job title.
    pub job_title: String,
}

impl From<DashlaneContactRecord> for DashlaneRecord {
    fn from(value: DashlaneContactRecord) -> Self {
        Self::Contact(value)
    }
}

impl From<DashlaneContactRecord> for GenericContactRecord {
    fn from(value: DashlaneContactRecord) -> Self {
        let has_some_name_parts = !value.last_name.is_empty()
            || !value.first_name.is_empty()
            || !value.middle_name.is_empty()
            || !value.title.is_empty();

        let name: [String; 5] = [
            value.last_name.clone(),
            value.first_name.clone(),
            value.middle_name.clone(),
            value.title.clone(),
            String::new(),
        ];

        let formatted_name = if has_some_name_parts {
            let mut parts: Vec<String> = Vec::new();
            if !value.title.is_empty() {
                parts.push(value.title);
            }
            if !value.first_name.is_empty() {
                parts.push(value.first_name);
            }
            if !value.middle_name.is_empty() {
                parts.push(value.middle_name);
            }
            if !value.last_name.is_empty() {
                parts.push(value.last_name);
            }
            parts.join(" ")
        } else if !value.item_name.is_empty() {
            value.item_name.clone()
        } else {
            UNTITLED.to_owned()
        };

        let label = if value.item_name.is_empty() {
            formatted_name.clone()
        } else if !value.item_name.is_empty() {
            value.item_name
        } else {
            UNTITLED.to_owned()
        };

        let date_of_birth: Option<Date> = if !value.date_of_birth.is_empty() {
            if let Ok(date_time) =
                UtcDateTime::parse_simple_date(&value.date_of_birth)
            {
                Some(date_time.into_date())
            } else {
                None
            }
        } else {
            None
        };

        let url: Option<Uri> = if !value.url.is_empty() {
            value.url.parse().ok()
        } else {
            None
        };

        let extended_address = vec![
            value.address_recipient,
            value.address_apartment,
            value.address_floor,
            value.address_building,
        ];

        let has_some_address_parts = !value.address.is_empty()
            || !value.city.is_empty()
            || !value.state.is_empty()
            || !value.zip.is_empty()
            || !value.country.is_empty();

        let address = if has_some_address_parts {
            Some(DeliveryAddress {
                po_box: None,
                extended_address: if !extended_address.is_empty() {
                    Some(extended_address.join(","))
                } else {
                    None
                },
                street_address: if !value.address.is_empty() {
                    Some(value.address)
                } else {
                    None
                },
                locality: if !value.city.is_empty() {
                    Some(value.city)
                } else {
                    None
                },
                region: if !value.state.is_empty() {
                    Some(value.state)
                } else {
                    None
                },
                country_name: if !value.country.is_empty() {
                    Some(value.country)
                } else {
                    None
                },
                postal_code: if !value.zip.is_empty() {
                    Some(value.zip)
                } else {
                    None
                },
            })
        } else {
            None
        };

        let mut builder = VcardBuilder::new(formatted_name);
        if has_some_name_parts {
            builder = builder.name(name);
        }
        if let Some(address) = address {
            builder = builder.address(address);
        }
        if !value.phone_number.is_empty() {
            builder = builder.telephone(value.phone_number);
        }
        if !value.email.is_empty() {
            builder = builder.email(value.email);
        }
        if let Some(url) = url {
            builder = builder.url(url);
        }
        if !value.job_title.is_empty() {
            builder = builder.title(value.job_title);
        }
        if let Some(date) = date_of_birth {
            builder = builder.birthday(date.into());
        }
        let vcard = builder.finish();
        Self {
            label,
            vcard,
            tags: None,
            note: None,
        }
    }
}

/// Parse records from a path.
pub async fn parse_path<P: AsRef<Path>>(
    path: P,
) -> Result<Vec<DashlaneRecord>> {
    parse(BufReader::new(vfs::File::open(path.as_ref()).await?)).await
}

async fn read_entry<R: AsyncBufRead + AsyncSeek + Unpin>(
    zip: &mut ZipFileReader<R>,
    index: usize,
) -> Result<Vec<u8>> {
    let mut reader = zip.reader_with_entry(index).await?;
    let mut buffer = Vec::new();
    reader.read_to_end_checked(&mut buffer).await?;
    Ok(buffer)
}

async fn parse<R: AsyncBufRead + AsyncSeek + Unpin>(
    rdr: R,
) -> Result<Vec<DashlaneRecord>> {
    let mut records = Vec::new();
    let mut zip = ZipFileReader::with_tokio(rdr).await?;

    for index in 0..zip.file().entries().len() {
        let entry = zip.file().entries().get(index).unwrap();
        let file_name = entry.filename();
        let file_name = file_name.as_str()?;

        match file_name {
            "securenotes.csv" => {
                let mut buffer = read_entry(&mut zip, index).await?;
                let reader = Cursor::new(&mut buffer);
                let mut items: Vec<DashlaneRecord> =
                    read_csv_records::<DashlaneNoteRecord, _>(reader)
                        .await?
                        .into_iter()
                        .map(|r| r.into())
                        .collect();
                records.append(&mut items);
            }
            "credentials.csv" => {
                let mut buffer = read_entry(&mut zip, index).await?;
                let reader = Cursor::new(&mut buffer);
                let mut items: Vec<DashlaneRecord> =
                    read_csv_records::<DashlanePasswordRecord, _>(reader)
                        .await?
                        .into_iter()
                        .map(|r| r.into())
                        .collect();
                records.append(&mut items);
            }
            "ids.csv" => {
                let mut buffer = read_entry(&mut zip, index).await?;
                let reader = Cursor::new(&mut buffer);
                let mut items: Vec<DashlaneRecord> =
                    read_csv_records::<DashlaneIdRecord, _>(reader)
                        .await?
                        .into_iter()
                        .map(|r| r.into())
                        .collect();
                records.append(&mut items);
            }
            "payments.csv" => {
                let mut buffer = read_entry(&mut zip, index).await?;
                let reader = Cursor::new(&mut buffer);
                let mut items: Vec<DashlaneRecord> =
                    read_csv_records::<DashlanePaymentRecord, _>(reader)
                        .await?
                        .into_iter()
                        .map(|r| r.into())
                        .collect();
                records.append(&mut items);
            }
            "personalInfo.csv" => {
                let mut buffer = read_entry(&mut zip, index).await?;
                let reader = Cursor::new(&mut buffer);
                let mut items: Vec<DashlaneRecord> =
                    read_csv_records::<DashlaneContactRecord, _>(reader)
                        .await?
                        .into_iter()
                        .map(|r| r.into())
                        .collect();
                records.append(&mut items);
            }
            _ => {
                eprintln!(
                    "unsupported dashlane file encountered {}",
                    file_name
                );
            }
        }
    }
    Ok(records)
}

/// Import a Dashlane CSV zip archive into a vault.
pub struct DashlaneCsvZip;

#[async_trait]
impl Convert for DashlaneCsvZip {
    type Input = PathBuf;

    async fn convert(
        &self,
        source: Self::Input,
        vault: Vault,
        key: &AccessKey,
    ) -> crate::Result<Vault> {
        let records: Vec<GenericCsvEntry> = parse_path(source)
            .await?
            .into_iter()
            .map(|r| r.into())
            .collect();
        GenericCsvConvert.convert(records, vault, key).await
    }
}