Debian Codegraph @

lemonldap-ng-manager/site/htdocs/static/js/2ndfa.js

raw symbols SCIP
1 (function () {
2 'use strict';
3
4 /*
5 * 2ndFA Session explorer
6 */
7 /*
8 * AngularJS applications
9 */
10 var categories, hiddenAttributes, llapp, max, menu, overScheme, schemes;
11
12 // Max number of session to display (see overScheme)
13 max = 25;
14
15 // Queries to do each type of display: each array item corresponds to the depth
16 // of opened nodes in the tree
17 schemes = {
18 _whatToTrace: [function (t, v) {
19 return `groupBy=substr(${t},1)`;
20 }, function (t, v) {
21 return `${t}=${v}*`;
22 }]
23 };
24 overScheme = {
25 _whatToTrace: function (t, v, level, over) {
26 console.debug('overSchema => level', level, 'over', over);
27 if (level === 1 && v.length > over) {
28 return `${t}=${v}*&groupBy=substr(${t},${level + over + 1})`;
29 } else {
30 return null;
31 }
32 }
33 };
34 hiddenAttributes = '_password';
35
36 // Attributes to group in session display
37 categories = {
38 dateTitle: ['_utime', '_startTime', '_updateTime'],
39 sfaTitle: ['_2fDevices']
40 };
41
42 // Menu entries
43 menu = {
44 home: []
45 };
46 llapp = angular.module('llngSessionsExplorer', ['ui.tree', 'ui.bootstrap', 'llApp']);
47
48 // Main controller
49 llapp.controller('SessionsExplorerCtrl', ['$scope', '$translator', '$location', '$q', '$http', function ($scope, $translator, $location, $q, $http) {
50 var autoId, c, pathEvent, sessionType;
51 $scope.links = links;
52 $scope.menulinks = menulinks;
53 $scope.staticPrefix = staticPrefix;
54 $scope.scriptname = scriptname;
55 $scope.formPrefix = formPrefix;
56 $scope.availableLanguages = availableLanguages;
57 $scope.waiting = true;
58 $scope.showM = false;
59 $scope.showT = true;
60 $scope.data = [];
61 $scope.currentScope = null;
62 $scope.currentSession = null;
63 $scope.menu = menu;
64 $scope.searchString = '';
65 $scope.sfatypes = {};
66 // Import translations functions
67 $scope.translateP = $translator.translateP;
68 $scope.translate = $translator.translate;
69 $scope.translateTitle = function (node) {
70 return $translator.translateField(node, 'title');
71 };
72 sessionType = 'persistent';
73 // Handle menu items
74 $scope.menuClick = function (button) {
75 if (button.popup) {
76 window.open(button.popup);
77 } else {
78 if (!button.action) {
79 button.action = button.title;
80 }
81 switch (typeof button.action) {
82 case 'function':
83 button.action($scope.currentNode, $scope);
84 $scope[button.action]();
85 break;
86 case 'string':
87 $scope[button.action]();
88 break;
89 default:
90 console.warn('Unknown action type', typeof button.action);
91 }
92 }
93 return $scope.showM = false;
94 };
95 //# SESSIONS MANAGEMENT
96 // Search 2FA sessions
97 $scope.search2FA = function (clear) {
98 if (clear) {
99 $scope.searchString = '';
100 }
101 $scope.currentSession = null;
102 $scope.data = [];
103 return $scope.updateTree2('', $scope.data, 0, 0);
104 };
105
106 // Delete 2FA device
107 $scope.delete2FA = function (type, epoch) {
108 var e, i, items, len;
109 items = document.querySelectorAll(`.data-${epoch}`);
110 for (i = 0, len = items.length; i < len; i++) {
111 e = items[i];
112 e.remove();
113 }
114 $scope.waiting = true;
115 $http['delete'](`${scriptname}sfa/${sessionType}/${$scope.currentSession.id}?type=${type}&epoch=${epoch}`).then(function (response) {
116 return $scope.waiting = false;
117 }, function (resp) {
118 return $scope.waiting = false;
119 });
120 return $scope.showT = false;
121 };
122 // Open node
123 $scope.stoggle = function (scope) {
124 var node;
125 node = scope.$modelValue;
126 if (node.nodes.length === 0) {
127 $scope.updateTree(node.value, node.nodes, node.level, node.over, node.query, node.count);
128 }
129 return scope.toggle();
130 };
131 // Display selected session
132 $scope.displaySession = function (scope) {
133 var sessionId, transformSession;
134 // Private functions
135
136 // Session preparation
137 transformSession = function (session) {
138 var _stToStr, array, arrayDate, attr, attrs, category, epoch, i, k, key, len, len1, name, pattern, res, sfDevice, subres, time, type, value;
139 _stToStr = function (s) {
140 return s;
141 };
142 time = session._utime;
143 // 1. Replace values if needed
144 for (key in session) {
145 value = session[key];
146 if (!value) {
147 delete session[key];
148 } else {
149 if (typeof session === 'string' && value.match(/; /)) {
150 session[key] = value.split('; ');
151 }
152 if (typeof session[key] !== 'object') {
153 if (hiddenAttributes.match(new RegExp('\b' + key + '\b'))) {
154 session[key] = '********';
155 } else if (key.match(/^(_utime|_lastAuthnUTime|_lastSeen|notification)$/)) {
156 session[key] = $scope.localeDate(value);
157 } else if (key.match(/^(_startTime|_updateTime)$/)) {
158 value = _stToStr(value);
159 pattern = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/;
160 arrayDate = value.match(pattern);
161 session[key] = `${arrayDate[3]}/${arrayDate[2]}/${arrayDate[1]} à ${arrayDate[4]}:${arrayDate[5]}:${arrayDate[6]}`;
162 }
163 }
164 }
165 }
166 res = [];
167 // 2. Push session keys in result, grouped by categories
168 for (category in categories) {
169 attrs = categories[category];
170 subres = [];
171 for (i = 0, len = attrs.length; i < len; i++) {
172 attr = attrs[i];
173 if (session[attr]) {
174 if (attr === "_2fDevices" && session[attr]) {
175 array = JSON.parse(session[attr]);
176 if (array.length > 0) {
177 subres.push({
178 title: "2fid",
179 value: "name",
180 type: "type",
181 epoch: "date"
182 });
183 for (k = 0, len1 = array.length; k < len1; k++) {
184 sfDevice = array[k];
185 for (key in sfDevice) {
186 value = sfDevice[key];
187 if (key === 'type') {
188 type = value;
189 }
190 if (key === 'name') {
191 name = value;
192 }
193 if (key === 'epoch') {
194 epoch = value;
195 }
196 }
197 subres.push({
198 title: '[' + type + ']' + epoch,
199 type: type,
200 value: name,
201 epoch: epoch,
202 sfrow: true
203 });
204 }
205 }
206 delete session[attr];
207 } else if (session[attr].toString().match(/\w+/)) {
208 subres.push({
209 title: attr,
210 value: session[attr]
211 });
212 delete session[attr];
213 } else {
214 delete session[attr];
215 }
216 } else {
217 delete session[attr];
218 }
219 }
220 if (subres.length > 0) {
221 res.push({
222 title: `__${category}__`,
223 nodes: subres
224 });
225 }
226 }
227 return {
228 _utime: time,
229 nodes: res
230 };
231 };
232 $scope.currentScope = scope;
233 sessionId = scope.$modelValue.session;
234 $http.get(`${scriptname}sfa/${sessionType}/${sessionId}`).then(function (response) {
235 $scope.currentSession = transformSession(response.data);
236 return $scope.currentSession.id = sessionId;
237 });
238 return $scope.showT = false;
239 };
240 $scope.localeDate = function (s) {
241 var d;
242 d = new Date(s * 1000);
243 return d.toLocaleString();
244 };
245 // Function to change interface language
246 $scope.getLanguage = function (lang) {
247 $scope.lang = lang;
248 $scope.form = 'white';
249 $scope.init();
250 return $scope.showM = false;
251 };
252 // URI local path management
253 pathEvent = function (event, next, current) {
254 var n;
255 n = next.match(/#!?\/(\w+)/);
256 if (n === null || n[1].match(/^(persistent)$/)) {
257 $scope.type = '_session_uid';
258 }
259 return $scope.init();
260 };
261 $scope.$on('$locationChangeSuccess', pathEvent);
262 // Functions to update tree: download value of opened subkey
263 autoId = 0;
264 $scope.updateTree = function (value, node, level, over, currentQuery, count) {
265 var query, scheme, tmp;
266 $scope.waiting = true;
267 // Query scheme selection:
268
269 // - if defined above
270 // - default to _whatToTrace scheme
271 scheme = schemes[$scope.type] ? schemes[$scope.type] : schemes._whatToTrace;
272 // Build query using schemes
273 query = scheme[level]($scope.type, value, currentQuery);
274 // If number of session exceeds "max" and overScheme exists, call it
275 if (count > max && overScheme[$scope.type]) {
276 if (tmp = overScheme[$scope.type]($scope.type, value, level, over, currentQuery)) {
277 over++;
278 query = tmp;
279 level = level - 1;
280 } else {
281 over = 0;
282 }
283 } else {
284 over = 0;
285 }
286 // Launch HTTP query
287 return $http.get(`${scriptname}sfa/${sessionType}?${query}` + Object.entries($scope.sfatypes).map(function (x) {
288 if (x[1]) {
289 return "&type=" + x[0];
290 } else {
291 return "";
292 }
293 }).join("")).then(function (response) {
294 var data, i, len, n, ref;
295 data = response.data;
296 if (data.result) {
297 ref = data.values;
298 for (i = 0, len = ref.length; i < len; i++) {
299 n = ref[i];
300 autoId++;
301 n.id = `node${autoId}`;
302 if (level < scheme.length - 1) {
303 n.nodes = [];
304 n.level = level + 1;
305 n.query = query;
306 n.over = over;
307 }
308 node.push(n);
309 }
310 if (value === '') {
311 $scope.total = data.total;
312 }
313 }
314 return $scope.waiting = false;
315 }, function (resp) {
316 return $scope.waiting = false;
317 });
318 };
319
320 // Functions to filter U2F sessions tree : download value of opened subkey
321 $scope.updateTree2 = function (value, node, level, over, currentQuery, count) {
322 var query, scheme, tmp;
323 $scope.waiting = true;
324 // Query scheme selection:
325
326 // - if defined above
327 // - _updateTime must be displayed as startDate
328 // - default to _whatToTrace scheme
329 scheme = schemes[$scope.type] ? schemes[$scope.type] : $scope.type === '_updateTime' ? schemes._startTime : schemes._whatToTrace;
330 // Build query using schemes
331 query = scheme[level]($scope.type, value, currentQuery);
332 // If number of session exceeds "max" and overScheme exists, call it
333 if (count > max && overScheme[$scope.type]) {
334 if (tmp = overScheme[$scope.type]($scope.type, value, level, over, currentQuery)) {
335 over++;
336 query = tmp;
337 level = level - 1;
338 } else {
339 over = 0;
340 }
341 } else {
342 over = 0;
343 }
344 // Launch HTTP
345 return $http.get(`${scriptname}sfa/${sessionType}?_session_uid=${$scope.searchString}*&groupBy=substr(_session_uid,${$scope.searchString.length})` + Object.entries($scope.sfatypes).map(function (x) {
346 if (x[1]) {
347 return "&type=" + x[0];
348 } else {
349 return "";
350 }
351 }).join("")).then(function (response) {
352 var data, i, len, n, ref;
353 data = response.data;
354 if (data.result) {
355 ref = data.values;
356 for (i = 0, len = ref.length; i < len; i++) {
357 n = ref[i];
358 autoId++;
359 n.id = `node${autoId}`;
360 if (level < scheme.length - 1) {
361 n.nodes = [];
362 n.level = level + 1;
363 n.query = query;
364 n.over = over;
365 }
366 node.push(n);
367 }
368 if (value === '') {
369 $scope.total = data.total;
370 }
371 }
372 return $scope.waiting = false;
373 }, function (resp) {
374 return $scope.waiting = false;
375 });
376 };
377 // Intialization function
378 // Simply set $scope.waiting to false during $translator and tree root
379 // initialization
380 $scope.init = function () {
381 $scope.waiting = true;
382 $scope.data = [];
383 $q.all([$translator.init($scope.lang), $scope.updateTree('', $scope.data, 0, 0)]).then(function () {
384 return $scope.waiting = false;
385 }, function (resp) {
386 return $scope.waiting = false;
387 });
388 // Colorized link
389 $scope.activeModule = "2ndFA";
390 return $scope.myStyle = {
391 color: '#ffb84d'
392 };
393 };
394 // Query scheme initialization
395 // Default to '_whatToTrace'
396 c = $location.path().match(/^\/(\w+)/);
397 return $scope.type = c ? c[1] : '_whatToTrace';
398 }]);
399
400 })();