keeweb/app/scripts/views/fields/field-view-otp.js

195 lines
5.1 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import { Timeouts } from 'const/timeouts';
import { FieldViewText } from 'views/fields/field-view-text';
2020-04-15 16:50:01 +02:00
import { Locale } from 'util/locale';
import { StringFormat } from 'util/formatting/string-format';
2016-04-02 15:20:48 +02:00
2017-01-31 07:50:28 +01:00
const MinOpacity = 0.1;
2016-04-01 21:19:27 +02:00
2019-09-16 19:55:06 +02:00
class FieldViewOtp extends FieldViewText {
otpTimeout = null;
otpTickInterval = null;
otpValue = null;
otpGenerator = null;
otpTimeLeft = 0;
otpValidUntil = 0;
fieldOpacity = null;
2020-04-15 16:50:01 +02:00
otpState = null;
2016-04-01 23:09:16 +02:00
2019-09-16 20:54:14 +02:00
constructor(model, options) {
super(model, options);
2020-04-15 16:50:01 +02:00
this.once('remove', () => this.stopOtpUpdater());
if (model.readonly) {
this.readonly = true;
}
2019-09-16 20:54:14 +02:00
}
2019-08-18 10:17:09 +02:00
renderValue(value) {
2016-04-01 23:09:16 +02:00
if (!value) {
2016-04-02 15:20:48 +02:00
this.resetOtp();
2016-04-01 23:09:16 +02:00
return '';
}
if (value !== this.otpGenerator) {
2020-04-15 16:50:01 +02:00
this.resetOtp();
2016-04-01 23:09:16 +02:00
this.otpGenerator = value;
this.requestOtpUpdate();
}
2020-04-15 16:50:01 +02:00
if (this.otpValue) {
return this.otpValue;
}
switch (this.otpState) {
case 'awaiting-command':
return Locale.detOtpClickToTouch;
case 'awaiting-touch':
return Locale.detOtpTouch.replace('{}', this.model.deviceShortName);
case 'error':
return StringFormat.capFirst(Locale.error);
case 'generating':
return Locale.detOtpGenerating;
default:
return '';
}
2019-09-16 19:55:06 +02:00
}
2016-04-01 21:19:27 +02:00
2019-08-18 10:17:09 +02:00
getEditValue(value) {
2016-04-01 23:09:16 +02:00
return value && value.url;
2019-09-16 19:55:06 +02:00
}
2016-04-01 23:09:16 +02:00
getTextValue() {
return this.otpValue;
}
2019-08-18 10:17:09 +02:00
render() {
2019-09-16 19:55:06 +02:00
super.render();
2016-04-02 15:20:48 +02:00
this.fieldOpacity = null;
this.otpTick();
2019-09-16 19:55:06 +02:00
}
2016-04-02 15:20:48 +02:00
2019-08-18 10:17:09 +02:00
resetOtp() {
2016-04-02 15:20:48 +02:00
this.otpGenerator = null;
this.otpValue = null;
this.otpTimeLeft = 0;
this.otpValidUntil = 0;
2020-04-15 16:50:01 +02:00
this.otpState = null;
2016-04-01 23:09:16 +02:00
if (this.otpTimeout) {
clearTimeout(this.otpTimeout);
2016-04-02 15:20:48 +02:00
this.otpTimeout = null;
}
if (this.otpTickInterval) {
clearInterval(this.otpTickInterval);
this.otpTickInterval = null;
2016-04-01 23:09:16 +02:00
}
2019-09-16 19:55:06 +02:00
}
2016-04-01 23:09:16 +02:00
2019-08-18 10:17:09 +02:00
requestOtpUpdate() {
2016-04-01 23:09:16 +02:00
if (this.value) {
2020-04-15 16:50:01 +02:00
if (this.model.needsTouch) {
this.otpState = 'awaiting-command';
} else {
this.otpState = 'generating';
this.value.next(this.otpUpdated.bind(this));
}
2016-04-01 23:09:16 +02:00
}
2019-09-16 19:55:06 +02:00
}
2016-04-01 23:09:16 +02:00
2020-04-15 16:50:01 +02:00
otpUpdated(err, pass, timeLeft) {
if (this.removed) {
return;
}
if (err) {
this.otpState = 'error';
this.render();
return;
}
2016-04-02 15:20:48 +02:00
if (!this.value || !pass) {
this.resetOtp();
2016-04-01 23:09:16 +02:00
return;
}
this.otpValue = pass || '';
2016-04-02 15:20:48 +02:00
this.otpTimeLeft = timeLeft || 0;
this.otpValidUntil = Date.now() + timeLeft;
2016-04-04 20:45:17 +02:00
if (!this.editing) {
this.render();
}
2016-04-01 23:16:23 +02:00
if (this.otpValue && timeLeft) {
2020-04-15 16:50:01 +02:00
this.otpTimeout = setTimeout(() => {
this.requestOtpUpdate();
if (this.otpTickInterval) {
clearInterval(this.otpTickInterval);
this.otpTickInterval = null;
}
if (this.model.needsTouch) {
this.fieldOpacity = null;
this.otpValue = null;
this.otpValidUntil = 0;
this.otpTimeLeft = 0;
this.valueEl.css('opacity', 1);
}
this.render();
}, timeLeft);
2016-04-02 15:20:48 +02:00
if (!this.otpTickInterval) {
this.otpTickInterval = setInterval(this.otpTick.bind(this), 300);
}
}
2019-09-16 19:55:06 +02:00
}
2016-04-02 15:20:48 +02:00
2019-08-18 10:17:09 +02:00
otpTick() {
2016-04-02 15:20:48 +02:00
if (!this.value || !this.otpValidUntil) {
return;
}
2017-01-31 07:50:28 +01:00
let opacity;
const timeLeft = this.otpValidUntil - Date.now();
2016-04-02 15:20:48 +02:00
if (timeLeft >= Timeouts.OtpFadeDuration || this.editing) {
opacity = 1;
} else if (timeLeft <= 0) {
opacity = MinOpacity;
} else {
opacity = Math.max(MinOpacity, Math.pow(timeLeft / Timeouts.OtpFadeDuration, 2));
}
if (this.fieldOpacity === opacity) {
return;
2016-04-01 23:09:16 +02:00
}
2016-04-02 15:20:48 +02:00
this.fieldOpacity = opacity;
this.valueEl.css('opacity', opacity);
2016-04-01 21:19:27 +02:00
}
2020-04-15 16:50:01 +02:00
copyValue() {
2020-06-01 16:53:51 +02:00
this.refreshOtp((err) => {
2020-05-09 10:37:34 +02:00
if (!err) {
super.copyValue();
}
});
}
refreshOtp(callback) {
2020-04-15 16:50:01 +02:00
if (this.model.needsTouch) {
if (this.otpValue) {
2020-05-09 10:37:34 +02:00
callback();
} else {
this.requestTouch(callback);
2020-04-15 16:50:01 +02:00
}
} else {
2020-05-09 10:37:34 +02:00
callback();
2020-04-15 16:50:01 +02:00
}
}
requestTouch(callback) {
this.otpState = 'awaiting-touch';
this.value.next((err, code, timeLeft) => {
this.otpUpdated(err, code, timeLeft);
callback(err);
});
this.render();
}
stopOtpUpdater() {
2020-05-05 20:14:32 +02:00
if (this.otpState === 'awaiting-touch') {
2020-04-15 16:50:01 +02:00
if (this.value && this.value.cancel) {
this.value.cancel();
}
}
this.resetOtp();
}
2019-09-16 19:55:06 +02:00
}
2016-04-01 21:19:27 +02:00
2019-09-15 14:16:32 +02:00
export { FieldViewOtp };