Debian Codegraph @

lemonldap-ng-manager/site/js-src/2ndfa.js

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