2020-11-02 13:36:10 +00:00
|
|
|
Ext.define('PBS.window.AddWebauthn', {
|
|
|
|
extend: 'Ext.window.Window',
|
|
|
|
alias: 'widget.pbsAddWebauthn',
|
|
|
|
mixins: ['Proxmox.Mixin.CBind'],
|
|
|
|
|
|
|
|
onlineHelp: 'user_mgmt',
|
|
|
|
|
|
|
|
modal: true,
|
|
|
|
resizable: false,
|
|
|
|
title: gettext('Add a Webauthn login token'),
|
|
|
|
width: 512,
|
|
|
|
|
|
|
|
user: undefined,
|
|
|
|
fixedUser: false,
|
|
|
|
|
|
|
|
initComponent: function() {
|
|
|
|
let me = this;
|
|
|
|
me.callParent();
|
|
|
|
Ext.GlobalEvents.fireEvent('proxmoxShowHelp', me.onlineHelp);
|
|
|
|
},
|
|
|
|
|
|
|
|
viewModel: {
|
|
|
|
data: {
|
|
|
|
valid: false,
|
2021-01-21 14:06:15 +00:00
|
|
|
userid: null,
|
2021-01-18 09:12:21 +00:00
|
|
|
},
|
2020-11-02 13:36:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
controller: {
|
|
|
|
xclass: 'Ext.app.ViewController',
|
|
|
|
|
|
|
|
control: {
|
|
|
|
'field': {
|
|
|
|
validitychange: function(field, valid) {
|
|
|
|
let me = this;
|
|
|
|
let viewmodel = me.getViewModel();
|
|
|
|
let form = me.lookup('webauthn_form');
|
|
|
|
viewmodel.set('valid', form.isValid());
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'#': {
|
|
|
|
show: function() {
|
|
|
|
let me = this;
|
|
|
|
let view = me.getView();
|
|
|
|
|
|
|
|
if (Proxmox.UserName === 'root@pam') {
|
|
|
|
view.lookup('password').setVisible(false);
|
|
|
|
view.lookup('password').setDisabled(true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
registerWebauthn: async function() {
|
|
|
|
let me = this;
|
|
|
|
let values = me.lookup('webauthn_form').getValues();
|
|
|
|
values.type = "webauthn";
|
|
|
|
|
|
|
|
let userid = values.user;
|
|
|
|
delete values.user;
|
|
|
|
|
2021-01-08 10:54:12 +00:00
|
|
|
me.getView().mask(gettext('Please wait...'), 'x-mask-loading');
|
|
|
|
|
2020-11-02 13:36:10 +00:00
|
|
|
try {
|
2021-07-09 14:53:11 +00:00
|
|
|
let register_response = await Proxmox.Async.api2({
|
2020-11-02 13:36:10 +00:00
|
|
|
url: `/api2/extjs/access/tfa/${userid}`,
|
|
|
|
method: 'POST',
|
|
|
|
params: values,
|
|
|
|
});
|
|
|
|
|
|
|
|
let data = register_response.result.data;
|
|
|
|
if (!data.challenge) {
|
|
|
|
throw "server did not respond with a challenge";
|
|
|
|
}
|
|
|
|
|
2021-03-03 13:05:02 +00:00
|
|
|
let creds = JSON.parse(data.challenge);
|
2020-11-02 13:36:10 +00:00
|
|
|
|
|
|
|
// Fix this up before passing it to the browser, but keep a copy of the original
|
|
|
|
// string to pass in the response:
|
2021-03-03 13:05:02 +00:00
|
|
|
let challenge_str = creds.publicKey.challenge;
|
|
|
|
creds.publicKey.challenge = PBS.Utils.base64url_to_bytes(challenge_str);
|
|
|
|
creds.publicKey.user.id =
|
|
|
|
PBS.Utils.base64url_to_bytes(creds.publicKey.user.id);
|
2020-11-02 13:36:10 +00:00
|
|
|
|
2021-02-25 09:01:21 +00:00
|
|
|
// convert existing authenticators structure
|
2021-03-03 13:05:02 +00:00
|
|
|
creds.publicKey.excludeCredentials =
|
|
|
|
(creds.publicKey.excludeCredentials || [])
|
|
|
|
.map((credential) => ({
|
|
|
|
id: PBS.Utils.base64url_to_bytes(credential.id),
|
|
|
|
type: credential.type,
|
2021-02-25 09:01:21 +00:00
|
|
|
}));
|
|
|
|
|
2020-11-02 13:36:10 +00:00
|
|
|
let msg = Ext.Msg.show({
|
|
|
|
title: `Webauthn: ${gettext('Setup')}`,
|
|
|
|
message: gettext('Please press the button on your Webauthn Device'),
|
|
|
|
buttons: [],
|
|
|
|
});
|
|
|
|
|
2021-02-25 09:01:21 +00:00
|
|
|
let token_response;
|
|
|
|
try {
|
2021-03-03 13:05:02 +00:00
|
|
|
token_response = await navigator.credentials.create(creds);
|
2021-02-25 09:01:21 +00:00
|
|
|
} catch (error) {
|
2021-03-03 12:59:58 +00:00
|
|
|
let errmsg = error.message;
|
2021-02-25 09:01:21 +00:00
|
|
|
if (error.name === 'InvalidStateError') {
|
2021-03-03 12:59:58 +00:00
|
|
|
errmsg = gettext('Is this token already registered?');
|
2021-02-25 09:01:21 +00:00
|
|
|
}
|
2021-03-03 12:59:58 +00:00
|
|
|
throw gettext('An error occurred during token registration.') +
|
|
|
|
`<br>${error.name}: ${errmsg}`;
|
2021-02-25 09:01:21 +00:00
|
|
|
}
|
2020-11-02 13:36:10 +00:00
|
|
|
|
|
|
|
// We cannot pass ArrayBuffers to the API, so extract & convert the data.
|
|
|
|
let response = {
|
|
|
|
id: token_response.id,
|
|
|
|
type: token_response.type,
|
|
|
|
rawId: PBS.Utils.bytes_to_base64url(token_response.rawId),
|
|
|
|
response: {
|
|
|
|
attestationObject: PBS.Utils.bytes_to_base64url(
|
|
|
|
token_response.response.attestationObject,
|
|
|
|
),
|
|
|
|
clientDataJSON: PBS.Utils.bytes_to_base64url(
|
|
|
|
token_response.response.clientDataJSON,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
msg.close();
|
|
|
|
|
|
|
|
let params = {
|
|
|
|
type: "webauthn",
|
|
|
|
challenge: challenge_str,
|
|
|
|
value: JSON.stringify(response),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (values.password) {
|
|
|
|
params.password = values.password;
|
|
|
|
}
|
|
|
|
|
2021-07-09 14:53:11 +00:00
|
|
|
await Proxmox.Async.api2({
|
2020-11-02 13:36:10 +00:00
|
|
|
url: `/api2/extjs/access/tfa/${userid}`,
|
|
|
|
method: 'POST',
|
|
|
|
params,
|
|
|
|
});
|
2021-07-09 14:53:11 +00:00
|
|
|
} catch (response) {
|
|
|
|
let error = response.result.message;
|
2020-11-02 13:36:10 +00:00
|
|
|
console.error(error); // for debugging if it's not displayable...
|
|
|
|
Ext.Msg.alert(gettext('Error'), error);
|
|
|
|
}
|
|
|
|
|
|
|
|
me.getView().close();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
xtype: 'form',
|
|
|
|
reference: 'webauthn_form',
|
|
|
|
layout: 'anchor',
|
2021-01-13 11:06:54 +00:00
|
|
|
border: false,
|
2020-11-02 13:36:10 +00:00
|
|
|
bodyPadding: 10,
|
|
|
|
fieldDefaults: {
|
|
|
|
anchor: '100%',
|
|
|
|
},
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
xtype: 'pmxDisplayEditField',
|
|
|
|
name: 'user',
|
|
|
|
cbind: {
|
|
|
|
editable: (get) => !get('fixedUser'),
|
2021-01-21 14:06:15 +00:00
|
|
|
value: () => Proxmox.UserName,
|
2020-11-02 13:36:10 +00:00
|
|
|
},
|
|
|
|
fieldLabel: gettext('User'),
|
|
|
|
editConfig: {
|
|
|
|
xtype: 'pbsUserSelector',
|
|
|
|
allowBlank: false,
|
|
|
|
},
|
|
|
|
renderer: Ext.String.htmlEncode,
|
2021-01-18 09:12:21 +00:00
|
|
|
listeners: {
|
|
|
|
change: function(field, newValue, oldValue) {
|
|
|
|
let vm = this.up('window').getViewModel();
|
|
|
|
vm.set('userid', newValue);
|
|
|
|
},
|
|
|
|
},
|
2020-11-02 13:36:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
xtype: 'textfield',
|
|
|
|
fieldLabel: gettext('Description'),
|
|
|
|
allowBlank: false,
|
|
|
|
name: 'description',
|
|
|
|
maxLength: 256,
|
2021-01-18 09:12:21 +00:00
|
|
|
emptyText: gettext('For example: TFA device ID, required to identify multiple factors.'),
|
2020-11-02 13:36:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
xtype: 'textfield',
|
2021-01-21 14:09:22 +00:00
|
|
|
name: 'password',
|
|
|
|
reference: 'password',
|
2021-01-18 09:12:21 +00:00
|
|
|
fieldLabel: gettext('Verify Password'),
|
2021-01-21 14:09:22 +00:00
|
|
|
inputType: 'password',
|
2020-11-02 13:36:10 +00:00
|
|
|
minLength: 5,
|
|
|
|
allowBlank: false,
|
|
|
|
validateBlank: true,
|
2021-01-21 14:06:15 +00:00
|
|
|
cbind: {
|
|
|
|
hidden: () => Proxmox.UserName === 'root@pam',
|
|
|
|
disabled: () => Proxmox.UserName === 'root@pam',
|
2021-02-03 09:21:56 +00:00
|
|
|
emptyText: () =>
|
|
|
|
Ext.String.format(gettext("Confirm your ({0}) password"), Proxmox.UserName),
|
2021-01-18 09:12:21 +00:00
|
|
|
},
|
2020-11-02 13:36:10 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
|
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
xtype: 'proxmoxHelpButton',
|
|
|
|
},
|
|
|
|
'->',
|
|
|
|
{
|
|
|
|
xtype: 'button',
|
|
|
|
text: gettext('Register Webauthn Device'),
|
|
|
|
handler: 'registerWebauthn',
|
|
|
|
bind: {
|
|
|
|
disabled: '{!valid}',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|