authtoken_view.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* global Handlebars, moment */
  2. /**
  3. * @author Christoph Wurst <christoph@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. (function (OC, _, $, Handlebars, moment) {
  22. 'use strict';
  23. OC.Settings = OC.Settings || {};
  24. var TEMPLATE_TOKEN =
  25. '<tr data-id="{{id}}">'
  26. + '<td class="has-tooltip" title="{{title}}">'
  27. + '<span class="token-name">{{name}}</span>'
  28. + '</td>'
  29. + '<td><span class="last-activity has-tooltip" title="{{lastActivityTime}}">{{lastActivity}}</span></td>'
  30. + '<td class="more">'
  31. + '{{#if showMore}}<a class="icon icon-more"/>{{/if}}'
  32. + '<div class="popovermenu bubble open menu configure">'
  33. + '{{#if canScope}}'
  34. + '<input class="filesystem checkbox" type="checkbox" id="{{id}}_filesystem" {{#if scope.filesystem}}checked{{/if}}/>'
  35. + '<label for="{{id}}_filesystem">' + t('core', 'Allow filesystem access') + '</label><br/>'
  36. + '{{/if}}'
  37. + '{{#if canDelete}}'
  38. + '<a class="icon icon-delete has-tooltip" title="' + t('core', 'Disconnect') + '">' + t('core', 'Revoke') +'</a>'
  39. + '{{/if}}'
  40. + '</div>'
  41. + '</td>'
  42. + '<tr>';
  43. var SubView = OC.Backbone.View.extend({
  44. collection: null,
  45. /**
  46. * token type
  47. * - 0: browser
  48. * - 1: device
  49. *
  50. * @see OC\Authentication\Token\IToken
  51. */
  52. type: 0,
  53. _template: undefined,
  54. template: function (data) {
  55. if (_.isUndefined(this._template)) {
  56. this._template = Handlebars.compile(TEMPLATE_TOKEN);
  57. }
  58. return this._template(data);
  59. },
  60. initialize: function (options) {
  61. this.type = options.type;
  62. this.collection = options.collection;
  63. this.on(this.collection, 'change', this.render);
  64. },
  65. render: function () {
  66. var _this = this;
  67. var list = this.$('.token-list');
  68. var tokens = this.collection.filter(function (token) {
  69. return token.get('type') === _this.type;
  70. });
  71. list.html('');
  72. // Show header only if there are tokens to show
  73. this._toggleHeader(tokens.length > 0);
  74. tokens.forEach(function (token) {
  75. var viewData = this._formatViewData(token);
  76. var html = _this.template(viewData);
  77. var $html = $(html);
  78. $html.find('.has-tooltip').tooltip({container: 'body'});
  79. list.append($html);
  80. }.bind(this));
  81. },
  82. toggleLoading: function (state) {
  83. this.$('table').toggleClass('icon-loading', state);
  84. },
  85. _toggleHeader: function (show) {
  86. this.$('.hidden-when-empty').toggleClass('hidden', !show);
  87. },
  88. _formatViewData: function (token) {
  89. var viewData = token.toJSON();
  90. var ts = viewData.lastActivity * 1000;
  91. viewData.lastActivity = OC.Util.relativeModifiedDate(ts);
  92. viewData.lastActivityTime = OC.Util.formatDate(ts, 'LLL');
  93. viewData.canScope = token.get('type') === 1;
  94. viewData.showMore = viewData.canScope || viewData.canDelete;
  95. // preserve title for cases where we format it further
  96. viewData.title = viewData.name;
  97. // pretty format sync client user agent
  98. var matches = viewData.name.match(/Mozilla\/5\.0 \((\w+)\) (?:mirall|csyncoC)\/(\d+\.\d+\.\d+)/);
  99. var userAgentMap = {
  100. ie: /(?:MSIE|Trident|Trident\/7.0; rv)[ :](\d+)/,
  101. // Microsoft Edge User Agent from https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
  102. edge: /^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Chrome\/[0-9.]+ (?:Mobile Safari|Safari)\/[0-9.]+ Edge\/[0-9.]+$/,
  103. // Firefox User Agent from https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference
  104. firefox: /^Mozilla\/5\.0 \([^)]*(Windows|OS X|Linux)[^)]+\) Gecko\/[0-9.]+ Firefox\/(\d+)(?:\.\d)?$/,
  105. // Chrome User Agent from https://developer.chrome.com/multidevice/user-agent
  106. chrome: /^Mozilla\/5\.0 \([^)]*(Windows|OS X|Linux)[^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Chrome\/(\d+)[0-9.]+ (?:Mobile Safari|Safari)\/[0-9.]+$/,
  107. // Safari User Agent from http://www.useragentstring.com/pages/Safari/
  108. safari: /^Mozilla\/5\.0 \([^)]*(Windows|OS X)[^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\)(?: Version\/([0-9]+)[0-9.]+)? Safari\/[0-9.A-Z]+$/,
  109. // Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent
  110. androidChrome: /Android.*(?:; (.*) Build\/).*Chrome\/(\d+)[0-9.]+/,
  111. iphone: / *CPU +iPhone +OS +([0-9]+)_(?:[0-9_])+ +like +Mac +OS +X */,
  112. ipad: /\(iPad\; *CPU +OS +([0-9]+)_(?:[0-9_])+ +like +Mac +OS +X */,
  113. iosClient: /^Mozilla\/5\.0 \(iOS\) (ownCloud|Nextcloud)\-iOS.*$/,
  114. androidClient:/^Mozilla\/5\.0 \(Android\) ownCloud\-android.*$/,
  115. // DAVdroid/1.2 (2016/07/03; dav4android; okhttp3) Android/6.0.1
  116. davDroid: /DAVdroid\/([0-9.]+)/,
  117. // Mozilla/5.0 (U; Linux; Maemo; Jolla; Sailfish; like Android 4.3) AppleWebKit/538.1 (KHTML, like Gecko) WebPirate/2.0 like Mobile Safari/538.1 (compatible)
  118. webPirate: /(Sailfish).*WebPirate\/(\d+)/,
  119. // Mozilla/5.0 (Maemo; Linux; U; Jolla; Sailfish; Mobile; rv:31.0) Gecko/31.0 Firefox/31.0 SailfishBrowser/1.0
  120. sailfishBrowser: /(Sailfish).*SailfishBrowser\/(\d+)/
  121. };
  122. var nameMap = {
  123. ie: t('setting', 'Internet Explorer'),
  124. edge: t('setting', 'Edge'),
  125. firefox: t('setting', 'Firefox'),
  126. chrome: t('setting', 'Google Chrome'),
  127. safari: t('setting', 'Safari'),
  128. androidChrome: t('setting', 'Google Chrome for Android'),
  129. iphone: t('setting', 'iPhone iOS'),
  130. ipad: t('setting', 'iPad iOS'),
  131. iosClient: t('setting', 'iOS Client'),
  132. androidClient: t('setting', 'Android Client'),
  133. davDroid: 'DAVdroid',
  134. webPirate: 'WebPirate',
  135. sailfishBrowser: 'SailfishBrowser'
  136. };
  137. if (matches) {
  138. viewData.name = t('settings', 'Sync client - {os}', {
  139. os: matches[1],
  140. version: matches[2]
  141. });
  142. }
  143. for (var client in userAgentMap) {
  144. if (matches = viewData.title.match(userAgentMap[client])) {
  145. if (matches[2] && matches[1]) { // version number and os
  146. viewData.name = nameMap[client] + ' ' + matches[2] + ' - ' + matches[1];
  147. }else if (matches[1]) { // only version number
  148. viewData.name = nameMap[client] + ' ' + matches[1];
  149. } else {
  150. viewData.name = nameMap[client];
  151. }
  152. }
  153. }
  154. if (viewData.current) {
  155. viewData.name = t('settings', 'This session');
  156. }
  157. return viewData;
  158. }
  159. });
  160. var AuthTokenView = OC.Backbone.View.extend({
  161. collection: null,
  162. _views: [],
  163. _form: undefined,
  164. _tokenName: undefined,
  165. _addAppPasswordBtn: undefined,
  166. _result: undefined,
  167. _newAppLoginName: undefined,
  168. _newAppPassword: undefined,
  169. _newAppId: undefined,
  170. _hideAppPasswordBtn: undefined,
  171. _addingToken: false,
  172. initialize: function (options) {
  173. this.collection = options.collection;
  174. var tokenTypes = [0, 1];
  175. var _this = this;
  176. _.each(tokenTypes, function (type) {
  177. var el = type === 0 ? '#sessions' : '#apppasswords';
  178. _this._views.push(new SubView({
  179. el: el,
  180. type: type,
  181. collection: _this.collection
  182. }));
  183. var $el = $(el);
  184. $('body').on('click', _.bind(_this._hideConfigureToken, _this));
  185. $el.on('click', '.popovermenu', function(event) {
  186. event.stopPropagation();
  187. });
  188. $el.on('click', 'a.icon-delete', _.bind(_this._onDeleteToken, _this));
  189. $el.on('click', '.icon-more', _.bind(_this._onConfigureToken, _this));
  190. $el.on('change', 'input.filesystem', _.bind(_this._onSetTokenScope, _this));
  191. });
  192. this._form = $('#app-password-form');
  193. this._tokenName = $('#app-password-name');
  194. this._addAppPasswordBtn = $('#add-app-password');
  195. this._addAppPasswordBtn.click(_.bind(this._addAppPassword, this));
  196. this._appPasswordName = $('#app-password-name');
  197. this._appPasswordName.on('keypress', function(event) {
  198. if (event.which === 13) {
  199. _this._addAppPassword();
  200. }
  201. });
  202. this._result = $('#app-password-result');
  203. this._newAppLoginName = $('#new-app-login-name');
  204. this._newAppLoginName.on('focus', _.bind(this._onNewTokenLoginNameFocus, this));
  205. this._newAppPassword = $('#new-app-password');
  206. this._newAppPassword.on('focus', _.bind(this._onNewTokenFocus, this));
  207. this._hideAppPasswordBtn = $('#app-password-hide');
  208. this._hideAppPasswordBtn.click(_.bind(this._hideToken, this));
  209. this._result.find('.clipboardButton').tooltip({placement: 'bottom', title: t('core', 'Copy'), trigger: 'hover'});
  210. // Clipboard!
  211. var clipboard = new Clipboard('.clipboardButton');
  212. clipboard.on('success', function(e) {
  213. var $input = $(e.trigger);
  214. $input.tooltip('hide')
  215. .attr('data-original-title', t('core', 'Copied!'))
  216. .tooltip('fixTitle')
  217. .tooltip({placement: 'bottom', trigger: 'manual'})
  218. .tooltip('show');
  219. _.delay(function() {
  220. $input.tooltip('hide')
  221. .attr('data-original-title', t('core', 'Copy'))
  222. .tooltip('fixTitle');
  223. }, 3000);
  224. });
  225. clipboard.on('error', function (e) {
  226. var $input = $(e.trigger);
  227. var actionMsg = '';
  228. if (/iPhone|iPad/i.test(navigator.userAgent)) {
  229. actionMsg = t('core', 'Not supported!');
  230. } else if (/Mac/i.test(navigator.userAgent)) {
  231. actionMsg = t('core', 'Press ⌘-C to copy.');
  232. } else {
  233. actionMsg = t('core', 'Press Ctrl-C to copy.');
  234. }
  235. $input.tooltip('hide')
  236. .attr('data-original-title', actionMsg)
  237. .tooltip('fixTitle')
  238. .tooltip({placement: 'bottom', trigger: 'manual'})
  239. .tooltip('show');
  240. _.delay(function () {
  241. $input.tooltip('hide')
  242. .attr('data-original-title', t('core', 'Copy'))
  243. .tooltip('fixTitle');
  244. }, 3000);
  245. });
  246. },
  247. render: function () {
  248. _.each(this._views, function (view) {
  249. view.render();
  250. view.toggleLoading(false);
  251. });
  252. },
  253. reload: function () {
  254. var _this = this;
  255. _.each(this._views, function (view) {
  256. view.toggleLoading(true);
  257. });
  258. var loadingTokens = this.collection.fetch();
  259. $.when(loadingTokens).done(function () {
  260. _this.render();
  261. });
  262. $.when(loadingTokens).fail(function () {
  263. OC.Notification.showTemporary(t('core', 'Error while loading browser sessions and device tokens'));
  264. });
  265. },
  266. _addAppPassword: function () {
  267. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  268. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._addAppPassword, this));
  269. return;
  270. }
  271. var _this = this;
  272. this._toggleAddingToken(true);
  273. var deviceName = this._tokenName.val() !== '' ? this._tokenName.val() : new Date();
  274. var creatingToken = $.ajax(OC.generateUrl('/settings/personal/authtokens'), {
  275. method: 'POST',
  276. data: {
  277. name: deviceName
  278. }
  279. });
  280. $.when(creatingToken).done(function (resp) {
  281. // We can delete token we add
  282. resp.deviceToken.canDelete = true;
  283. _this.collection.add(resp.deviceToken);
  284. _this.render();
  285. _this._newAppLoginName.val(resp.loginName);
  286. _this._newAppPassword.val(resp.token);
  287. _this._newAppId = resp.deviceToken.id;
  288. _this._toggleFormResult(false);
  289. _this._newAppPassword.select();
  290. _this._tokenName.val('');
  291. });
  292. $.when(creatingToken).fail(function () {
  293. OC.Notification.showTemporary(t('core', 'Error while creating device token'));
  294. });
  295. $.when(creatingToken).always(function () {
  296. _this._toggleAddingToken(false);
  297. });
  298. },
  299. _onNewTokenLoginNameFocus: function () {
  300. this._newAppLoginName.select();
  301. },
  302. _onNewTokenFocus: function () {
  303. this._newAppPassword.select();
  304. },
  305. _hideToken: function () {
  306. this._toggleFormResult(true);
  307. },
  308. _toggleAddingToken: function (state) {
  309. this._addingToken = state;
  310. this._addAppPasswordBtn.toggleClass('icon-loading-small', state);
  311. },
  312. _onConfigureToken: function (event) {
  313. event.stopPropagation();
  314. this._hideConfigureToken();
  315. var $target = $(event.target);
  316. var $row = $target.closest('tr');
  317. $row.toggleClass('active');
  318. var id = $row.data('id');
  319. },
  320. _hideConfigureToken: function() {
  321. $('.token-list tr').removeClass('active');
  322. },
  323. _onDeleteToken: function (event) {
  324. var $target = $(event.target);
  325. var $row = $target.closest('tr');
  326. var id = $row.data('id');
  327. if (id === this._newAppId) {
  328. this._toggleFormResult(true);
  329. }
  330. var token = this.collection.get(id);
  331. if (_.isUndefined(token)) {
  332. // Ignore event
  333. return;
  334. }
  335. var destroyingToken = token.destroy();
  336. $row.find('.icon-delete').tooltip('hide');
  337. var _this = this;
  338. $.when(destroyingToken).fail(function () {
  339. OC.Notification.showTemporary(t('core', 'Error while deleting the token'));
  340. });
  341. $.when(destroyingToken).always(function () {
  342. _this.render();
  343. });
  344. },
  345. _onSetTokenScope: function (event) {
  346. var $target = $(event.target);
  347. var $row = $target.closest('tr');
  348. var id = $row.data('id');
  349. var token = this.collection.get(id);
  350. if (_.isUndefined(token)) {
  351. // Ignore event
  352. return;
  353. }
  354. var scope = token.get('scope');
  355. scope.filesystem = $target.is(":checked");
  356. token.set('scope', scope);
  357. token.save();
  358. },
  359. _toggleFormResult: function (showForm) {
  360. if (showForm) {
  361. this._result.slideUp();
  362. this._form.slideDown();
  363. } else {
  364. this._form.slideUp();
  365. this._result.slideDown();
  366. }
  367. }
  368. });
  369. OC.Settings.AuthTokenView = AuthTokenView;
  370. })(OC, _, $, Handlebars, moment);