Can generate reset access token for an account

This commit is contained in:
Pierre HUBERT 2021-05-14 15:33:04 +02:00
parent 585a66ef0a
commit 9b0f3c1fec
3 changed files with 66 additions and 1 deletions

View File

@ -39,6 +39,11 @@ export interface AdminAccountKey {
time_add: number; time_add: number;
} }
export interface AdminResetToken {
token: string;
expire: number;
}
const SESSION_STORAGE_TOKEN = "auth_token"; const SESSION_STORAGE_TOKEN = "auth_token";
let currentAccount: AdminAccount | null = null; let currentAccount: AdminAccount | null = null;
@ -150,6 +155,17 @@ export class AccountHelper {
}); });
} }
/**
* Generate a access reset token for an admin
*
* @param adminID The id of the target administrator
*/
static async GenerateResetToken(adminID: number): Promise<AdminResetToken> {
return await serverRequest("accounts/generate_reset_token", {
id: adminID,
});
}
/** /**
* First step of access key enrollment * First step of access key enrollment
*/ */

View File

@ -26,6 +26,7 @@ import {
AdminAccount, AdminAccount,
AdminAccountKey, AdminAccountKey,
} from "../../helpers/AccountHelper"; } from "../../helpers/AccountHelper";
import { CopyToClipboard } from "../../utils/ClipboardUtils";
import { AsyncWidget } from "../widgets/AsyncWidget"; import { AsyncWidget } from "../widgets/AsyncWidget";
import { import {
input, input,
@ -192,6 +193,7 @@ export class KeySettingsSection extends React.Component<
this.load = this.load.bind(this); this.load = this.load.bind(this);
this.build = this.build.bind(this); this.build = this.build.bind(this);
this.generateResetToken = this.generateResetToken.bind(this);
this.registerNewKey = this.registerNewKey.bind(this); this.registerNewKey = this.registerNewKey.bind(this);
this.deleteKey = this.deleteKey.bind(this); this.deleteKey = this.deleteKey.bind(this);
} }
@ -202,6 +204,27 @@ export class KeySettingsSection extends React.Component<
this.setState({ keys: keys }); this.setState({ keys: keys });
} }
async generateResetToken() {
try {
if (
!(await matConfirm(
"Do you really want to generate a reset token for this account?"
))
)
return;
const token = await AccountHelper.GenerateResetToken(
this.props.admin.id
);
CopyToClipboard(token.token);
snackbar("Reset token was successfully copied to the clipboard!");
} catch (e) {
console.error(e);
matAlert("Failed to generate a token!");
}
}
async registerNewKey() { async registerNewKey() {
try { try {
const challenge = await AccountHelper.GetKeyRegistrationChallenge(); const challenge = await AccountHelper.GetKeyRegistrationChallenge();
@ -297,6 +320,9 @@ export class KeySettingsSection extends React.Component<
margin: "5px 10px", margin: "5px 10px",
}} }}
> >
<Button onClick={this.generateResetToken}>
New reset token
</Button>
<Button <Button
disabled={ disabled={
this.props.admin.id !== this.props.admin.id !==
@ -316,7 +342,7 @@ function SettingsSection(p: { title: string; children?: React.ReactNode }) {
return ( return (
<Grid item sm={6}> <Grid item sm={6}>
<Paper> <Paper>
<Typography variant="h5" style={{ padding: "10px 10px " }}> <Typography variant="h6" style={{ padding: "10px 15px " }}>
{p.title} {p.title}
</Typography> </Typography>
<Divider /> <Divider />

View File

@ -0,0 +1,23 @@
/**
* Clipboard utilities
*
* @author Pierre Hubert
*/
/**
* Copy some piece of text to the clipboard
*
* @param str The content to copy
*/
export function CopyToClipboard(str: string) {
const input = document.createElement("input");
input.value = str;
document.body.appendChild(input);
input.select();
input.setSelectionRange(0, str.length);
document.execCommand("copy");
input.remove();
}