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
use anyhow::{Context, Result};
use diesel::prelude::*;
use gtk::{
    glib::{self, clone},
    prelude::*,
    subclass::prelude::*,
};

use crate::{
    models::{database, keyring, DieselProvider, Method, OTPUri, Provider, OTP, RUNTIME},
    schema::accounts,
    utils::spawn_tokio_blocking,
};

#[derive(Insertable)]
#[diesel(table_name = accounts)]
struct NewAccount {
    pub name: String,
    pub token_id: String,
    pub provider_id: i32,
    pub counter: i32,
}

#[derive(Identifiable, Queryable, Associations)]
#[diesel(belongs_to(DieselProvider, foreign_key = provider_id))]
#[diesel(table_name = accounts)]
pub struct DieselAccount {
    pub id: i32,
    pub name: String,
    pub counter: i32,
    pub token_id: String,
    pub provider_id: i32,
}

#[doc(hidden)]
mod imp {
    use std::cell::{Cell, OnceCell, RefCell};

    use glib::ParamSpecObject;
    use once_cell::sync::Lazy;

    use super::*;

    #[derive(glib::Properties)]
    #[properties(wrapper_type = super::Account)]
    pub struct Account {
        #[property(get, set, construct)]
        pub id: Cell<u32>,
        #[property(get, set)]
        pub code: RefCell<String>,
        #[property(get, set = Self::set_name)]
        pub name: RefCell<String>,
        #[property(get, set = Self::set_counter, default = OTP::DEFAULT_COUNTER)]
        pub counter: Cell<u32>,
        pub otp: OnceCell<OTP>,
        #[property(get, set, construct_only)]
        pub token_id: RefCell<String>,
        // We don't use property here as we can't mark the getter as not nullable
        pub provider: RefCell<Option<Provider>>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for Account {
        const NAME: &'static str = "Account";
        type Type = super::Account;

        fn new() -> Self {
            Self {
                id: Cell::default(),
                counter: Cell::new(OTP::DEFAULT_COUNTER),
                name: RefCell::default(),
                code: RefCell::default(),
                token_id: RefCell::default(),
                provider: RefCell::default(),
                otp: OnceCell::default(),
            }
        }
    }

    impl ObjectImpl for Account {
        fn properties() -> &'static [glib::ParamSpec] {
            static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
                let mut props = Account::derived_properties().to_vec();
                props.push(ParamSpecObject::builder::<Provider>("provider").build());
                props
            });
            PROPERTIES.as_ref()
        }

        fn set_property(&self, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
            match pspec.name() {
                "provider" => {
                    let provider = value.get().unwrap();
                    self.provider.replace(provider);
                }
                _ => self.derived_set_property(id, value, pspec),
            }
        }

        fn property(&self, id: usize, pspec: &glib::ParamSpec) -> glib::Value {
            match pspec.name() {
                "provider" => self.provider.borrow().to_value(),
                _ => self.derived_property(id, pspec),
            }
        }
    }

    impl Account {
        fn set_name_inner(&self, id: i32, name: &str) -> Result<()> {
            let db = database::connection();
            let mut conn = db.get()?;

            let target = accounts::table.filter(accounts::columns::id.eq(id));
            diesel::update(target)
                .set(accounts::columns::name.eq(name))
                .execute(&mut conn)?;
            Ok(())
        }

        fn set_name(&self, name: &str) {
            match self.set_name_inner(self.obj().id() as i32, name) {
                Ok(_) => {
                    self.name.replace(name.to_owned());
                }
                Err(err) => {
                    tracing::warn!("Failed to update account name {err}");
                }
            }
        }

        fn set_counter_inner(&self, id: i32, counter: u32) -> Result<()> {
            let db = database::connection();
            let mut conn = db.get()?;

            let target = accounts::table.filter(accounts::columns::id.eq(id));
            diesel::update(target)
                .set(accounts::columns::counter.eq(counter as i32))
                .execute(&mut conn)?;
            Ok(())
        }

        fn set_counter(&self, counter: u32) {
            match self.set_counter_inner(self.obj().id() as i32, counter) {
                Ok(_) => {
                    self.counter.set(counter);
                }
                Err(err) => {
                    tracing::warn!("Failed to update account counter {err}");
                }
            }
        }
    }
}

glib::wrapper! {
    pub struct Account(ObjectSubclass<imp::Account>);
}

impl Account {
    pub fn create(
        name: &str,
        token: &str,
        counter: Option<u32>,
        provider: &Provider,
    ) -> Result<Account> {
        let db = database::connection();
        let mut conn = db.get()?;

        let label = format!("{} - {name}", provider.name());
        let token_send = token.to_owned();
        let token_id = spawn_tokio_blocking(async move {
            keyring::store(&label, &token_send)
                .await
                .context("Failed to save token")
        })?;

        diesel::insert_into(accounts::table)
            .values(NewAccount {
                name: name.to_string(),
                token_id,
                provider_id: provider.id() as i32,
                counter: counter.unwrap_or_else(|| provider.default_counter()) as i32,
            })
            .execute(&mut conn)?;

        accounts::table
            .order(accounts::columns::id.desc())
            .first::<DieselAccount>(&mut conn)
            .map_err(From::from)
            .map(|account| {
                Self::new(
                    account.id as u32,
                    &account.name,
                    &account.token_id,
                    account.counter as u32,
                    provider,
                    Some(token),
                )
                .unwrap()
            })
    }

    pub fn load(p: &Provider) -> Result<impl Iterator<Item = Self>> {
        let db = database::connection();
        let mut conn = db.get()?;

        let dip = DieselProvider::from(p);
        let results = DieselAccount::belonging_to(&dip)
            .load::<DieselAccount>(&mut conn)?
            .into_iter()
            .filter_map(clone!(@strong p => move |account| {
                match Self::new(
                    account.id  as u32,
                    &account.name,
                    &account.token_id,
                    account.counter as u32,
                    &p,
                    None,
                )
                {
                    Ok(account) => Some(account),
                    Err(e) => {
                        let name = account.name;
                        let provider = p.name();
                        tracing::error!("Failed to load account '{name}' / '{provider}' with error {e}");
                        None
                    }
                }
            }));

        Ok(results)
    }

    pub fn new(
        id: u32,
        name: &str,
        token_id: &str,
        counter: u32,
        provider: &Provider,
        secret: Option<&str>,
    ) -> Result<Account> {
        let account = glib::Object::builder::<Self>()
            .property("id", id)
            .property("name", name)
            .property("token-id", token_id)
            .property("provider", provider)
            .property("counter", counter)
            .build();

        let secret = if let Some(t) = secret {
            t.to_string()
        } else {
            let token_id = token_id.to_owned();
            spawn_tokio_blocking(async move {
                keyring::token(&token_id).await?.with_context(|| {
                    format!("Could not get item with token identifier '{token_id}' from keyring")
                })
            })?
        };
        let otp = OTP::from_str(&secret, provider.algorithm(), provider.digits())?;
        account.imp().otp.set(otp).unwrap();
        account.generate_otp();
        Ok(account)
    }

    pub fn generate_otp(&self) {
        let provider = self.provider();

        let otp_password = match provider.method() {
            Method::Steam => self.otp().steam(None),
            Method::TOTP => self.otp().totp_formatted(Some(provider.period())),
            Method::HOTP => self.otp().hotp_formatted(self.counter() as u64),
        };

        let label = match otp_password {
            Ok(password) => password,
            Err(err) => {
                tracing::warn!("Failed to generate the OTP {}", err);
                "Error".to_string()
            }
        };

        self.set_code(label);
    }

    /// Increment the internal counter in case of a HOTP account
    pub fn increment_counter(&self) -> Result<()> {
        let new_value = self.counter() + 1;
        self.imp().counter.set(new_value);

        let db = database::connection();
        let mut conn = db.get()?;

        let target = accounts::table.filter(accounts::columns::id.eq(self.id() as i32));
        diesel::update(target)
            .set(accounts::columns::counter.eq(new_value as i32))
            .execute(&mut conn)?;
        Ok(())
    }

    pub fn copy_otp(&self) {
        let display = gtk::gdk::Display::default().unwrap();
        let clipboard = display.clipboard();
        // The codes come with the white space shown in the label.
        let code = self.code().replace(' ', "");
        clipboard.set_text(&code);

        // Indirectly increment the counter once the token was copied
        if self.provider().method().is_event_based() {
            self.generate_otp();
        }
    }

    pub fn provider(&self) -> Provider {
        self.imp().provider.borrow().clone().unwrap()
    }

    pub fn set_provider(&self, provider: &Provider) -> Result<()> {
        let db = database::connection();
        let mut conn = db.get()?;

        let target = accounts::table.filter(accounts::columns::id.eq(self.id() as i32));
        diesel::update(target)
            .set(accounts::columns::provider_id.eq(provider.id() as i32))
            .execute(&mut conn)?;
        self.imp().provider.replace(Some(provider.clone()));
        self.notify("provider");
        Ok(())
    }

    pub fn otp(&self) -> &OTP {
        self.imp().otp.get().unwrap()
    }

    pub fn otp_uri(&self) -> OTPUri {
        self.into()
    }

    pub fn delete(&self) -> Result<()> {
        let token_id = self.token_id();
        RUNTIME.spawn(async move {
            if let Err(err) = keyring::remove_token(&token_id).await {
                tracing::error!("Failed to remove the token from secret service {}", err);
            }
        });
        let db = database::connection();
        let mut conn = db.get()?;
        diesel::delete(accounts::table.filter(accounts::columns::id.eq(self.id() as i32)))
            .execute(&mut conn)?;
        Ok(())
    }
}