darkwing/server/services/browser_profile_services/config/
passwords.rs

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
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use super::{consts::WEBSITE_URLS, FromStartRequest, Navigator, Screen};
use crate::{
  database::browser_profile::MainWebsite,
  server::{
    dtos::{
      browser_profile_dto::BrowserProfileFullData, start_dto::StartRequest,
    },
    error::Error,
  },
};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Password {
  pub(super) username: String,
  pub(super) password: String,
}

/// Map of website URLs to passwords.
pub type Passwords = HashMap<String, Password>;

impl From<(String, String)> for Password {
  fn from((login, password): (String, String)) -> Self {
    Password {
      username: login,
      password,
    }
  }
}

impl FromStartRequest<Option<Passwords>> for Option<Passwords> {
  fn from_start_request(
    bp: &BrowserProfileFullData,
    _request: &StartRequest,
    _navigator: &Navigator,
    _screen: &Screen,
    _token: &str,
  ) -> Result<Self, Error> {
    assert_all_websites_are_present();

    match (bp.login.clone(), bp.password.clone()) {
      (Some(login), Some(password)) => match WEBSITE_URLS.get(&bp.main_website)
      {
        Some(url) => {
          let mut map = HashMap::new();
          map.insert(url.to_string(), (login, password).into());
          Ok(Some(map))
        }
        None => Ok(None),
      },
      _ => Ok(None),
    }
  }
}

fn assert_all_websites_are_present() {
  #[cfg(any(debug_assertions, test))]
  {
    let mut contains = true;
    let all_websites = MainWebsite::all().into_iter();

    for website in all_websites {
      contains &= WEBSITE_URLS.contains_key(&website);
    }

    assert!(contains);
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_assert_all_websites_are_present() {
    assert_all_websites_are_present();
  }
}