2016-08-29 20:19:44 +03:00
|
|
|
/* global Backbone, Handlebars, OC, _ */
|
|
|
|
|
2017-02-28 12:20:30 +03:00
|
|
|
(function(OC, Handlebars, $, _) {
|
2016-08-29 20:19:44 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
OC.Settings = OC.Settings || {};
|
|
|
|
OC.Settings.TwoFactorBackupCodes = OC.Settings.TwoFactorBackupCodes || {};
|
|
|
|
|
|
|
|
var TEMPLATE = '<div>'
|
2017-02-28 12:20:30 +03:00
|
|
|
+ '{{#unless enabled}}'
|
|
|
|
+ '<button id="generate-backup-codes">' + t('twofactor_backupcodes', 'Generate backup codes') + '</button>'
|
|
|
|
+ '{{else}}'
|
|
|
|
+ '<p>'
|
|
|
|
+ '{{#unless codes}}'
|
|
|
|
+ t('twofactor_backupcodes', 'Backup codes have been generated. {{used}} of {{total}} codes have been used.')
|
|
|
|
+ '{{else}}'
|
|
|
|
+ t('twofactor_backupcodes', 'These are your backup codes. Please save and/or print them as you will not be able to read the codes again later')
|
|
|
|
+ '<ul>'
|
|
|
|
+ '{{#each codes}}'
|
|
|
|
+ '<li class="backup-code">{{this}}</li>'
|
|
|
|
+ '{{/each}}'
|
|
|
|
+ '</ul>'
|
|
|
|
+ '<a href="{{download}}" class="button" download="Nextcloud-backup-codes.txt">' + t('twofactor_backupcodes', 'Save backup codes') + '</a>'
|
|
|
|
+ '<button id="print-backup-codes" class="button">' + t('twofactor_backupcodes', 'Print backup codes') + '</button>'
|
|
|
|
+ '{{/unless}}'
|
|
|
|
+ '</p>'
|
|
|
|
+ '<p>'
|
|
|
|
+ '<button id="generate-backup-codes">' + t('twofactor_backupcodes', 'Regenerate backup codes') + '</button>'
|
|
|
|
+ '</p>'
|
|
|
|
+ '<p>'
|
|
|
|
+ t('twofactor_backupcodes', 'If you regenerate backup codes, you automatically invalidate old codes.')
|
|
|
|
+ '</p>'
|
|
|
|
+ '{{/unless}}'
|
|
|
|
+ '</div';
|
2016-08-29 20:19:44 +03:00
|
|
|
|
2017-02-28 12:20:30 +03:00
|
|
|
/**
|
|
|
|
* @class OC.Settings.TwoFactorBackupCodes.View
|
|
|
|
*/
|
2016-08-29 20:19:44 +03:00
|
|
|
var View = OC.Backbone.View.extend({
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {undefined|Function}
|
|
|
|
*/
|
2016-08-29 20:19:44 +03:00
|
|
|
_template: undefined,
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} data
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
template: function(data) {
|
2016-08-29 20:19:44 +03:00
|
|
|
if (!this._template) {
|
|
|
|
this._template = Handlebars.compile(TEMPLATE);
|
|
|
|
}
|
|
|
|
return this._template(data);
|
|
|
|
},
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2016-08-29 20:19:44 +03:00
|
|
|
_loading: undefined,
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2016-08-29 20:19:44 +03:00
|
|
|
_enabled: undefined,
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2016-08-29 20:19:44 +03:00
|
|
|
_total: undefined,
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2016-08-29 20:19:44 +03:00
|
|
|
_used: undefined,
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Array}
|
|
|
|
*/
|
2016-08-29 20:19:44 +03:00
|
|
|
_codes: undefined,
|
2017-02-28 12:20:30 +03:00
|
|
|
|
2016-08-29 20:19:44 +03:00
|
|
|
events: {
|
|
|
|
'click #generate-backup-codes': '_onGenerateBackupCodes',
|
2017-02-28 12:20:30 +03:00
|
|
|
'click #print-backup-codes': '_onPrintBackupCodes'
|
2016-08-29 20:19:44 +03:00
|
|
|
},
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
initialize: function() {
|
2016-08-29 20:19:44 +03:00
|
|
|
this._load();
|
|
|
|
},
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {self}
|
|
|
|
*/
|
|
|
|
render: function() {
|
2016-08-29 20:19:44 +03:00
|
|
|
this.$el.html(this.template({
|
|
|
|
enabled: this._enabled,
|
|
|
|
total: this._total,
|
|
|
|
used: this._used,
|
|
|
|
codes: this._codes,
|
2016-12-15 20:16:41 +03:00
|
|
|
download: this._getDownloadData()
|
2016-08-29 20:19:44 +03:00
|
|
|
}));
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
return this;
|
2016-08-29 20:19:44 +03:00
|
|
|
},
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
_getDownloadData: function() {
|
|
|
|
if (!this._codes) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return 'data:text/plain,' + encodeURIComponent(_.reduce(this._codes, function(prev, code) {
|
|
|
|
return prev + code + '\r\n';
|
|
|
|
}, ''));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
_getPrintData: function() {
|
2016-08-29 20:19:44 +03:00
|
|
|
if (!this._codes) {
|
|
|
|
return '';
|
|
|
|
}
|
2017-02-28 12:20:30 +03:00
|
|
|
return _.reduce(this._codes, function(prev, code) {
|
2016-12-15 20:16:41 +03:00
|
|
|
return prev + code + "<br>";
|
|
|
|
}, '');
|
2016-08-29 20:19:44 +03:00
|
|
|
},
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load codes from the server
|
|
|
|
*
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
_load: function() {
|
2016-08-29 20:19:44 +03:00
|
|
|
this._loading = true;
|
|
|
|
|
|
|
|
var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/state');
|
|
|
|
var loading = $.ajax(url, {
|
2017-02-28 12:20:30 +03:00
|
|
|
method: 'GET'
|
2016-08-29 20:19:44 +03:00
|
|
|
});
|
|
|
|
|
2017-02-28 12:20:30 +03:00
|
|
|
$.when(loading).done(function(data) {
|
2016-08-29 20:19:44 +03:00
|
|
|
this._enabled = data.enabled;
|
|
|
|
this._total = data.total;
|
|
|
|
this._used = data.used;
|
|
|
|
}.bind(this));
|
2017-02-28 12:20:30 +03:00
|
|
|
$.when(loading).always(function() {
|
2016-08-29 20:19:44 +03:00
|
|
|
this._loading = false;
|
|
|
|
this.render();
|
|
|
|
}.bind(this));
|
|
|
|
},
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Event handler to generate the codes
|
|
|
|
*
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
_onGenerateBackupCodes: function() {
|
2016-09-19 17:58:38 +03:00
|
|
|
if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
|
|
|
|
OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._onGenerateBackupCodes, this));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-29 20:19:44 +03:00
|
|
|
// Hide old codes
|
|
|
|
this._enabled = false;
|
|
|
|
this.render();
|
|
|
|
$('#generate-backup-codes').addClass('icon-loading-small');
|
|
|
|
var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/create');
|
|
|
|
$.ajax(url, {
|
|
|
|
method: 'POST'
|
2017-02-28 12:20:30 +03:00
|
|
|
}).done(function(data) {
|
2016-08-29 20:19:44 +03:00
|
|
|
this._enabled = data.state.enabled;
|
|
|
|
this._total = data.state.total;
|
|
|
|
this._used = data.state.used;
|
|
|
|
this._codes = data.codes;
|
|
|
|
this.render();
|
2017-02-28 12:20:30 +03:00
|
|
|
}.bind(this)).fail(function() {
|
2016-09-06 13:52:44 +03:00
|
|
|
OC.Notification.showTemporary(t('twofactor_backupcodes', 'An error occurred while generating your backup codes'));
|
2016-08-29 20:19:44 +03:00
|
|
|
$('#generate-backup-codes').removeClass('icon-loading-small');
|
|
|
|
});
|
|
|
|
},
|
2017-02-28 12:20:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Event handler to print the codes
|
|
|
|
*
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
_onPrintBackupCodes: function() {
|
|
|
|
var data = this._getPrintData();
|
2016-12-15 20:16:41 +03:00
|
|
|
var newTab = window.open('', t('twofactor_backupcodes', 'Nextcloud backup codes'));
|
2016-12-20 16:52:24 +03:00
|
|
|
newTab.document.write('<h1>' + t('twofactor_backupcodes', 'Nextcloud backup codes') + '</h1>');
|
2016-12-15 20:16:41 +03:00
|
|
|
newTab.document.write(data);
|
|
|
|
newTab.print();
|
|
|
|
newTab.close();
|
2016-08-29 20:19:44 +03:00
|
|
|
}
|
2017-02-28 12:20:30 +03:00
|
|
|
|
2016-08-29 20:19:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
OC.Settings.TwoFactorBackupCodes.View = View;
|
|
|
|
|
2016-09-06 11:39:00 +03:00
|
|
|
})(OC, Handlebars, $, _);
|