-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindicator.js
More file actions
214 lines (169 loc) · 5.69 KB
/
Copy pathindicator.js
File metadata and controls
214 lines (169 loc) · 5.69 KB
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
/* indicator.js
* Copyright (C) 2025 Daniel K. O.
*
* 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 HOTP from './hotp.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;
#otp_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.lockOTPSecrets.bind(this),
'changes-prevent-symbolic');
this.#unlock_item = this.menu.addAction(_('Unlock OTP secrets...'),
this.unlockOTPSecrets.bind(this),
'changes-allow-symbolic');
this.#unlock_item.visible = !this.#lock_item.visible;
this.menu.addAction(_('Settings...'),
this.editOTPSecrets.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.clearOTPItems();
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;
if (locked)
this.clearOTPItems();
else
await this.refreshOTPItems();
} else
this.clearOTPItems();
}
catch (e) {
logError(e);
}
}
async lockOTPSecrets()
{
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 unlockOTPSecrets()
{
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));
}
}
editOTPSecrets()
{
this.#ext.openPreferences();
}
async refreshOTPItems()
{
try {
let secrets = await SecretUtils.getOTPItems();
this.clearOTPItems();
secrets.forEach(x => {
let otp = null;
let args = x.get_attributes();
if (args.type == 'TOTP')
otp = new TOTP(args);
if (args.type == 'HOTP')
otp = new HOTP(args);
if (otp == null)
throw Error(`BUG: args.type is ${args.type}`);
let label = makeLabel(otp);
let item = this.menu.addAction(label,
this.copyCode.bind(this, otp),
'edit-copy-symbolic');
this.#otp_items.push(item);
});
}
catch (e) {
logError(e);
Main.notifyError(_('Error retrieving OTP items.'), _(e.message));
}
}
clearOTPItems()
{
this.#otp_items.forEach(x => x.destroy());
this.#otp_items = [];
}
async copyCode(otp)
{
try {
otp.secret = await SecretUtils.getSecret(otp);
const code = otp.code();
if (otp.type == 'HOTP')
await SecretUtils.incrementHOTP(otp);
copyToClipboard(code);
}
catch (e) {
logError(e);
Main.notifyError(_('Error copying the OTP authentication code.'), _(e.message));
}
}
};