vault/ui/app/components/generate-credentials-totp.js
lane-wetmore 55674ddd1f
UI: Add TOTP secrets engine (#29751)
* TOTP secrets in the web UI
---------

Co-authored-by: Moritz Pflanzer <moritz@pflanzer.eu>
Co-authored-by: claire bontempo <68122737+hellobontempo@users.noreply.github.com>
Co-authored-by: Shannon Roberts (Beagin) <beagins@users.noreply.github.com>
2025-04-17 12:59:45 -05:00

75 lines
1.9 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { task, timeout } from 'ember-concurrency';
import { service } from '@ember/service';
import { action } from '@ember/object';
const SHOW_ROUTE = 'vault.cluster.secrets.backend.show';
export default class GenerateCredentialsTotp extends Component {
@tracked elapsedTime = 0;
@tracked totpCode = null;
@service store;
@service router;
title = 'Generate TOTP code';
constructor() {
super(...arguments);
this.startTimer.perform();
}
get remainingTime() {
const { totpCodePeriod } = this.args;
if (!totpCodePeriod) {
return 0; // TODO improve this
}
return totpCodePeriod - this.elapsedTime;
}
@task({ restartable: true })
*startTimer() {
const { backendPath, keyName, totpCodePeriod } = this.args;
if (totpCodePeriod) {
this.generateTotpCode(backendPath, keyName);
while (this.elapsedTime <= totpCodePeriod) {
yield timeout(1000);
this.elapsedTime += 1;
}
if (this.elapsedTime > totpCodePeriod) {
this.elapsedTime = 0;
this.generateTotpCode(backendPath, keyName);
this.startTimer.perform();
}
}
}
async generateTotpCode(backend, keyName) {
// TODO improvement: refreshing does not currently result in a new code
try {
const totpCode = await this.store.adapterFor('totp-key').generateCode(backend, keyName);
this.totpCode = totpCode.code;
} catch (e) {
// swallow error, non-essential data
return;
}
}
@action redirectPreviousPage() {
const { backRoute, keyName } = this.args;
if (backRoute === SHOW_ROUTE) {
this.router.transitionTo(this.args.backRoute, keyName);
} else {
this.router.transitionTo(this.args.backRoute);
}
}
}