Skip to content

Commit 9c77f8f

Browse files
committed
Expose enrollmentCompletionDeadline field in TotpSecret. (#6870)
Also changed the demo app to display this field as a countdown.
1 parent 7cbc518 commit 9c77f8f

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

common/api-review/auth.api.md

+1
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ export interface TotpMultiFactorInfo extends MultiFactorInfo {
767767
export class TotpSecret {
768768
readonly codeIntervalSeconds: number;
769769
readonly codeLength: number;
770+
readonly enrollmentCompletionDeadline: string;
770771
// Warning: (ae-forgotten-export) The symbol "StartTotpMfaEnrollmentResponse" needs to be exported by the entry point index.d.ts
771772
//
772773
// @internal (undocumented)

packages/auth/demo/public/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@
534534
</button>
535535
<br>
536536
<p hidden class="totp-text" id="totp-text"> Please scan the QR code below in a TOTP app </p>
537-
<br>
537+
<p hidden class="totp-deadline" id="totp-deadline"></p>
538538
<img hidden class="totp-qr-image" id="totp-qr-image"/>
539539
<br>
540540
<input type="text" id="enroll-mfa-totp-verification-code"

packages/auth/demo/public/style.css

+6
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ input + .form,
180180
.totp-text {
181181
font-family: 'Courier New', Courier;
182182
}
183+
184+
.totp-deadline {
185+
font-family: 'Courier New', Courier;
186+
color: #d9534f;
187+
}
188+
183189
.totp-qr-image {
184190
height: 120px;
185191
margin-right: 10px;

packages/auth/demo/src/index.js

+21
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,27 @@ async function onStartEnrollWithTotpMultiFactor() {
710710
);
711711
const url = totpSecret.generateQrCodeUrl('test', 'testissuer');
712712
console.log('TOTP URL is ' + url);
713+
console.log(
714+
'Finalize sign in by ' + totpSecret.enrollmentCompletionDeadline
715+
);
716+
// display the numbr of seconds left to enroll.
717+
$('p.totp-deadline').show();
718+
var id = setInterval(function () {
719+
var deadline = new Date(totpSecret.enrollmentCompletionDeadline);
720+
var t = deadline - new Date().getTime();
721+
if (t < 0) {
722+
clearInterval(id);
723+
document.getElementById('totp-deadline').innerText =
724+
'TOTP enrollment expired!';
725+
} else {
726+
var minutes = Math.floor(t / (1000 * 60));
727+
var seconds = Math.floor((t % (60 * 1000)) / 1000);
728+
// accessing the field using $ does not work here.
729+
document.getElementById(
730+
'totp-deadline'
731+
).innerText = `Time left - ${minutes} minutes, ${seconds} seconds.`;
732+
}
733+
}, 1000);
713734
// Use the QRServer API documented at https://goqr.me/api/doc/
714735
const qrCodeUrl = `https://api.qrserver.com/v1/create-qr-code/?data=${url}&amp;size=30x30`;
715736
$('img.totp-qr-image').attr('src', qrCodeUrl).show();

packages/auth/src/mfa/assertions/totp.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -199,25 +199,27 @@ export class TotpSecret {
199199
* The interval (in seconds) when the OTP codes should change.
200200
*/
201201
readonly codeIntervalSeconds: number;
202-
// TODO(prameshj) - make this public after API review.
202+
/**
203+
* The timestamp(UTC string) by which TOTP enrollment should be completed.
204+
*/
203205
// This can be used by callers to show a countdown of when to enter OTP code by.
204-
private readonly finalizeEnrollmentBy: string;
206+
readonly enrollmentCompletionDeadline: string;
205207

206208
// The public members are declared outside the constructor so the docs can be generated.
207209
private constructor(
208210
secretKey: string,
209211
hashingAlgorithm: string,
210212
codeLength: number,
211213
codeIntervalSeconds: number,
212-
finalizeEnrollmentBy: string,
214+
enrollmentCompletionDeadline: string,
213215
private readonly sessionInfo: string,
214216
private readonly auth: AuthInternal
215217
) {
216218
this.secretKey = secretKey;
217219
this.hashingAlgorithm = hashingAlgorithm;
218220
this.codeLength = codeLength;
219221
this.codeIntervalSeconds = codeIntervalSeconds;
220-
this.finalizeEnrollmentBy = finalizeEnrollmentBy;
222+
this.enrollmentCompletionDeadline = enrollmentCompletionDeadline;
221223
}
222224

223225
/** @internal */

0 commit comments

Comments
 (0)