diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2016-08-29 19:19:44 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2016-09-05 08:51:13 +0200 |
commit | 8acb734854484e2ffd235929f6e7d0ba4c273844 (patch) | |
tree | 3269bc6cc60b51d4fd507d91e8eca3a4ecc262cd /apps/twofactor_backupcodes/js | |
parent | 8b484eedf029b8e1a9dcef0efb09db381888c4b0 (diff) | |
download | nextcloud-server-8acb734854484e2ffd235929f6e7d0ba4c273844.tar.gz nextcloud-server-8acb734854484e2ffd235929f6e7d0ba4c273844.zip |
add 2fa backup codes app
* add backup codes app unit tests
* add integration tests for the backup codes app
Diffstat (limited to 'apps/twofactor_backupcodes/js')
-rw-r--r-- | apps/twofactor_backupcodes/js/settings.js | 16 | ||||
-rw-r--r-- | apps/twofactor_backupcodes/js/settingsview.js | 120 |
2 files changed, 136 insertions, 0 deletions
diff --git a/apps/twofactor_backupcodes/js/settings.js b/apps/twofactor_backupcodes/js/settings.js new file mode 100644 index 00000000000..a4045cbc118 --- /dev/null +++ b/apps/twofactor_backupcodes/js/settings.js @@ -0,0 +1,16 @@ +/* global OC */ + +(function (OC) { + 'use strict'; + + OC.Settings = OC.Settings || {}; + OC.Settings.TwoFactorBackupCodes = OC.Settings.TwoFactorBackupCodes || {}; + + $(function () { + var view = new OC.Settings.TwoFactorBackupCodes.View({ + el: $('#twofactor-backupcodes-settings') + }); + view.render(); + }); +})(OC); + diff --git a/apps/twofactor_backupcodes/js/settingsview.js b/apps/twofactor_backupcodes/js/settingsview.js new file mode 100644 index 00000000000..3da4c141159 --- /dev/null +++ b/apps/twofactor_backupcodes/js/settingsview.js @@ -0,0 +1,120 @@ +/* global Backbone, Handlebars, OC, _ */ + +(function (OC, Handlebars, $, _) { + 'use strict'; + + OC.Settings = OC.Settings || {}; + OC.Settings.TwoFactorBackupCodes = OC.Settings.TwoFactorBackupCodes || {}; + + var TEMPLATE = '<div>' + + '{{#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'; + + var View = OC.Backbone.View.extend({ + _template: undefined, + template: function (data) { + if (!this._template) { + this._template = Handlebars.compile(TEMPLATE); + } + return this._template(data); + }, + _loading: undefined, + _enabled: undefined, + _total: undefined, + _used: undefined, + _codes: undefined, + events: { + 'click #generate-backup-codes': '_onGenerateBackupCodes', + 'click #print-backup-codes': '_onPrintBackupCodes', + }, + initialize: function () { + this._load(); + }, + render: function () { + this.$el.html(this.template({ + enabled: this._enabled, + total: this._total, + used: this._used, + codes: this._codes, + download: this._getDownloadDataHref() + })); + }, + _getDownloadDataHref: function () { + if (!this._codes) { + return ''; + } + return 'data:text/plain,' + encodeURIComponent(_.reduce(this._codes, function (prev, code) { + return prev + code + "\r\n"; + }, '')); + }, + _load: function () { + this._loading = true; + + var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/state'); + var loading = $.ajax(url, { + method: 'GET', + }); + + $.when(loading).done(function (data) { + this._enabled = data.enabled; + this._total = data.total; + this._used = data.used; + }.bind(this)); + $.when(loading).always(function () { + this._loading = false; + this.render(); + }.bind(this)); + }, + _onGenerateBackupCodes: function () { + // 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' + }).done(function (data) { + this._enabled = data.state.enabled; + this._total = data.state.total; + this._used = data.state.used; + this._codes = data.codes; + this.render(); + }.bind(this)).fail(function () { + OC.Notification.showTemporary('An error occurred while generating your backup codes'); + $('#generate-backup-codes').removeClass('icon-loading-small'); + }); + }, + _onPrintBackupCodes: function () { + var url = this._getDownloadDataHref(); + window.open(url, 'Nextcloud backpu codes'); + window.print(); + window.close(); + } + }); + + OC.Settings.TwoFactorBackupCodes.View = View; + +})(OC, Handlebars, $, _);
\ No newline at end of file |