aboutsummaryrefslogtreecommitdiffstats
path: root/src/actions.rs
blob: 634fa9ab138ada096c8eb2218aa9d6d7af0186bd (plain) (blame)
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
use crate::prelude::*;

pub async fn login(
    email: &str,
    password: &crate::locked::Password,
) -> Result<(String, String, u32, String, crate::locked::Keys)> {
    let config = crate::config::Config::load_async().await?;
    let client =
        crate::api::Client::new(&config.base_url(), &config.identity_url());

    let iterations = client.prelogin(email).await?;
    let identity =
        crate::identity::Identity::new(email, password, iterations)?;

    let (access_token, refresh_token, protected_key) = client
        .login(&identity.email, &identity.master_password_hash)
        .await?;
    let master_keys = crate::cipherstring::CipherString::new(&protected_key)?
        .decrypt_locked(&identity.keys)?;

    Ok((
        access_token,
        refresh_token,
        iterations,
        protected_key,
        crate::locked::Keys::new(master_keys),
    ))
}

pub async fn unlock(
    email: &str,
    password: &crate::locked::Password,
    iterations: u32,
    protected_key: &str,
) -> Result<crate::locked::Keys> {
    let identity =
        crate::identity::Identity::new(email, password, iterations)?;

    let protected_key =
        crate::cipherstring::CipherString::new(protected_key)?;
    let master_keys = protected_key.decrypt_locked(&identity.keys)?;

    Ok(crate::locked::Keys::new(master_keys))
}

pub async fn sync(
    access_token: &str,
    refresh_token: &str,
) -> Result<(Option<String>, String, Vec<crate::db::Entry>)> {
    let res = sync_once(access_token).await;
    match res {
        Ok((protected_key, ciphers)) => Ok((None, protected_key, ciphers)),
        Err(crate::error::Error::RequestUnauthorized) => {
            let access_token =
                exchange_refresh_token_async(refresh_token).await?;
            let (protected_key, ciphers) = sync_once(&access_token).await?;
            Ok((Some(access_token), protected_key, ciphers))
        }
        Err(e) => Err(e),
    }
}

async fn sync_once(
    access_token: &str,
) -> Result<(String, Vec<crate::db::Entry>)> {
    let config = crate::config::Config::load_async().await?;
    let client =
        crate::api::Client::new(&config.base_url(), &config.identity_url());
    client.sync(access_token).await
}

pub fn add(
    access_token: &str,
    refresh_token: &str,
    name: &str,
    username: Option<&str>,
    password: Option<&str>,
    notes: Option<&str>,
) -> Result<Option<String>> {
    match add_once(access_token, name, username, password, notes) {
        Ok(()) => Ok(None),
        Err(crate::error::Error::RequestUnauthorized) => {
            let access_token = exchange_refresh_token(refresh_token)?;
            add_once(&access_token, name, username, password, notes)?;
            Ok(Some(access_token))
        }
        Err(e) => Err(e),
    }
}

fn add_once(
    access_token: &str,
    name: &str,
    username: Option<&str>,
    password: Option<&str>,
    notes: Option<&str>,
) -> Result<()> {
    let config = crate::config::Config::load()?;
    let client =
        crate::api::Client::new(&config.base_url(), &config.identity_url());
    client.add(access_token, name, username, password, notes)?;
    Ok(())
}

pub fn edit(
    access_token: &str,
    refresh_token: &str,
    id: &str,
    name: &str,
    username: Option<&str>,
    password: Option<&str>,
    notes: Option<&str>,
) -> Result<Option<String>> {
    match edit_once(access_token, id, name, username, password, notes) {
        Ok(()) => Ok(None),
        Err(crate::error::Error::RequestUnauthorized) => {
            let access_token = exchange_refresh_token(refresh_token)?;
            edit_once(&access_token, id, name, username, password, notes)?;
            Ok(Some(access_token))
        }
        Err(e) => Err(e),
    }
}

fn edit_once(
    access_token: &str,
    id: &str,
    name: &str,
    username: Option<&str>,
    password: Option<&str>,
    notes: Option<&str>,
) -> Result<()> {
    let config = crate::config::Config::load()?;
    let client =
        crate::api::Client::new(&config.base_url(), &config.identity_url());
    client.edit(access_token, id, name, username, password, notes)?;
    Ok(())
}

pub fn remove(
    access_token: &str,
    refresh_token: &str,
    id: &str,
) -> Result<Option<String>> {
    match remove_once(access_token, id) {
        Ok(()) => Ok(None),
        Err(crate::error::Error::RequestUnauthorized) => {
            let access_token = exchange_refresh_token(refresh_token)?;
            remove_once(&access_token, id)?;
            Ok(Some(access_token))
        }
        Err(e) => Err(e),
    }
}

fn remove_once(access_token: &str, id: &str) -> Result<()> {
    let config = crate::config::Config::load()?;
    let client =
        crate::api::Client::new(&config.base_url(), &config.identity_url());
    client.remove(access_token, id)?;
    Ok(())
}

fn exchange_refresh_token(refresh_token: &str) -> Result<String> {
    let config = crate::config::Config::load()?;
    let client =
        crate::api::Client::new(&config.base_url(), &config.identity_url());
    client.exchange_refresh_token(refresh_token)
}

async fn exchange_refresh_token_async(refresh_token: &str) -> Result<String> {
    let config = crate::config::Config::load_async().await?;
    let client =
        crate::api::Client::new(&config.base_url(), &config.identity_url());
    client.exchange_refresh_token_async(refresh_token).await
}