-
Notifications
You must be signed in to change notification settings - Fork 1
/
indicator.js
201 lines (155 loc) · 5.18 KB
/
indicator.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
/* indicator.js
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
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 * as SecretUtils from './secretUtils.js';
import TOTP from './totp.js';
import {gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js';
function copyToClipboard(text)
{
// this runs inside gnome-shell, so we use St
let clipboard = St.Clipboard.get_default();
clipboard.set_text(St.ClipboardType.PRIMARY, text);
clipboard.set_text(St.ClipboardType.CLIPBOARD, text);
Main.notify(_('OTP code copied to clipboard.'), text);
}
function makeLabel({issuer, name})
{
return `${issuer}: ${name}`;
}
export default
class Indicator extends PanelMenu.Button {
static {
GObject.registerClass(this);
}
#ext;
#lock_item;
#totp_items = [];
#unlock_item;
constructor(ext)
{
super();
this.#ext = ext;
this.add_child(
new St.Icon({
icon_name: 'changes-prevent-symbolic',
style_class: 'system-status-icon'
})
);
this.#lock_item = this.menu.addAction(_('Lock OTP secrets'),
this.lockTOTPSecrets.bind(this),
'changes-prevent-symbolic');
this.#unlock_item = this.menu.addAction(_('Unlock OTP secrets...'),
this.unlockTOTPSecrets.bind(this),
'changes-allow-symbolic');
this.#unlock_item.visible = !this.#lock_item.visible;
this.menu.addAction(_('Settings...'),
this.editTOTPSecrets.bind(this),
'preferences-other-symbolic');
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem(_('OTP Secrets')));
Main.panel.addToStatusArea(ext.uuid, this);
}
_init()
{
super._init(0.5, 'TOTP');
}
destroy()
{
this.#lock_item.destroy();
this.#lock_item = null;
this.#unlock_item.destroy();
this.#unlock_item = null;
this.clearTOTPItems();
super.destroy();
}
async _onOpenStateChanged(menu, is_open)
{
super._onOpenStateChanged(menu, is_open);
try {
if (is_open) {
let locked = await SecretUtils.isOTPCollectionLocked();
this.#lock_item.visible = !locked;
this.#unlock_item.visible = locked;
await this.refreshTOTPItems();
} else
this.clearTOTPItems();
}
catch (e) {
logError(e);
}
}
async lockTOTPSecrets()
{
try {
if (!await SecretUtils.lockOTPCollection())
// Sometimes the keyring locks just fine, yet it reports incorrectly that
// nothing was locked. So we double check here.
if (!await SecretUtils.isOTPCollectionLocked())
Main.notify(_('Failed to lock OTP secrets.'));
}
catch (e) {
logError(e);
Main.notifyError(_('Error locking OTP secrets.'), _(e.message));
}
}
async unlockTOTPSecrets()
{
try {
if (!await SecretUtils.unlockOTPCollection())
// Sometimes the keyring unlocks just fine, yet it reports incorrectly
// that nothing was unlocked. So we double check here.
if (await SecretUtils.isOTPCollectionLocked())
Main.notify(_('Failed to unlock OTP secrets.'));
}
catch (e) {
logError(e);
Main.notifyError(_('Error unlocking OTP secrets.'), _(e.message));
}
}
editTOTPSecrets()
{
this.#ext.openPreferences();
}
async refreshTOTPItems()
{
try {
let secrets = await SecretUtils.getOTPItems();
this.clearTOTPItems();
secrets.forEach(x => {
let totp = new TOTP(x.get_attributes());
let label = makeLabel(totp);
let item = this.menu.addAction(label,
this.copyCode.bind(this, totp),
'edit-copy-symbolic');
this.#totp_items.push(item);
});
}
catch (e) {
logError(e);
Main.notifyError(_('Error retrieving OTP items.'), _(e.message));
}
}
clearTOTPItems()
{
this.#totp_items.forEach(x => x.destroy());
this.#totp_items = [];
}
async copyCode(totp)
{
try {
totp.secret = await SecretUtils.getSecret(totp);
const code = totp.code();
copyToClipboard(code);
}
catch (e) {
logError(e);
Main.notifyError(_('Error copying the OTP authentication code.'), _(e.message));
}
}
};