summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-csshnpd/htdocs/luci-static/resources/view/sshnpd/enroll.js
blob: a97cefb9585e4019b0309eae1b247527c113d96b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
'require view';
'require dom';
'require fs';
'require ui';
'require uci';
'require network';


return view.extend({
	handleCommand: function(exec, args) {
		let buttons = document.querySelectorAll('.diag-action > .cbi-button');

		for (let i = 0; i < buttons.length; i++)
			buttons[i].setAttribute('disabled', 'true');

		return fs.exec(exec, args).then(function(res) {
			let out = document.querySelector('textarea');

			dom.content(out, [ res.stdout || '', res.stderr || '' ]);
		}).catch(function(err) {
			ui.addNotification(null, E('p', [ err ]))
		}).finally(function() {
			for (let i = 0; i < buttons.length; i++)
				buttons[i].removeAttribute('disabled');
		});
	},

	handleEnroll: function() {
		return this.handleCommand('at_enroll.sh', "");
	},

	load: function() {
		return uci.load('sshnpd').then(function() {
			let atsign = uci.get_first('sshnpd','','atsign'),
				keyfile = '/root/.atsign/keys/'+atsign+'_key.atKeys';
				return L.resolveDefault(fs.stat(keyfile), {});
		});
	},

	render: function(res) {

		const has_atkey = res.path;
		const atsign = uci.get_first('sshnpd','','atsign');
		const device = uci.get_first('sshnpd','','device');
		const otp = uci.get_first('sshnpd','','otp');
		const enrollready = atsign && device && otp && !has_atkey;

		const instructions = E('div', { 'class': 'cbi-map-descr'}, _('Press the Enroll button then run this command on a system where '+atsign+' is activated:'));

		const enrollcmd = E('code','at_activate approve -a '+atsign+' --arx noports --drx '+device);

		let table = E('table', { 'class': 'table' }, [
				E('tr', { 'class': 'tr' }, [
					E('td', { 'class': 'td left' }, [
						E('span', { 'class': 'diag-action' }, [
							E('button', {
								'class': 'cbi-button cbi-button-action',
								'click': ui.createHandlerFn(this, 'handleEnroll')
							}, [ _('Enroll') ])
						])
					]),
				])
			]);

		const cmdwindow = E('div', {'class': 'cbi-section'}, [
			E('div', { 'id' : 'command-output'},
				E('textarea', {
					'id': 'widget.command-output',
					'style': 'width: 100%; font-family:monospace; white-space:pre',
					'readonly': true,
					'wrap': 'on',
					'rows': '20'
				})
			)
			]);

		let view = E('div', { 'class': 'cbi-map'}, [
			E('h2', {}, [ _('NoPorts atSign Enrollment') ]),
			atsign ? E([]) : E('div', { 'class': 'cbi-map-descr'}, _('atSign must be configured')),
			device ? E([]) : E('div', { 'class': 'cbi-map-descr'}, _('Device must be configured')),
			otp ? E([]) : E('div', { 'class': 'cbi-map-descr'}, _('OTP must be configured. An OTP can be generated using:')),
			otp ? E([]) : E('code','at_activate otp -a '+atsign),
			has_atkey ? E('div', { 'class': 'cbi-map-descr'}, _('Existing key found at: '+has_atkey)) : E([]),
			enrollready ? instructions : E([]),
			enrollready ? enrollcmd  : E([]),
			enrollready ? table : E([]),
			enrollready ? cmdwindow : E([]),
		]);

		return view;
	},

	handleSaveApply: null,
	handleSave: null,
	handleReset: null
});