1/*
2* Copyright (c) 2011-2018 Alecaddd (http://alecaddd.com)
3*
4* This program is free software; you can redistribute it and/or
5* modify it under the terms of the GNU General Public
6* License as published by the Free Software Foundation; either
7* version 2 of the License, or (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12* General Public License for more details.
13*
14* You should have received a copy of the GNU General Public
15* License along with this program; if not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17* Boston, MA 02110-1301 USA
18*
19* Authored by: Alessandro "Alecaddd" Castellani <castellani.ale@gmail.com>
20*/
21
22public class Sequeler.Services.PasswordManager : Object {
23
24    // Store Password Async
25    public async void store_password_async (string id, string password) throws Error {
26        var attributes = new GLib.HashTable<string, string> (str_hash, str_equal);
27        attributes["id"] = id;
28        attributes["schema"] = Constants.PROJECT_NAME;
29
30        var key_name = Constants.PROJECT_NAME + "." + id;
31
32        bool result = yield Secret.password_storev (schema, attributes, Secret.COLLECTION_DEFAULT, key_name, password, null);
33
34        if (! result) {
35            debug ("Unable to store password for \"%s\" in libsecret keyring", key_name);
36        }
37    }
38
39    // Get Password Async
40    public async string? get_password_async (string id) throws Error {
41        var attributes = new GLib.HashTable<string, string> (str_hash, str_equal);
42        attributes["id"] = id;
43        attributes["schema"] = Constants.PROJECT_NAME;
44
45        var key_name = Constants.PROJECT_NAME + "." + id;
46
47        string? password = yield Secret.password_lookupv (schema, attributes, null);
48
49        if (password == null) {
50            debug ("Unable to fetch password in libsecret keyring for %s", key_name);
51        }
52
53        return password;
54    }
55
56    // Delete Password Async
57    public async void clear_password_async (string id) throws Error {
58        var attributes = new GLib.HashTable<string, string> (str_hash, str_equal);
59        attributes["id"] = id;
60        attributes["schema"] = Constants.PROJECT_NAME;
61
62        var key_name = Constants.PROJECT_NAME + "." + id;
63
64        bool removed = yield Secret.password_clearv (schema, attributes, null);
65
66        if (! removed) {
67            debug ("Unable to clear password in libsecret keyring for %s", key_name);
68        }
69    }
70
71    // Delete All Passwords
72    public async void clear_all_passwords_async () throws Error {
73        var attributes = new GLib.HashTable<string, string> (str_hash, str_equal);
74        attributes["schema"] = Constants.PROJECT_NAME;
75
76        bool removed = yield Secret.password_clearv (schema, attributes, null);
77
78        if (! removed) {
79            debug ("Unable to clear all passwords in libsecret");
80        }
81    }
82}
83