Skip to content

Commit

Permalink
Make TOTP period optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mpflanzer committed Jul 5, 2024
1 parent 8663321 commit 807679c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
28 changes: 18 additions & 10 deletions ui/app/components/generate-credentials-totp.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ import timestamp from 'core/utils/timestamp';
import { getUnixTime } from 'date-fns';

export default class GenerateCredentialsTotp extends Component {
title = "Generate TOTP code";
title = 'Generate TOTP code';

@tracked elapsedTime = 0;
nextTick = null;

get remainingTime() {
const { model } = this.args;

if (!model.period) {
return;
}

return model.period - this.elapsedTime;
}

Expand All @@ -28,14 +33,17 @@ export default class GenerateCredentialsTotp extends Component {

@action
startTimer() {
this.nextTick = later(
this,
function () {
const { model } = this.args;
this.elapsedTime = getUnixTime(timestamp.now()) % model.period;
this.startTimer();
},
1000
);
const { model } = this.args;

if (model.period) {
this.nextTick = later(
this,
function () {
this.elapsedTime = getUnixTime(timestamp.now()) % model.period;
this.startTimer();
},
1000
);
}
}
}
15 changes: 12 additions & 3 deletions ui/app/routes/vault/cluster/secrets/backend/credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ export default Route.extend({
return this.store.adapterFor('totp').generateCode(backend, keyName);
},

async getTOTPKey(backend, keyName) {
try {
const key = await this.store.queryRecord('totp', { id: keyName, backend });
return key;
} catch (e) {
// swallow error, non-essential data
return;
}
},

async model(params) {
const role = params.secret;
const { id: backendPath, type: backendType } = this.modelFor('vault.cluster.secrets.backend');
Expand All @@ -82,8 +92,7 @@ export default Route.extend({
awsRole = await this.getAwsRole(backendPath, role);
} else if (backendType === 'totp') {
totpCode = await this.getTOTPCode(backendPath, role);
const totpKey = await this.store.queryRecord('totp', { id: role, backend: backendPath });
totpCode.period = totpKey.period;
totpCode.period = (await this.getTOTPKey(backendPath, role))?.period;
}

return resolve({
Expand All @@ -98,7 +107,7 @@ export default Route.extend({
},

async afterModel(model, transition) {
if (model.backendType === 'totp') {
if (model.backendType === 'totp' && model.totpCode.period) {
later(
() => {
this.refresh();
Expand Down
12 changes: 7 additions & 5 deletions ui/app/templates/components/generate-credentials-totp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
<MessageError @model={{this.model}} />
<InfoTableRow @label="Code">
<MaskedInput @name="code" @value={{@model.code}} @displayOnly={{true}} @allowCopy={{true}} />
<label class="code-validity">
<div>Valid for {{this.remainingTime}} seconds</div>
<progress id="code-validity" value="{{this.elapsedTime}}" max="{{@model.period}}"></progress>
</label>
{{#if @model.period}}
<label class="code-validity">
<div>Valid for {{this.remainingTime}} seconds</div>
<progress id="code-validity" value="{{this.elapsedTime}}" max="{{@model.period}}"></progress>
</label>
{{/if}}
</InfoTableRow>
</div>
<div class="field is-grouped box is-fullwidth is-bottomless">
Expand All @@ -48,4 +50,4 @@
data-test-secret-generate-back
/>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion ui/app/templates/components/totp-edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,4 @@
/>
{{/each}}
</div>
{{/if}}
{{/if}}

0 comments on commit 807679c

Please sign in to comment.