This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathkeytar_posix.cc
184 lines (146 loc) · 5.4 KB
/
keytar_posix.cc
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
#include "keytar.h"
// This is needed to make the builds on Ubuntu 14.04 / libsecret v0.16 work.
// The API we use has already stabilized.
#define SECRET_API_SUBJECT_TO_CHANGE
#include <libsecret/secret.h>
#include <stdio.h>
#include <string.h>
namespace keytar {
namespace {
static const SecretSchema schema = {
"org.freedesktop.Secret.Generic", SECRET_SCHEMA_NONE, {
{ "service", SECRET_SCHEMA_ATTRIBUTE_STRING },
{ "account", SECRET_SCHEMA_ATTRIBUTE_STRING }
}
};
} // namespace
KEYTAR_OP_RESULT SetPassword(const std::string& service,
const std::string& account,
const std::string& password,
std::string* errStr) {
GError* error = NULL;
secret_password_store_sync(
&schema, // The schema.
SECRET_COLLECTION_DEFAULT, // Default collection.
(service + "/" + account).c_str(), // The label.
password.c_str(), // The password.
NULL, // Cancellable. (unneeded)
&error, // Reference to the error.
"service", service.c_str(),
"account", account.c_str(),
NULL); // End of arguments.
if (error != NULL) {
*errStr = std::string(error->message);
g_error_free(error);
return FAIL_ERROR;
}
return SUCCESS;
}
KEYTAR_OP_RESULT GetPassword(const std::string& service,
const std::string& account,
std::string* password,
std::string* errStr) {
GError* error = NULL;
gchar* raw_password = secret_password_lookup_sync(
&schema, // The schema.
NULL, // Cancellable. (unneeded)
&error, // Reference to the error.
"service", service.c_str(),
"account", account.c_str(),
NULL); // End of arguments.
if (error != NULL) {
*errStr = std::string(error->message);
g_error_free(error);
return FAIL_ERROR;
}
if (raw_password == NULL)
return FAIL_NONFATAL;
*password = raw_password;
secret_password_free(raw_password);
return SUCCESS;
}
KEYTAR_OP_RESULT DeletePassword(const std::string& service,
const std::string& account,
std::string* errStr) {
GError* error = NULL;
gboolean result = secret_password_clear_sync(
&schema, // The schema.
NULL, // Cancellable. (unneeded)
&error, // Reference to the error.
"service", service.c_str(),
"account", account.c_str(),
NULL); // End of arguments.
if (error != NULL) {
*errStr = std::string(error->message);
g_error_free(error);
return FAIL_ERROR;
}
if (!result)
return FAIL_NONFATAL;
return SUCCESS;
}
KEYTAR_OP_RESULT FindPassword(const std::string& service,
std::string* password,
std::string* errStr) {
GError* error = NULL;
gchar* raw_password = secret_password_lookup_sync(
&schema, // The schema.
NULL, // Cancellable. (unneeded)
&error, // Reference to the error.
"service", service.c_str(),
NULL); // End of arguments.
if (error != NULL) {
*errStr = std::string(error->message);
g_error_free(error);
return FAIL_ERROR;
}
if (raw_password == NULL)
return FAIL_NONFATAL;
*password = raw_password;
secret_password_free(raw_password);
return SUCCESS;
}
KEYTAR_OP_RESULT FindCredentials(const std::string& service,
std::vector<Credentials>* credentials,
std::string* errStr) {
GError* error = NULL;
GHashTable* attributes = g_hash_table_new(NULL, NULL);
g_hash_table_replace(attributes,
(gpointer) "service",
(gpointer) service.c_str());
GList* items = secret_service_search_sync(
NULL,
&schema, // The schema.
attributes,
static_cast<SecretSearchFlags>(SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK |
SECRET_SEARCH_LOAD_SECRETS),
NULL, // Cancellable. (unneeded)
&error); // Reference to the error.
g_hash_table_destroy(attributes);
if (error != NULL) {
*errStr = std::string(error->message);
g_error_free(error);
return FAIL_ERROR;
}
GList* current = items;
for (current = items; current != NULL; current = current->next) {
SecretItem* item = reinterpret_cast<SecretItem*>(current->data);
GHashTable* itemAttrs = secret_item_get_attributes(item);
char* account = strdup(
reinterpret_cast<char*>(g_hash_table_lookup(itemAttrs, "account")));
SecretValue* secret = secret_item_get_secret(item);
char* password = strdup(secret_value_get_text(secret));
if (account == NULL || password == NULL) {
if (account)
free(account);
if (password)
free(password);
continue;
}
credentials->push_back(Credentials(account, password));
free(account);
free(password);
}
return SUCCESS;
}
} // namespace keytar