ui: implement OpenId login

This commit is contained in:
Dietmar Maurer 2021-06-14 11:58:52 +02:00
parent 3b7b1dfb8e
commit cd975e5787
3 changed files with 114 additions and 2 deletions

View File

@ -49,9 +49,15 @@ Ext.define('PBS.Application', {
var provider = new Ext.state.LocalStorageProvider({ prefix: 'ext-pbs-' }); var provider = new Ext.state.LocalStorageProvider({ prefix: 'ext-pbs-' });
Ext.state.Manager.setProvider(provider); Ext.state.Manager.setProvider(provider);
let openid_login = false;
let param = PBS.Utils.openid_login_param();
if (param !== undefined) {
openid_login = true;
}
// show login window if not loggedin // show login window if not loggedin
var loggedin = Proxmox.Utils.authOK(); var loggedin = Proxmox.Utils.authOK();
if (!loggedin) { if (openid_login || !loggedin) {
me.changeView('loginview', true); me.changeView('loginview', true);
} else { } else {
me.changeView('mainview', true); me.changeView('mainview', true);

View File

@ -2,6 +2,21 @@ Ext.define('PBS.LoginView', {
extend: 'Ext.container.Container', extend: 'Ext.container.Container',
xtype: 'loginview', xtype: 'loginview',
viewModel: {
data: {
openid: false,
},
formulas: {
button_text: function(get) {
if (get("openid") === true) {
return gettext("Login (OpenID redirect)");
} else {
return gettext("Login");
}
},
},
},
controller: { controller: {
xclass: 'Ext.app.ViewController', xclass: 'Ext.app.ViewController',
@ -15,8 +30,33 @@ Ext.define('PBS.LoginView', {
return; return;
} }
let redirect_url = location.origin;
let params = loginForm.getValues(); let params = loginForm.getValues();
if (this.getViewModel().data.openid === true) {
let realm = params.realm;
try {
let resp = await PBS.Async.api2({
url: '/api2/extjs/access/openid/auth-url',
params: {
realm: realm,
"redirect-url": redirect_url,
},
method: 'POST',
});
window.location = resp.result.data;
} catch (error) {
Proxmox.Utils.authClear();
loginForm.unmask();
Ext.MessageBox.alert(
gettext('Error'),
gettext('OpenId redirect failed. Please try again<br>Error: ' + error),
);
}
return;
}
params.username = params.username + '@' + params.realm; params.username = params.username + '@' + params.realm;
delete params.realm; delete params.realm;
@ -98,6 +138,14 @@ Ext.define('PBS.LoginView', {
window.location.reload(); window.location.reload();
}, },
}, },
'field[name=realm]': {
change: function(f, value) {
let record = f.store.getById(value);
if (record === undefined) return;
let data = record.data;
this.getViewModel().set("openid", data.type === "openid");
},
},
'button[reference=loginButton]': { 'button[reference=loginButton]': {
click: 'submitForm', click: 'submitForm',
}, },
@ -116,6 +164,43 @@ Ext.define('PBS.LoginView', {
var pwField = this.lookupReference('passwordField'); var pwField = this.lookupReference('passwordField');
pwField.focus(); pwField.focus();
} }
let param = PBS.Utils.openid_login_param();
if (param !== undefined) {
Proxmox.Utils.authClear();
let loginForm = this.lookupReference('loginForm');
loginForm.mask(gettext('OpenID login - please wait...'), 'x-mask-loading');
let redirect_url = location.origin;
Proxmox.Utils.API2Request({
url: '/api2/extjs/access/openid/login',
params: {
state: param.state,
code: param.code,
"redirect-url": redirect_url,
},
method: 'POST',
failure: function(response) {
loginForm.unmask();
Ext.MessageBox.alert(
gettext('Error'),
gettext('Login failed. Please try again<br>Error: ' + response.htmlStatus),
function() {
window.location = redirect_url;
},
);
},
success: function(response, options) {
loginForm.unmask();
let data = response.result.data;
PBS.Utils.updateLoginData(data);
PBS.app.changeView('mainview');
history.replaceState(null, '', redirect_url + '#pbsDashboard');
},
});
}
}, },
}, },
}, },
@ -191,6 +276,10 @@ Ext.define('PBS.LoginView', {
itemId: 'usernameField', itemId: 'usernameField',
reference: 'usernameField', reference: 'usernameField',
stateId: 'login-username', stateId: 'login-username',
bind: {
visible: "{!openid}",
disabled: "{openid}",
},
}, },
{ {
xtype: 'textfield', xtype: 'textfield',
@ -199,6 +288,10 @@ Ext.define('PBS.LoginView', {
name: 'password', name: 'password',
itemId: 'passwordField', itemId: 'passwordField',
reference: 'passwordField', reference: 'passwordField',
bind: {
visible: "{!openid}",
disabled: "{openid}",
},
}, },
{ {
xtype: 'pmxRealmComboBox', xtype: 'pmxRealmComboBox',
@ -223,9 +316,14 @@ Ext.define('PBS.LoginView', {
labelWidth: 250, labelWidth: 250,
labelAlign: 'right', labelAlign: 'right',
submitValue: false, submitValue: false,
bind: {
visible: "{!openid}",
},
}, },
{ {
text: gettext('Login'), bind: {
text: "{button_text}",
},
reference: 'loginButton', reference: 'loginButton',
formBind: true, formBind: true,
}, },

View File

@ -326,6 +326,14 @@ Ext.define('PBS.Utils', {
}; };
}, },
openid_login_param: function() {
let param = Ext.Object.fromQueryString(window.location.search);
if (param.state !== undefined && param.code !== undefined) {
return param;
}
return undefined;
},
calculate_dedup_factor: function(gcstatus) { calculate_dedup_factor: function(gcstatus) {
let dedup = 1.0; let dedup = 1.0;
if (gcstatus['disk-bytes'] > 0) { if (gcstatus['disk-bytes'] > 0) {