beginTotp method

Future<DVTotpEnrollment> beginTotp(
  1. String userId, {
  2. required String account,
})

Starts TOTP enrollment for userId. Nothing is required of later sign-ins until confirmTotp proves the app holds the secret.

Refused while a confirmed authenticator exists: swapping it for a new one is removing a second factor, which is a decision to make after one is presented, not something a session can do on its own.

Implementation

Future<DVTotpEnrollment> beginTotp(String userId,
    {required String account}) async {
  final DVTotpRecord? existing = await store.totp(userId);
  if (existing != null && existing.confirmed) {
    throw StateError(
        'This account already has an authenticator. Remove it first.');
  }
  final String secret = DVTotp.base32Encode(
      List<int>.generate(20, (_) => _random.nextInt(256)));
  await store.putPendingTotp(DVTotpRecord(
    userId: userId,
    sealedSecret:
        cipher.encrypt(model: _model, field: _field, plaintext: secret),
    confirmed: false,
  ));
  return DVTotpEnrollment(
    secret: secret,
    uri: totp.provisioningUri(
        secret: secret, issuer: issuer, account: account),
  );
}