-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsecretUtils.js
279 lines (229 loc) · 8.25 KB
/
secretUtils.js
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
/* secretUtils.js
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import Gio from 'gi://Gio';
import Secret from 'gi://Secret';
// strings will be translated by gettext in the frontend
const _ = x => x;
Gio._promisify(Secret, 'password_clear', 'password_clear_finish');
Gio._promisify(Secret, 'password_lookup', 'password_lookup_finish');
Gio._promisify(Secret, 'password_search', 'password_search_finish');
Gio._promisify(Secret, 'password_store', 'password_store_finish');
Gio._promisify(Secret.Collection, 'create', 'create_finish');
Gio._promisify(Secret.Item.prototype, 'set_attributes', 'set_attributes_finish');
Gio._promisify(Secret.Item.prototype, 'set_label', 'set_label_finish');
Gio._promisify(Secret.Item.prototype, 'set_secret', 'set_secret_finish');
Gio._promisify(Secret.Service, 'get', 'get_finish');
Gio._promisify(Secret.Service.prototype, 'search', 'search_finish');
Gio._promisify(Secret.Service.prototype, 'lock', 'lock_finish');
Gio._promisify(Secret.Service.prototype, 'unlock', 'unlock_finish');
const OTP_COLLECTION_DBUS_PATH = '/org/freedesktop/secrets/collection/OTP';
function makeSchema()
{
return new Secret.Schema('org.gnome.shell.extensions.totp',
Secret.SchemaFlags.NONE,
{
type : Secret.SchemaAttributeType.STRING,
issuer : Secret.SchemaAttributeType.STRING,
name : Secret.SchemaAttributeType.STRING,
digits : Secret.SchemaAttributeType.INTEGER,
period : Secret.SchemaAttributeType.INTEGER,
algorithm : Secret.SchemaAttributeType.STRING
});
}
async function findOTPCollection()
{
const service = await Secret.Service.get(Secret.ServiceFlags.LOAD_COLLECTIONS, null);
const collections = service.get_collections();
// look for the 'OTP' at the hardcoded path
for (let i = 0; i < collections.length; ++i)
if (collections[i].get_object_path() == OTP_COLLECTION_DBUS_PATH)
return [service, collections[i]];
return [service, null];
}
async function ensureCollection()
{
let [service, collection] = await findOTPCollection();
if (collection)
return;
// could not find it, so create one
await Secret.Collection.create(service,
'OTP',
null,
Secret.CollectionCreateFlags.NONE,
null);
}
export
async function isOTPCollectionLocked()
{
// force a new connection, so we get reliable lock status
Secret.Service.disconnect();
const [service, collection] = await findOTPCollection();
if (!collection)
return false;
return collection.locked;
}
export
async function lockOTPCollection()
{
const [service, collection] = await findOTPCollection();
if (!collection)
return false;
return await service.lock([collection], null) > 0;
}
export
async function unlockOTPCollection()
{
const [service, collection] = await findOTPCollection();
if (!collection)
return false;
return await service.unlock([collection], null) > 0;
}
function getOrder(label)
{
const [token] = label.split(':', 1);
if (!token)
return 0;
const value = parseFloat(token);
if (isNaN(value))
return 0;
return value;
}
export
async function getOTPItems(unlock = false)
{
try {
let flags = Secret.SearchFlags.ALL;
if (unlock)
flags |= Secret.SearchFlags.UNLOCK;
const items = await Secret.password_search(makeSchema(),
{ type: 'TOTP' },
flags,
null);
// return them sorted, using the label
items.sort((a, b) => getOrder(a.get_label()) - getOrder(b.get_label()));
return items;
}
catch (e) {
return [];
}
}
export
async function getOTPItem(totp)
{
const [item] = await Secret.password_search(makeSchema(),
makeAttributes(totp),
Secret.SearchFlags.LOAD_SECRETS, // don't unlock
null);
if (!item)
throw new Error(_('Failed to lookup secret.'));
return item;
}
// libsecret wants the attributes to all be strings
function makeAttributes({issuer, name, digits, period, algorithm})
{
return {
type: 'TOTP',
issuer: issuer,
name: name,
digits: digits.toString(),
period: period.toString(),
algorithm: algorithm
};
}
function makeLabel({issuer, name}, order = -1)
{
const prefix = order > -1 ? order : "-";
return `${prefix}:${issuer}:${name}`;
}
export
async function getSecret(args)
{
const secret = await Secret.password_lookup(makeSchema(),
makeAttributes(args),
null);
if (secret == null)
throw new Error(_('Failed to retrieve secret.'));
return secret;
}
function equalDictionaries(a, b)
{
const ak = Object.keys(a);
const bk = Object.keys(b);
if (ak.length != bk.length)
return false;
for (let k of ak)
if (a[k] !== b[k])
return false;
return true;
}
export
async function updateTOTPItem(old_totp, new_totp)
{
const service = await Secret.Service.get(
Secret.ServiceFlags.OPEN_SESSION | Secret.ServiceFlags.LOAD_COLLECTIONS,
null);
const old_attributes = makeAttributes(old_totp);
const [item] = await service.search(makeSchema(),
old_attributes,
Secret.SearchFlags.UNLOCK
| Secret.SearchFlags.LOAD_SECRETS,
null);
if (!item)
throw new Error(_('Failed to lookup item.'));
// check if label changed
const old_label = item.get_label();
const new_label = makeLabel(new_totp, getOrder(old_label));
if (old_label != new_label)
if (!await item.set_label(new_label, null))
throw new Error(_('Failed to set label.'));
// check if attributes changed
const new_attributes = makeAttributes(new_totp);
if (!equalDictionaries(old_attributes, new_attributes))
if (!await item.set_attributes(makeSchema(), new_attributes, null))
throw new Error(_('Failed to set attributes.'));
// check if secret changed
if (old_totp.secret != new_totp.secret) {
const secret_value = new Secret.Value(new_totp.secret, -1, "text/plain");
if (!await item.set_secret(secret_value, null))
throw new Error(_('Failed to set secret.'));
}
}
export
async function updateTOTPOrder(totp, order)
{
const service = await Secret.Service.get(
Secret.ServiceFlags.OPEN_SESSION | Secret.ServiceFlags.LOAD_COLLECTIONS,
null);
const [item] = await service.search(makeSchema(),
makeAttributes(totp),
Secret.SearchFlags.NONE,
null);
if (!item)
throw new Error(_('Failed to lookup item.'));
const old_label = item.get_label();
const new_label = makeLabel(totp, order);
if (new_label == old_label)
return;
if (!await item.set_label(new_label, null))
throw new Error(_('Failed to set label.'));
}
export
async function createTOTPItem(totp, order)
{
await ensureCollection();
return await Secret.password_store(makeSchema(),
makeAttributes(totp),
OTP_COLLECTION_DBUS_PATH,
makeLabel(totp, order),
totp.secret,
null);
}
export
async function removeTOTPItem(totp)
{
return await Secret.password_clear(makeSchema(),
makeAttributes(totp),
null);
}