Fix saving backup codes by using a correct data uri

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2017-02-28 10:20:30 +01:00
parent b72b02778d
commit 2f980ca76c
No known key found for this signature in database
GPG Key ID: CC42AC2A7F0E56D8
1 changed files with 121 additions and 41 deletions

View File

@ -1,6 +1,6 @@
/* global Backbone, Handlebars, OC, _ */ /* global Backbone, Handlebars, OC, _ */
(function (OC, Handlebars, $, _) { (function(OC, Handlebars, $, _) {
'use strict'; 'use strict';
OC.Settings = OC.Settings || {}; OC.Settings = OC.Settings || {};
@ -33,27 +33,68 @@
+ '{{/unless}}' + '{{/unless}}'
+ '</div'; + '</div';
/**
* @class OC.Settings.TwoFactorBackupCodes.View
*/
var View = OC.Backbone.View.extend({ var View = OC.Backbone.View.extend({
/**
* @type {undefined|Function}
*/
_template: undefined, _template: undefined,
template: function (data) {
/**
* @param {Object} data
* @returns {string}
*/
template: function(data) {
if (!this._template) { if (!this._template) {
this._template = Handlebars.compile(TEMPLATE); this._template = Handlebars.compile(TEMPLATE);
} }
return this._template(data); return this._template(data);
}, },
/**
* @type {boolean}
*/
_loading: undefined, _loading: undefined,
/**
* @type {boolean}
*/
_enabled: undefined, _enabled: undefined,
/**
* @type {Number}
*/
_total: undefined, _total: undefined,
/**
* @type {Number}
*/
_used: undefined, _used: undefined,
/**
* @type {Array}
*/
_codes: undefined, _codes: undefined,
events: { events: {
'click #generate-backup-codes': '_onGenerateBackupCodes', 'click #generate-backup-codes': '_onGenerateBackupCodes',
'click #print-backup-codes': '_onPrintBackupCodes', 'click #print-backup-codes': '_onPrintBackupCodes'
}, },
initialize: function () {
/**
* @returns {undefined}
*/
initialize: function() {
this._load(); this._load();
}, },
render: function () {
/**
* @returns {self}
*/
render: function() {
this.$el.html(this.template({ this.$el.html(this.template({
enabled: this._enabled, enabled: this._enabled,
total: this._total, total: this._total,
@ -61,34 +102,66 @@
codes: this._codes, codes: this._codes,
download: this._getDownloadData() download: this._getDownloadData()
})); }));
return this;
}, },
_getDownloadData: function () {
/**
* @private
* @returns {String}
*/
_getDownloadData: function() {
if (!this._codes) { if (!this._codes) {
return ''; return '';
} }
return _.reduce(this._codes, function (prev, code) { return 'data:text/plain,' + encodeURIComponent(_.reduce(this._codes, function(prev, code) {
return prev + code + '\r\n';
}, ''));
},
/**
* @private
* @returns {String}
*/
_getPrintData: function() {
if (!this._codes) {
return '';
}
return _.reduce(this._codes, function(prev, code) {
return prev + code + "<br>"; return prev + code + "<br>";
}, ''); }, '');
}, },
_load: function () {
/**
* Load codes from the server
*
* @returns {undefined}
*/
_load: function() {
this._loading = true; this._loading = true;
var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/state'); var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/state');
var loading = $.ajax(url, { var loading = $.ajax(url, {
method: 'GET', method: 'GET'
}); });
$.when(loading).done(function (data) { $.when(loading).done(function(data) {
this._enabled = data.enabled; this._enabled = data.enabled;
this._total = data.total; this._total = data.total;
this._used = data.used; this._used = data.used;
}.bind(this)); }.bind(this));
$.when(loading).always(function () { $.when(loading).always(function() {
this._loading = false; this._loading = false;
this.render(); this.render();
}.bind(this)); }.bind(this));
}, },
_onGenerateBackupCodes: function () {
/**
* Event handler to generate the codes
*
* @returns {undefined}
*/
_onGenerateBackupCodes: function() {
if (OC.PasswordConfirmation.requiresPasswordConfirmation()) { if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._onGenerateBackupCodes, this)); OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._onGenerateBackupCodes, this));
return; return;
@ -101,25 +174,32 @@
var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/create'); var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/create');
$.ajax(url, { $.ajax(url, {
method: 'POST' method: 'POST'
}).done(function (data) { }).done(function(data) {
this._enabled = data.state.enabled; this._enabled = data.state.enabled;
this._total = data.state.total; this._total = data.state.total;
this._used = data.state.used; this._used = data.state.used;
this._codes = data.codes; this._codes = data.codes;
this.render(); this.render();
}.bind(this)).fail(function () { }.bind(this)).fail(function() {
OC.Notification.showTemporary(t('twofactor_backupcodes', 'An error occurred while generating your backup codes')); OC.Notification.showTemporary(t('twofactor_backupcodes', 'An error occurred while generating your backup codes'));
$('#generate-backup-codes').removeClass('icon-loading-small'); $('#generate-backup-codes').removeClass('icon-loading-small');
}); });
}, },
_onPrintBackupCodes: function () {
var data = this._getDownloadData(); /**
* Event handler to print the codes
*
* @returns {undefined}
*/
_onPrintBackupCodes: function() {
var data = this._getPrintData();
var newTab = window.open('', t('twofactor_backupcodes', 'Nextcloud backup codes')); var newTab = window.open('', t('twofactor_backupcodes', 'Nextcloud backup codes'));
newTab.document.write('<h1>' + t('twofactor_backupcodes', 'Nextcloud backup codes') + '</h1>'); newTab.document.write('<h1>' + t('twofactor_backupcodes', 'Nextcloud backup codes') + '</h1>');
newTab.document.write(data); newTab.document.write(data);
newTab.print(); newTab.print();
newTab.close(); newTab.close();
} }
}); });
OC.Settings.TwoFactorBackupCodes.View = View; OC.Settings.TwoFactorBackupCodes.View = View;