mirror of https://github.com/keeweb/keeweb.git
84 lines
2.4 KiB
JavaScript
84 lines
2.4 KiB
JavaScript
|
import { ExternalOtpDeviceModel } from 'models/external/external-otp-device-model';
|
||
|
import { ExternalOtpEntryModel } from 'models/external/external-otp-entry-model';
|
||
|
import { Launcher } from 'comp/launcher';
|
||
|
|
||
|
class YubiKeyOtpModel extends ExternalOtpDeviceModel {
|
||
|
constructor(props) {
|
||
|
super({
|
||
|
shortName: 'YubiKey',
|
||
|
...props
|
||
|
});
|
||
|
}
|
||
|
|
||
|
open(callback) {
|
||
|
this.openProcess = Launcher.spawn({
|
||
|
cmd: 'ykman',
|
||
|
args: ['oath', 'code'],
|
||
|
noStdOutLogging: true,
|
||
|
complete: (err, stdout, code) => {
|
||
|
this.openProcess = null;
|
||
|
if (err) {
|
||
|
return callback(err);
|
||
|
}
|
||
|
for (const line of stdout.split('\n')) {
|
||
|
const match = line.match(/^(.*?):(.*?)\s+(.*)$/);
|
||
|
if (!match) {
|
||
|
continue;
|
||
|
}
|
||
|
const [, title, user, code] = match;
|
||
|
const needsTouch = !code.match(/^\d+$/);
|
||
|
|
||
|
this.entries.push(
|
||
|
new ExternalOtpEntryModel({
|
||
|
id: title + ':' + user,
|
||
|
device: this,
|
||
|
icon: 'clock-o',
|
||
|
title,
|
||
|
user,
|
||
|
needsTouch
|
||
|
})
|
||
|
);
|
||
|
}
|
||
|
callback();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
cancelOpen() {
|
||
|
this.openAborted = true;
|
||
|
if (this.openProcess) {
|
||
|
this.openProcess.kill();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
getOtp(entry, callback) {
|
||
|
const msPeriod = 30000;
|
||
|
const timeLeft = msPeriod - (Date.now() % msPeriod) + 500;
|
||
|
return Launcher.spawn({
|
||
|
cmd: 'ykman',
|
||
|
args: ['oath', 'code', '--single', `${entry.title}:${entry.user}`],
|
||
|
noStdOutLogging: true,
|
||
|
complete: (err, stdout) => {
|
||
|
if (err) {
|
||
|
return callback(err, null, timeLeft);
|
||
|
}
|
||
|
const otp = stdout.trim();
|
||
|
callback(null, otp, timeLeft);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
cancelGetOtp(entry, ps) {
|
||
|
if (ps) {
|
||
|
ps.kill();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
YubiKeyOtpModel.defineModelProperties({
|
||
|
openProcess: null,
|
||
|
openAborted: false
|
||
|
});
|
||
|
|
||
|
export { YubiKeyOtpModel };
|