watching yubikeys

This commit is contained in:
antelle 2020-05-05 21:26:41 +02:00
parent 277d443d4c
commit 46710aead4
No known key found for this signature in database
GPG Key ID: 094A2F2D6136A4EE
3 changed files with 18 additions and 1 deletions

View File

@ -166,6 +166,7 @@ class AppModel {
this.refresh();
file.on('reload', this.reloadFile.bind(this));
file.on('change', () => Events.emit('file-changed', file));
file.on('ejected', () => this.closeFile(file));
return true;
}

View File

@ -1,6 +1,8 @@
import { Events } from 'framework/events';
import { ExternalOtpDeviceModel } from 'models/external/external-otp-device-model';
import { ExternalOtpEntryModel } from 'models/external/external-otp-entry-model';
import { Launcher } from 'comp/launcher';
import { UsbListener } from 'comp/app/usb-listener';
let ykmanStatus;
@ -19,6 +21,12 @@ class YubiKeyOtpModel extends ExternalOtpDeviceModel {
return ykmanStatus;
}
onUsbDevicesChanged = () => {
if (UsbListener.attachedYubiKeys.length === 0) {
this.emit('ejected');
}
};
open(callback) {
this.openProcess = Launcher.spawn({
cmd: 'ykman',
@ -26,6 +34,9 @@ class YubiKeyOtpModel extends ExternalOtpDeviceModel {
noStdOutLogging: true,
complete: (err, stdout, code) => {
this.openProcess = null;
if (this.openAborted) {
err = 'Open aborted';
}
if (err) {
return callback(err);
}
@ -49,12 +60,14 @@ class YubiKeyOtpModel extends ExternalOtpDeviceModel {
);
}
this.active = true;
Events.on('usb-devices-changed', this.onUsbDevicesChanged);
callback();
}
});
}
cancelOpen() {
Events.off('usb-devices-changed', this.onUsbDevicesChanged);
this.openAborted = true;
if (this.openProcess) {
this.openProcess.kill();
@ -85,6 +98,7 @@ class YubiKeyOtpModel extends ExternalOtpDeviceModel {
}
close(callback) {
Events.off('usb-devices-changed', this.onUsbDevicesChanged);
this.set({
active: false
});
@ -114,6 +128,7 @@ class YubiKeyOtpModel extends ExternalOtpDeviceModel {
}
YubiKeyOtpModel.defineModelProperties({
onUsbDevicesChanged: null,
openProcess: null,
openAborted: false
});

View File

@ -106,7 +106,8 @@ class OpenView extends View {
const canOpenYubiKey =
this.model.settings.canOpenOtpDevice &&
this.model.settings.yubiKeyShowIcon &&
!!UsbListener.attachedYubiKeys.length;
!!UsbListener.attachedYubiKeys.length &&
!this.model.files.get('yubikey');
super.render({
lastOpenFiles: this.getLastOpenFiles(),