-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
463 lines (349 loc) · 11.7 KB
/
extension.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* extension.js
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Secret from 'gi://Secret';
import St from 'gi://St';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import {
Extension,
gettext as _
} from 'resource:///org/gnome/shell/extensions/extension.js';
Gio._promisify(Secret.Collection, 'for_alias', 'for_alias_finish');
Gio._promisify(Secret.Service, 'get', 'get_finish');
Gio._promisify(Secret.Service.prototype, 'lock', 'lock_finish');
class Indicator extends PanelMenu.Button {
static {
GObject.registerClass(this);
}
#collection_items = [];
#ext;
#icon;
#lock_item;
#status_item;
constructor(ext)
{
super();
this.#ext = ext;
this.#icon = new St.Icon({
icon_name: 'security-medium-symbolic',
style_class: 'system-status-icon',
});
this.add_child(this.#icon);
this.menu.addAction(_('Settings...'),
() => this.#ext.openPreferences(),
'preferences-other-symbolic');
this.#lock_item = this.menu.addAction(_('Lock the keyring now'),
() => this.#ext.lockAll(),
'channel-secure-symbolic');
this.#status_item = new PopupMenu.PopupSeparatorMenuItem('-');
this.menu.addMenuItem(this.#status_item);
Main.panel.addToStatusArea(this.#ext.metadata.uuid, this);
}
_init()
{
super._init(0.5, 'Keyring Autolock');
}
destroy()
{
this.#ext = null;
this.#icon.destroy();
this.#icon = null;
this.#lock_item.destroy();
this.#lock_item = null;
this.#status_item.destroy();
this.#status_item = null;
super.destroy();
}
updateIcon(name)
{
this.#icon.set_icon_name(name);
}
_onOpenStateChanged(menu, is_open)
{
super._onOpenStateChanged(menu, is_open);
if (is_open)
this.updateLockedStatus();
}
async updateLockedStatus()
{
try {
const [locked, total, level, collections] = await this.#ext.refreshLevel();
this.#status_item.label.text = _('Locked:') + ` ${locked} / ${total}`;
this.#lock_item.visible = level != 'high';
this.#collection_items.forEach(item => item.destroy());
this.#collection_items = [];
collections.forEach(col => {
const item = this.menu.addAction(col.label,
() => this.#ext.lockCollection(col),
col.locked
? 'changes-prevent-symbolic'
: 'changes-allow-symbolic');
if (col.locked)
item.sensitive = false;
this.#collection_items.push(item);
});
}
catch (e) {
logError(e, 'updatedLockedStatus()');
}
}
};
export default
class KeyringAutolockExtension extends Extension {
#check_interval = 30;
#check_interval_signal = 0;
#delayed_lock_source = 0;
#hide_locked = false;
#hide_locked_signal = 0;
#ignored = [];
#ignored_signal = 0;
#indicator;
#level = 'medium';
#lock_delay = 60;
#lock_delay_signal = 0;
#periodic_check_source = 0;
#settings;
enable()
{
this.#indicator = new Indicator(this);
this.#settings = this.getSettings();
this.#check_interval_signal =
this.#settings.connect('changed::check-interval',
(settings, key) => {
this.check_interval = settings.get_uint(key);
});
this.#hide_locked_signal =
this.#settings.connect('changed::hide-locked',
(settings, key) => {
this.hide_locked = settings.get_boolean(key);
});
this.#lock_delay_signal =
this.#settings.connect('changed::lock-delay',
(settings, key) => {
this.lock_delay = settings.get_uint(key);
});
this.#ignored_signal =
this.#settings.connect('changed::ignored-collections',
(settings, key) => {
this.ignored = settings.get_value(key).get_objv();
});
this.check_interval = this.#settings.get_uint('check-interval');
this.hide_locked = this.#settings.get_boolean('hide-locked');
this.lock_delay = this.#settings.get_uint('lock-delay');
this.ignored = this.#settings.get_value('ignored-collections').get_objv();
}
disable()
{
this.cancelPeriodicCheck();
this.cancelDelayedLock();
if (this.#check_interval_signal) {
this.#settings?.disconnect(this.#check_interval_signal);
this.#check_interval_signal = 0;
}
if (this.#hide_locked_signal) {
this.#settings?.disconnect(this.#hide_locked_signal);
this.#hide_locked_signal = 0;
}
if (this.#lock_delay_signal) {
this.#settings?.disconnect(this.#lock_delay_signal);
this.#lock_delay_signal = 0;
}
if (this.#ignored_signal) {
this.#settings?.disconnect(this.#ignored_signal);
this.#ignored_signal = 0;
}
this.#settings = null;
this.#indicator?.destroy();
this.#indicator = null;
}
set level(val)
{
this.#level = val;
if (this.hide_locked) {
if (this.#level == 'high')
this.#indicator?.hide();
else
this.#indicator?.show();
}
const level_to_icon = {
'high' : 'security-high-symbolic',
'medium' : 'security-medium-symbolic',
'low' : 'security-low-symbolic'
};
this.#indicator?.updateIcon(level_to_icon[this.#level]);
}
get level()
{
return this.#level;
}
async refreshLevel()
{
try {
/*
* WORKAROUND: libsecret does not always report the updated locked state on
* password-less collections. But if we disonnect the service, it will work
* correctly next time.
*/
Secret.Service.disconnect();
const [service, collections] = await this.getCollections();
const locked = collections.reduce((total, c) => total + c.locked, 0);
if (locked == collections.length)
this.level = 'high';
else if (locked == 0)
this.level = 'low';
else
this.level = 'medium';
return [locked, collections.length, this.level, collections];
}
catch (e) {
logError(e, 'getLevel()');
return [0, 0, 'medium', []];
}
}
set check_interval(val)
{
this.#check_interval = val;
// set it up again, so it uses the new value
this.cancelPeriodicCheck();
this.schedulePeriodicCheck();
this.checkTask(); // no waiting
}
get check_interval()
{
return this.#check_interval;
}
schedulePeriodicCheck()
{
if (this.#periodic_check_source)
return;
this.#periodic_check_source = GLib.timeout_add(GLib.PRIORITY_LOW,
this.check_interval * 1000,
this.checkTask.bind(this));
}
cancelPeriodicCheck()
{
if (this.#periodic_check_source) {
GLib.Source.remove(this.#periodic_check_source);
this.#periodic_check_source = 0;
}
}
async checkTask()
{
try {
let [locked, total, level, collections] = await this.refreshLevel();
// always schedule a lock if we're below 'high' and there isn't a lock scheduled
if (level != 'high')
if (!this.hasPendingLock())
this.scheduleDelayedLock();
}
catch (e) {
logError(e, 'checkTask()');
}
return GLib.SOURCE_CONTINUE;
}
set hide_locked(val)
{
this.#hide_locked = val;
if (this.#hide_locked) {
if (this.level == 'high')
this.#indicator?.hide();
else
this.#indicator?.show();
} else
this.#indicator?.show();
this.checkTask(); // no waiting
}
get hide_locked()
{
return this.#hide_locked;
}
set lock_delay(val)
{
this.#lock_delay = val;
// if a lock is scheduled, make sure it uses the new delay
if (this.hasPendingLock()) {
this.cancelDelayedLock();
this.scheduleDelayedLock();
} else
this.checkTask(); // no waiting
}
get lock_delay()
{
return this.#lock_delay;
}
// return true if there's already a locking task scheduled
hasPendingLock()
{
return this.#delayed_lock_source != 0;
}
scheduleDelayedLock()
{
if (this.hasPendingLock())
return;
this.#delayed_lock_source =
GLib.timeout_add(GLib.PRIORITY_DEFAULT,
this.lock_delay * 1000,
this.lockAll.bind(this));
}
cancelDelayedLock()
{
if (this.hasPendingLock()) {
GLib.Source.remove(this.#delayed_lock_source);
this.#delayed_lock_source = 0;
}
}
async lockAll()
{
try {
let [service, collections] = await this.getCollections();
await service.lock(collections, null);
}
catch (e) {
logError(e, 'lockAll()');
}
this.cancelDelayedLock();
this.checkTask(); // no waiting
return GLib.SOURCE_REMOVE;
}
async lockCollection(col)
{
try {
await col.service.lock([col], null);
}
catch (e) {
logError(e, 'lockCollection()');
}
this.checkTask(); // no waiting
}
set ignored(val)
{
this.#ignored = val;
this.checkTask(); // no waiting
}
get ignored()
{
return this.#ignored;
}
// return all non-ignored collections, except 'session'.
async getCollections()
{
const service = await Secret.Service.get(Secret.ServiceFlags.LOAD_COLLECTIONS, null);
let collections = service.get_collections();
const session = await Secret.Collection.for_alias(service,
'session',
Secret.CollectionFlags.NONE,
null);
const session_path = session?.get_object_path();
let ignored = this.ignored.slice(); // make a copy
ignored.push(session_path);
collections = collections.filter(c => !ignored.includes(c.get_object_path()));
collections.sort((a, b) => a.label.localeCompare(b.label));
return [service, collections];
}
};