fixed sequential otp generation

This commit is contained in:
antelle 2020-05-11 17:44:18 +02:00
parent c6ce694dfe
commit 5d15411d3d
No known key found for this signature in database
GPG Key ID: 094A2F2D6136A4EE
2 changed files with 42 additions and 8 deletions

View File

@ -12,14 +12,45 @@ class ExternalOtpEntryModel extends ExternalEntryModel {
if (this.otpGenerator) {
return;
}
this.otpGenerator = {
const gen = {
next: callback => {
this.otpState = this.device.getOtp(this, callback);
if (gen.otp && gen.expires) {
const timeLeft = gen.expires - Date.now();
if (timeLeft > 0) {
return callback(null, gen.otp, timeLeft);
}
}
if (gen.promise) {
gen.promise.then(({ err, otp, timeLeft }) => {
callback(err, otp, timeLeft);
});
return;
}
gen.promise = new Promise(resolve => {
gen.otpState = this.device.getOtp(this, (err, otp, timeLeft) => {
gen.otpState = null;
gen.promise = null;
if (otp && timeLeft > 0) {
gen.otp = otp;
gen.expires = Date.now() + timeLeft;
} else {
gen.otp = null;
gen.expires = null;
}
callback(err, otp, timeLeft);
resolve({ err, otp, timeLeft });
});
});
},
cancel: () => {
this.device.cancelGetOtp(this, this.otpState);
if (this.otpState) {
this.device.cancelGetOtp(this, this.otpState);
}
}
};
this.otpGenerator = gen;
}
_buildFields() {
@ -31,8 +62,7 @@ class ExternalOtpEntryModel extends ExternalEntryModel {
ExternalOtpEntryModel.defineModelProperties({
user: undefined,
otpGenerator: undefined,
needsTouch: false,
otpState: null
needsTouch: false
});
export { ExternalOtpEntryModel };

View File

@ -445,13 +445,17 @@ class DetailsView extends View {
initOtp() {
this.matchingOtpEntry = null;
if (!this.model || this.model.external) {
if (!this.model) {
return;
}
if (this.model.external) {
this.model.initOtpGenerator();
return;
}
this.matchingOtpEntry = this.appModel.getMatchingOtpEntry(this.model);
this.model.initOtpGenerator();
this.matchingOtpEntry?.initOtpGenerator();
}