]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/window/LoginWindow.js
api network reload: code cleanup
[pve-manager.git] / www / manager6 / window / LoginWindow.js
CommitLineData
6a1c9c29 1/*global u2f*/
88d5be7d
DM
2Ext.define('PVE.window.LoginWindow', {
3 extend: 'Ext.window.Window',
4
04237985
DM
5 controller: {
6
7 xclass: 'Ext.app.ViewController',
da096950
DM
8
9 onLogon: function() {
10 var me = this;
11
12 var form = this.lookupReference('loginForm');
65532495
DC
13 var unField = this.lookupReference('usernameField');
14 var saveunField = this.lookupReference('saveunField');
da096950
DM
15 var view = this.getView();
16
24d2ed8c
WB
17 if (!form.isValid()) {
18 return;
19 }
20
24d2ed8c
WB
21 view.el.mask(gettext('Please wait...'), 'x-mask-loading');
22
23 // set or clear username
24 var sp = Ext.state.Manager.getProvider();
25 if (saveunField.getValue() === true) {
26 sp.set(unField.getStateId(), unField.getValue());
27 } else {
28 sp.clear(unField.getStateId());
29 }
30 sp.set(saveunField.getStateId(), saveunField.getValue());
31
32 form.submit({
33 failure: function(f, resp){
4c86db12 34 me.failure(resp);
24d2ed8c
WB
35 },
36 success: function(f, resp){
37 view.el.unmask();
da096950 38
24d2ed8c 39 var data = resp.result.data;
313eb589
WB
40 if (Ext.isDefined(data.NeedTFA)) {
41 // Store first factor login information first:
42 data.LoggedOut = true;
43 Proxmox.Utils.setAuthData(data);
44
45 if (Ext.isDefined(data.U2FChallenge)) {
46 me.perform_u2f(data);
47 } else {
48 me.perform_otp();
49 }
24d2ed8c 50 } else {
4c86db12 51 me.success(data);
24d2ed8c 52 }
65532495 53 }
24d2ed8c
WB
54 });
55
4c86db12
TL
56 },
57 failure: function(resp) {
58 var me = this;
59 var view = me.getView();
60 view.el.unmask();
61 var handler = function() {
62 var uf = me.lookupReference('usernameField');
63 uf.focus(true, true);
24d2ed8c 64 };
65532495 65
f41b67ab
OB
66 let emsg = gettext("Login failed. Please try again");
67
68 if (resp.failureType === "connect") {
e5d8aebb 69 emsg = gettext("Connection failure. Network error or Proxmox VE services not running?");
f41b67ab
OB
70 }
71
72 Ext.MessageBox.alert(gettext('Error'), emsg, handler);
4c86db12
TL
73 },
74 success: function(data) {
75 var me = this;
76 var view = me.getView();
77 var handler = view.handler || Ext.emptyFn;
78 handler.call(me, data);
79 view.close();
80 },
81
313eb589
WB
82 perform_otp: function() {
83 var me = this;
84 var win = Ext.create('PVE.window.TFALoginWindow', {
85 onLogin: function(value) {
86 me.finish_tfa(value);
87 },
88 onCancel: function() {
89 Proxmox.LoggedOut = false;
90 Proxmox.Utils.authClear();
91 me.getView().show();
92 }
93 });
94 win.show();
95 },
96
4c86db12
TL
97 perform_u2f: function(data) {
98 var me = this;
4c86db12
TL
99 // Show the message:
100 var msg = Ext.Msg.show({
101 title: 'U2F: '+gettext('Verification'),
102 message: gettext('Please press the button on your U2F Device'),
103 buttons: []
104 });
105 var chlg = data.U2FChallenge;
106 var key = {
107 version: chlg.version,
108 keyHandle: chlg.keyHandle
24d2ed8c 109 };
4c86db12
TL
110 u2f.sign(chlg.appId, chlg.challenge, [key], function(res) {
111 msg.close();
112 if (res.errorCode) {
113 Proxmox.Utils.authClear();
2d41c7e6 114 Ext.Msg.alert(gettext('Error'), PVE.Utils.render_u2f_error(res.errorCode));
4c86db12
TL
115 return;
116 }
117 delete res.errorCode;
313eb589 118 me.finish_tfa(JSON.stringify(res));
4c86db12
TL
119 });
120 },
313eb589 121 finish_tfa: function(res) {
4c86db12
TL
122 var me = this;
123 var view = me.getView();
124 view.el.mask(gettext('Please wait...'), 'x-mask-loading');
313eb589 125 var params = { response: res };
4c86db12
TL
126 Proxmox.Utils.API2Request({
127 url: '/api2/extjs/access/tfa',
128 params: params,
129 method: 'POST',
130 timeout: 5000, // it'll delay both success & failure
131 success: function(resp, opts) {
132 view.el.unmask();
133 // Fill in what we copy over from the 1st factor:
134 var data = resp.result.data;
135 data.CSRFPreventionToken = Proxmox.CSRFPreventionToken;
136 data.username = Proxmox.UserName;
137 // Finish logging in:
138 me.success(data);
139 },
140 failure: function(resp, opts) {
141 Proxmox.Utils.authClear();
142 me.failure(resp);
143 }
144 });
da096950
DM
145 },
146
147 control: {
148 'field[name=username]': {
149 specialkey: function(f, e) {
150 if (e.getKey() === e.ENTER) {
151 var pf = this.lookupReference('passwordField');
26031ab1 152 if (!pf.getValue()) {
da096950
DM
153 pf.focus(false);
154 }
155 }
156 }
157 },
da096950
DM
158 'field[name=lang]': {
159 change: function(f, value) {
160 var dt = Ext.Date.add(new Date(), Ext.Date.YEAR, 10);
161 Ext.util.Cookies.set('PVELangCookie', value, dt);
162 this.getView().mask(gettext('Please wait...'), 'x-mask-loading');
163 window.location.reload();
164 }
165 },
166 'button[reference=loginButton]': {
167 click: 'onLogon'
65532495
DC
168 },
169 '#': {
170 show: function() {
171 var sp = Ext.state.Manager.getProvider();
172 var checkboxField = this.lookupReference('saveunField');
173 var unField = this.lookupReference('usernameField');
174
175 var checked = sp.get(checkboxField.getStateId());
176 checkboxField.setValue(checked);
177
178 if(checked === true) {
179 var username = sp.get(unField.getStateId());
65532495
DC
180 unField.setValue(username);
181 var pwField = this.lookupReference('passwordField');
182 pwField.focus();
183 }
184 }
185 }
da096950 186 }
04237985 187 },
da096950 188
3262415b
DM
189 width: 400,
190
191 modal: true,
192
193 border: false,
194
195 draggable: true,
196
197 closable: false,
198
199 resizable: false,
200
201 layout: 'auto',
202
203 title: gettext('Proxmox VE Login'),
204
a765aeca
DC
205 defaultFocus: 'usernameField',
206
551456ff
DC
207 defaultButton: 'loginButton',
208
da096950
DM
209 items: [{
210 xtype: 'form',
211 layout: 'form',
212 url: '/api2/extjs/access/ticket',
213 reference: 'loginForm',
214
215 fieldDefaults: {
216 labelAlign: 'right',
217 allowBlank: false
218 },
219
220 items: [
221 {
222 xtype: 'textfield',
223 fieldLabel: gettext('User name'),
224 name: 'username',
a765aeca 225 itemId: 'usernameField',
da096950 226 reference: 'usernameField',
7e3e04e6 227 stateId: 'login-username'
da096950
DM
228 },
229 {
230 xtype: 'textfield',
231 inputType: 'password',
232 fieldLabel: gettext('Password'),
233 name: 'password',
7e3e04e6 234 reference: 'passwordField'
da096950 235 },
da096950
DM
236 {
237 xtype: 'pveRealmComboBox',
238 name: 'realm'
239 },
240 {
14580653 241 xtype: 'proxmoxLanguageSelector',
da096950 242 fieldLabel: gettext('Language'),
f4aa76c5 243 value: Ext.util.Cookies.get('PVELangCookie') || Proxmox.defaultLang || 'en',
da096950
DM
244 name: 'lang',
245 reference: 'langField',
246 submitValue: false
247 }
248 ],
249 buttons: [
65532495
DC
250 {
251 xtype: 'checkbox',
252 fieldLabel: gettext('Save User name'),
253 name: 'saveusername',
254 reference: 'saveunField',
255 stateId: 'login-saveusername',
256 labelWidth: 'auto',
257 labelAlign: 'right',
258 submitValue: false
259 },
da096950
DM
260 {
261 text: gettext('Login'),
262 reference: 'loginButton'
263 }
264 ]
265 }]
266 });
313eb589
WB
267Ext.define('PVE.window.TFALoginWindow', {
268 extend: 'Ext.window.Window',
269
270 modal: true,
271 resizable: false,
1102bcb1 272 title: 'Two-Factor Authentication',
313eb589
WB
273 layout: 'form',
274 defaultButton: 'loginButton',
275 defaultFocus: 'otpField',
276
277 controller: {
278 xclass: 'Ext.app.ViewController',
279 login: function() {
280 var me = this;
281 var view = me.getView();
6db00a09 282 view.onLogin(me.lookup('otpField').getValue());
313eb589
WB
283 view.close();
284 },
285 cancel: function() {
286 var me = this;
287 var view = me.getView();
288 view.onCancel();
289 view.close();
290 }
291 },
292
293 items: [
294 {
295 xtype: 'textfield',
1102bcb1 296 fieldLabel: gettext('Please enter your OTP verification code:'),
313eb589
WB
297 name: 'otp',
298 itemId: 'otpField',
299 reference: 'otpField',
7a42fc55
DC
300 allowBlank: false
301 }
313eb589
WB
302 ],
303
304 buttons: [
305 {
306 text: gettext('Login'),
307 reference: 'loginButton',
308 handler: 'login'
309 },
310 {
311 text: gettext('Cancel'),
312 handler: 'cancel'
313 }
314 ]
315});