2020-11-02 13:36:10 +00:00
|
|
|
Ext.define('PBS.window.AddTfaRecovery', {
|
2021-01-13 11:06:51 +00:00
|
|
|
extend: 'Proxmox.window.Edit',
|
2020-11-02 13:36:10 +00:00
|
|
|
alias: 'widget.pbsAddTfaRecovery',
|
|
|
|
mixins: ['Proxmox.Mixin.CBind'],
|
|
|
|
|
|
|
|
onlineHelp: 'user_mgmt',
|
2021-01-13 11:06:51 +00:00
|
|
|
isCreate: true,
|
|
|
|
isAdd: true,
|
|
|
|
subject: gettext('TFA recovery keys'),
|
2020-11-02 13:36:10 +00:00
|
|
|
width: 512,
|
2021-01-13 11:06:51 +00:00
|
|
|
method: 'POST',
|
2020-11-02 13:36:10 +00:00
|
|
|
|
|
|
|
fixedUser: false,
|
|
|
|
|
2021-01-13 11:06:51 +00:00
|
|
|
url: '/api2/extjs/access/tfa',
|
|
|
|
submitUrl: function(url, values) {
|
|
|
|
let userid = values.userid;
|
|
|
|
delete values.userid;
|
|
|
|
return `${url}/${userid}`;
|
|
|
|
},
|
2020-11-02 13:36:10 +00:00
|
|
|
|
2021-01-13 11:06:51 +00:00
|
|
|
apiCallDone: function(success, response) {
|
|
|
|
if (!success) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:46:47 +00:00
|
|
|
let values = response
|
|
|
|
.result
|
|
|
|
.data
|
|
|
|
.recovery
|
|
|
|
.map((v, i) => `${i}: ${v}`)
|
|
|
|
.join("\n");
|
2021-01-13 11:06:51 +00:00
|
|
|
Ext.create('PBS.window.TfaRecoveryShow', {
|
|
|
|
autoShow: true,
|
2021-01-18 09:45:47 +00:00
|
|
|
userid: this.getViewModel().get('userid'),
|
2021-01-13 11:06:51 +00:00
|
|
|
values,
|
|
|
|
});
|
2020-11-02 13:36:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
viewModel: {
|
|
|
|
data: {
|
|
|
|
has_entry: 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',
|
|
|
|
hasEntry: async function(userid) {
|
|
|
|
let me = this;
|
|
|
|
let view = me.getView();
|
|
|
|
|
|
|
|
try {
|
|
|
|
await PBS.Async.api2({
|
2021-01-13 11:06:51 +00:00
|
|
|
url: `${view.url}/${userid}/recovery`,
|
2020-11-02 13:36:10 +00:00
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
} catch (_ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-01-13 11:06:51 +00:00
|
|
|
init: function(view) {
|
2020-11-02 13:36:10 +00:00
|
|
|
this.onUseridChange(null, Proxmox.UserName);
|
|
|
|
},
|
|
|
|
|
2021-01-13 11:06:51 +00:00
|
|
|
onUseridChange: async function(field, userid) {
|
2020-11-02 13:36:10 +00:00
|
|
|
let me = this;
|
2021-01-18 09:12:21 +00:00
|
|
|
let vm = me.getViewModel();
|
2020-11-02 13:36:10 +00:00
|
|
|
|
|
|
|
me.userid = userid;
|
2021-01-18 09:12:21 +00:00
|
|
|
vm.set('userid', userid);
|
2020-11-02 13:36:10 +00:00
|
|
|
|
|
|
|
let has_entry = await me.hasEntry(userid);
|
2021-01-18 09:12:21 +00:00
|
|
|
vm.set('has_entry', has_entry);
|
2020-11-02 13:36:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
xtype: 'pmxDisplayEditField',
|
|
|
|
name: 'userid',
|
|
|
|
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,
|
2021-01-13 11:06:51 +00:00
|
|
|
validator: function(_value) {
|
|
|
|
return !this.up('window').getViewModel().get('has_entry');
|
|
|
|
},
|
2020-11-02 13:36:10 +00:00
|
|
|
},
|
|
|
|
renderer: Ext.String.htmlEncode,
|
|
|
|
listeners: {
|
|
|
|
change: 'onUseridChange',
|
|
|
|
},
|
|
|
|
},
|
2021-01-13 11:06:51 +00:00
|
|
|
{
|
|
|
|
xtype: 'hiddenfield',
|
|
|
|
name: 'type',
|
|
|
|
value: 'recovery',
|
|
|
|
},
|
2020-11-02 13:36:10 +00:00
|
|
|
{
|
|
|
|
xtype: 'displayfield',
|
|
|
|
bind: {
|
|
|
|
hidden: '{!has_entry}',
|
|
|
|
},
|
2021-01-13 11:06:51 +00:00
|
|
|
hidden: true,
|
|
|
|
userCls: 'pmx-hint',
|
2020-11-02 13:36:10 +00:00
|
|
|
value: gettext('User already has recovery keys.'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xtype: 'textfield',
|
2021-01-21 14:09:22 +00:00
|
|
|
name: 'password',
|
|
|
|
reference: 'password',
|
|
|
|
fieldLabel: gettext('Verify Password'),
|
2020-11-02 13:36:10 +00:00
|
|
|
inputType: 'password',
|
|
|
|
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
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
Ext.define('PBS.window.TfaRecoveryShow', {
|
|
|
|
extend: 'Ext.window.Window',
|
|
|
|
alias: ['widget.pbsTfaRecoveryShow'],
|
|
|
|
mixins: ['Proxmox.Mixin.CBind'],
|
|
|
|
|
|
|
|
width: 600,
|
|
|
|
modal: true,
|
|
|
|
resizable: false,
|
|
|
|
title: gettext('Recovery Keys'),
|
2021-01-18 09:44:37 +00:00
|
|
|
onEsc: Ext.emptyFn,
|
2020-11-02 13:36:10 +00:00
|
|
|
|
|
|
|
items: [
|
|
|
|
{
|
2021-01-13 11:06:52 +00:00
|
|
|
xtype: 'form',
|
|
|
|
layout: 'anchor',
|
2020-11-02 13:36:10 +00:00
|
|
|
bodyPadding: 10,
|
|
|
|
border: false,
|
|
|
|
fieldDefaults: {
|
|
|
|
anchor: '100%',
|
|
|
|
},
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
xtype: 'textarea',
|
|
|
|
editable: false,
|
|
|
|
inputId: 'token-secret-value',
|
|
|
|
cbind: {
|
|
|
|
value: '{values}',
|
|
|
|
},
|
|
|
|
fieldStyle: {
|
|
|
|
'fontFamily': 'monospace',
|
|
|
|
},
|
|
|
|
height: '160px',
|
|
|
|
},
|
2021-01-13 11:06:52 +00:00
|
|
|
{
|
|
|
|
xtype: 'displayfield',
|
|
|
|
border: false,
|
|
|
|
padding: '5 0 0 0',
|
|
|
|
userCls: 'pmx-hint',
|
|
|
|
value: gettext('Please record recovery keys - they will only be displayed now'),
|
|
|
|
},
|
2020-11-02 13:36:10 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
handler: function(b) {
|
|
|
|
document.getElementById('token-secret-value').select();
|
|
|
|
document.execCommand("copy");
|
|
|
|
},
|
2021-01-18 09:45:28 +00:00
|
|
|
iconCls: 'fa fa-clipboard',
|
|
|
|
text: gettext('Copy Recovery Keys'),
|
2020-11-02 13:36:10 +00:00
|
|
|
},
|
2021-01-18 09:45:47 +00:00
|
|
|
{
|
|
|
|
handler: function(b) {
|
|
|
|
let win = this.up('window');
|
|
|
|
win.paperkeys(win.values, win.userid);
|
|
|
|
},
|
|
|
|
iconCls: 'fa fa-print',
|
|
|
|
text: gettext('Print Recovery Keys'),
|
|
|
|
},
|
2020-11-02 13:36:10 +00:00
|
|
|
],
|
2021-01-18 09:45:47 +00:00
|
|
|
paperkeys: function(keyString, userid) {
|
|
|
|
let me = this;
|
|
|
|
|
|
|
|
let printFrame = document.createElement("iframe");
|
|
|
|
Object.assign(printFrame.style, {
|
|
|
|
position: "fixed",
|
|
|
|
right: "0",
|
|
|
|
bottom: "0",
|
|
|
|
width: "0",
|
|
|
|
height: "0",
|
|
|
|
border: "0",
|
|
|
|
});
|
|
|
|
const host = document.location.host;
|
|
|
|
const title = document.title;
|
|
|
|
const html = `<html><head><script>
|
|
|
|
window.addEventListener('DOMContentLoaded', (ev) => window.print());
|
|
|
|
</script><style>@media print and (max-height: 150mm) {
|
|
|
|
h4, p { margin: 0; font-size: 1em; }
|
|
|
|
}</style></head><body style="padding: 5px;">
|
|
|
|
<h4>Recovery Keys for '${userid}' - ${title} (${host})</h4>
|
|
|
|
<p style="font-size:1.5em;line-height:1.5em;font-family:monospace;
|
|
|
|
white-space:pre-wrap;overflow-wrap:break-word;">
|
|
|
|
${keyString}
|
|
|
|
</p>
|
|
|
|
</body></html>`;
|
|
|
|
|
|
|
|
printFrame.src = "data:text/html;base64," + btoa(html);
|
|
|
|
document.body.appendChild(printFrame);
|
|
|
|
},
|
2020-11-02 13:36:10 +00:00
|
|
|
});
|