authtoken_view.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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._result = $('#app-password-result');
  197. this._newAppLoginName = $('#new-app-login-name');
  198. this._newAppLoginName.on('focus', _.bind(this._onNewTokenLoginNameFocus, this));
  199. this._newAppPassword = $('#new-app-password');
  200. this._newAppPassword.on('focus', _.bind(this._onNewTokenFocus, this));
  201. this._hideAppPasswordBtn = $('#app-password-hide');
  202. this._hideAppPasswordBtn.click(_.bind(this._hideToken, this));
  203. this._result.find('.clipboardButton').tooltip({placement: 'bottom', title: t('core', 'Copy'), trigger: 'hover'});
  204. // Clipboard!
  205. var clipboard = new Clipboard('.clipboardButton');
  206. clipboard.on('success', function(e) {
  207. var $input = $(e.trigger);
  208. $input.tooltip('hide')
  209. .attr('data-original-title', t('core', 'Copied!'))
  210. .tooltip('fixTitle')
  211. .tooltip({placement: 'bottom', trigger: 'manual'})
  212. .tooltip('show');
  213. _.delay(function() {
  214. $input.tooltip('hide')
  215. .attr('data-original-title', t('core', 'Copy'))
  216. .tooltip('fixTitle');
  217. }, 3000);
  218. });
  219. clipboard.on('error', function (e) {
  220. var $input = $(e.trigger);
  221. var actionMsg = '';
  222. if (/iPhone|iPad/i.test(navigator.userAgent)) {
  223. actionMsg = t('core', 'Not supported!');
  224. } else if (/Mac/i.test(navigator.userAgent)) {
  225. actionMsg = t('core', 'Press ⌘-C to copy.');
  226. } else {
  227. actionMsg = t('core', 'Press Ctrl-C to copy.');
  228. }
  229. $input.tooltip('hide')
  230. .attr('data-original-title', actionMsg)
  231. .tooltip('fixTitle')
  232. .tooltip({placement: 'bottom', trigger: 'manual'})
  233. .tooltip('show');
  234. _.delay(function () {
  235. $input.tooltip('hide')
  236. .attr('data-original-title', t('core', 'Copy'))
  237. .tooltip('fixTitle');
  238. }, 3000);
  239. });
  240. },
  241. render: function () {
  242. _.each(this._views, function (view) {
  243. view.render();
  244. view.toggleLoading(false);
  245. });
  246. },
  247. reload: function () {
  248. var _this = this;
  249. _.each(this._views, function (view) {
  250. view.toggleLoading(true);
  251. });
  252. var loadingTokens = this.collection.fetch();
  253. $.when(loadingTokens).done(function () {
  254. _this.render();
  255. });
  256. $.when(loadingTokens).fail(function () {
  257. OC.Notification.showTemporary(t('core', 'Error while loading browser sessions and device tokens'));
  258. });
  259. },
  260. _addAppPassword: function () {
  261. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  262. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._addAppPassword, this));
  263. return;
  264. }
  265. var _this = this;
  266. this._toggleAddingToken(true);
  267. var deviceName = this._tokenName.val() !== '' ? this._tokenName.val() : new Date();
  268. var creatingToken = $.ajax(OC.generateUrl('/settings/personal/authtokens'), {
  269. method: 'POST',
  270. data: {
  271. name: deviceName
  272. }
  273. });
  274. $.when(creatingToken).done(function (resp) {
  275. // We can delete token we add
  276. resp.deviceToken.canDelete = true;
  277. _this.collection.add(resp.deviceToken);
  278. _this.render();
  279. _this._newAppLoginName.val(resp.loginName);
  280. _this._newAppPassword.val(resp.token);
  281. _this._newAppId = resp.deviceToken.id;
  282. _this._toggleFormResult(false);
  283. _this._newAppPassword.select();
  284. _this._tokenName.val('');
  285. });
  286. $.when(creatingToken).fail(function () {
  287. OC.Notification.showTemporary(t('core', 'Error while creating device token'));
  288. });
  289. $.when(creatingToken).always(function () {
  290. _this._toggleAddingToken(false);
  291. });
  292. },
  293. _onNewTokenLoginNameFocus: function () {
  294. this._newAppLoginName.select();
  295. },
  296. _onNewTokenFocus: function () {
  297. this._newAppPassword.select();
  298. },
  299. _hideToken: function () {
  300. this._toggleFormResult(true);
  301. },
  302. _toggleAddingToken: function (state) {
  303. this._addingToken = state;
  304. this._addAppPasswordBtn.toggleClass('icon-loading-small', state);
  305. },
  306. _onConfigureToken: function (event) {
  307. event.stopPropagation();
  308. this._hideConfigureToken();
  309. var $target = $(event.target);
  310. var $row = $target.closest('tr');
  311. $row.toggleClass('active');
  312. var id = $row.data('id');
  313. },
  314. _hideConfigureToken: function() {
  315. $('.token-list tr').removeClass('active');
  316. },
  317. _onDeleteToken: function (event) {
  318. var $target = $(event.target);
  319. var $row = $target.closest('tr');
  320. var id = $row.data('id');
  321. if (id === this._newAppId) {
  322. this._toggleFormResult(true);
  323. }
  324. var token = this.collection.get(id);
  325. if (_.isUndefined(token)) {
  326. // Ignore event
  327. return;
  328. }
  329. var destroyingToken = token.destroy();
  330. $row.find('.icon-delete').tooltip('hide');
  331. var _this = this;
  332. $.when(destroyingToken).fail(function () {
  333. OC.Notification.showTemporary(t('core', 'Error while deleting the token'));
  334. });
  335. $.when(destroyingToken).always(function () {
  336. _this.render();
  337. });
  338. },
  339. _onSetTokenScope: function (event) {
  340. var $target = $(event.target);
  341. var $row = $target.closest('tr');
  342. var id = $row.data('id');
  343. var token = this.collection.get(id);
  344. if (_.isUndefined(token)) {
  345. // Ignore event
  346. return;
  347. }
  348. var scope = token.get('scope');
  349. scope.filesystem = $target.is(":checked");
  350. token.set('scope', scope);
  351. token.save();
  352. },
  353. _toggleFormResult: function (showForm) {
  354. if (showForm) {
  355. this._result.slideUp();
  356. this._form.slideDown();
  357. } else {
  358. this._form.slideUp();
  359. this._result.slideDown();
  360. }
  361. }
  362. });
  363. OC.Settings.AuthTokenView = AuthTokenView;
  364. })(OC, _, $, Handlebars, moment);