]>
git.proxmox.com Git - pve-manager.git/blob - www/manager6/window/LoginWindow.js
2 Ext
.define('PVE.window.LoginWindow', {
3 extend
: 'Ext.window.Window',
10 button_text: function(get) {
11 if (get("openid") === true) {
12 return gettext("Login (OpenID redirect)");
14 return gettext("Login");
22 xclass
: 'Ext.app.ViewController',
24 onLogon
: async
function() {
27 var form
= this.lookupReference('loginForm');
28 var unField
= this.lookupReference('usernameField');
29 var saveunField
= this.lookupReference('saveunField');
30 var view
= this.getView();
32 if (!form
.isValid()) {
36 let creds
= form
.getValues();
38 if (this.getViewModel().data
.openid
=== true) {
39 const redirectURL
= location
.origin
;
40 Proxmox
.Utils
.API2Request({
41 url
: '/api2/extjs/access/openid/auth-url',
44 "redirect-url": redirectURL
,
47 success: function(resp
, opts
) {
48 window
.location
= resp
.result
.data
;
50 failure: function(resp
, opts
) {
51 Proxmox
.Utils
.authClear();
55 gettext('OpenID redirect failed.') + `<br>${resp.htmlStatus}`,
62 view
.el
.mask(gettext('Please wait...'), 'x-mask-loading');
64 // set or clear username
65 var sp
= Ext
.state
.Manager
.getProvider();
66 if (saveunField
.getValue() === true) {
67 sp
.set(unField
.getStateId(), unField
.getValue());
69 sp
.clear(unField
.getStateId());
71 sp
.set(saveunField
.getStateId(), saveunField
.getValue());
74 // Request updated authentication mechanism:
75 creds
['new-format'] = 1;
77 let resp
= await Proxmox
.Async
.api2({
78 url
: '/api2/extjs/access/ticket',
83 let data
= resp
.result
.data
;
84 if (data
.ticket
.startsWith("PVE:!tfa!")) {
85 // Store first factor login information first:
86 data
.LoggedOut
= true;
87 Proxmox
.Utils
.setAuthData(data
);
89 data
= await me
.performTFAChallenge(data
);
91 // Fill in what we copy over from the 1st factor:
92 data
.CSRFPreventionToken
= Proxmox
.CSRFPreventionToken
;
93 data
.username
= Proxmox
.UserName
;
95 } else if (Ext
.isDefined(data
.NeedTFA
)) {
96 // Store first factor login information first:
97 data
.LoggedOut
= true;
98 Proxmox
.Utils
.setAuthData(data
);
100 if (Ext
.isDefined(data
.U2FChallenge
)) {
101 me
.perform_u2f(data
);
113 /* START NEW TFA CODE (pbs copy) */
114 performTFAChallenge
: async
function(data
) {
117 let userid
= data
.username
;
118 let ticket
= data
.ticket
;
119 let challenge
= JSON
.parse(decodeURIComponent(
120 ticket
.split(':')[1].slice("!tfa!".length
),
123 let resp
= await
new Promise((resolve
, reject
) => {
124 Ext
.create('Proxmox.window.TfaLoginWindow', {
128 onResolve
: value
=> resolve(value
),
133 return resp
.result
.data
;
135 /* END NEW TFA CODE (pbs copy) */
137 failure: function(resp
) {
139 var view
= me
.getView();
141 var handler = function() {
142 var uf
= me
.lookupReference('usernameField');
143 uf
.focus(true, true);
146 let emsg
= gettext("Login failed. Please try again");
148 if (resp
.failureType
=== "connect") {
149 emsg
= gettext("Connection failure. Network error or Proxmox VE services not running?");
152 Ext
.MessageBox
.alert(gettext('Error'), emsg
, handler
);
154 success: function(data
) {
156 var view
= me
.getView();
157 var handler
= view
.handler
|| Ext
.emptyFn
;
158 handler
.call(me
, data
);
162 perform_otp: function() {
164 var win
= Ext
.create('PVE.window.TFALoginWindow', {
165 onLogin: function(value
) {
166 me
.finish_tfa(value
);
168 onCancel: function() {
169 Proxmox
.LoggedOut
= false;
170 Proxmox
.Utils
.authClear();
177 perform_u2f: function(data
) {
180 var msg
= Ext
.Msg
.show({
181 title
: 'U2F: '+gettext('Verification'),
182 message
: gettext('Please press the button on your U2F Device'),
185 var chlg
= data
.U2FChallenge
;
187 version
: chlg
.version
,
188 keyHandle
: chlg
.keyHandle
,
190 u2f
.sign(chlg
.appId
, chlg
.challenge
, [key
], function(res
) {
193 Proxmox
.Utils
.authClear();
194 Ext
.Msg
.alert(gettext('Error'), PVE
.Utils
.render_u2f_error(res
.errorCode
));
197 delete res
.errorCode
;
198 me
.finish_tfa(JSON
.stringify(res
));
201 finish_tfa: function(res
) {
203 var view
= me
.getView();
204 view
.el
.mask(gettext('Please wait...'), 'x-mask-loading');
205 Proxmox
.Utils
.API2Request({
206 url
: '/api2/extjs/access/tfa',
211 timeout
: 5000, // it'll delay both success & failure
212 success: function(resp
, opts
) {
214 // Fill in what we copy over from the 1st factor:
215 var data
= resp
.result
.data
;
216 data
.CSRFPreventionToken
= Proxmox
.CSRFPreventionToken
;
217 data
.username
= Proxmox
.UserName
;
218 // Finish logging in:
221 failure: function(resp
, opts
) {
222 Proxmox
.Utils
.authClear();
229 'field[name=username]': {
230 specialkey: function(f
, e
) {
231 if (e
.getKey() === e
.ENTER
) {
232 var pf
= this.lookupReference('passwordField');
233 if (!pf
.getValue()) {
239 'field[name=lang]': {
240 change: function(f
, value
) {
241 var dt
= Ext
.Date
.add(new Date(), Ext
.Date
.YEAR
, 10);
242 Ext
.util
.Cookies
.set('PVELangCookie', value
, dt
);
243 this.getView().mask(gettext('Please wait...'), 'x-mask-loading');
244 window
.location
.reload();
247 'field[name=realm]': {
248 change: function(f
, value
) {
249 let record
= f
.store
.getById(value
);
250 if (record
=== undefined) return;
251 let data
= record
.data
;
252 this.getViewModel().set("openid", data
.type
=== "openid");
255 'button[reference=loginButton]': {
262 var sp
= Ext
.state
.Manager
.getProvider();
263 var checkboxField
= this.lookupReference('saveunField');
264 var unField
= this.lookupReference('usernameField');
266 var checked
= sp
.get(checkboxField
.getStateId());
267 checkboxField
.setValue(checked
);
269 if (checked
=== true) {
270 var username
= sp
.get(unField
.getStateId());
271 unField
.setValue(username
);
272 var pwField
= this.lookupReference('passwordField');
276 let auth
= Proxmox
.Utils
.getOpenIDRedirectionAuthorization();
277 if (auth
!== undefined) {
278 Proxmox
.Utils
.authClear();
280 let loginForm
= this.lookupReference('loginForm');
281 loginForm
.mask(gettext('OpenID login - please wait...'), 'x-mask-loading');
283 const redirectURL
= location
.origin
;
285 Proxmox
.Utils
.API2Request({
286 url
: '/api2/extjs/access/openid/login',
290 "redirect-url": redirectURL
,
293 failure: function(response
) {
295 let error
= response
.htmlStatus
;
296 Ext
.MessageBox
.alert(
298 gettext('OpenID login failed, please try again') + `<br>${error}`,
299 () => { window
.location
= redirectURL
; },
302 success: function(response
, options
) {
304 let data
= response
.result
.data
;
305 history
.replaceState(null, '', redirectURL
);
323 title
: gettext('Proxmox VE Login'),
325 defaultFocus
: 'usernameField',
326 defaultButton
: 'loginButton',
331 url
: '/api2/extjs/access/ticket',
332 reference
: 'loginForm',
342 fieldLabel
: gettext('User name'),
344 itemId
: 'usernameField',
345 reference
: 'usernameField',
346 stateId
: 'login-username',
348 visible
: "{!openid}",
349 disabled
: "{openid}",
354 inputType
: 'password',
355 fieldLabel
: gettext('Password'),
357 reference
: 'passwordField',
359 visible
: "{!openid}",
360 disabled
: "{openid}",
364 xtype
: 'pmxRealmComboBox',
368 xtype
: 'proxmoxLanguageSelector',
369 fieldLabel
: gettext('Language'),
370 value
: PVE
.Utils
.getUiLanguage(),
372 reference
: 'langField',
379 fieldLabel
: gettext('Save User name'),
380 name
: 'saveusername',
381 reference
: 'saveunField',
382 stateId
: 'login-saveusername',
387 visible
: "{!openid}",
392 text
: "{button_text}",
394 reference
: 'loginButton',