registerWithEmail method

Future<Response?> registerWithEmail(
  1. Request request
)

Implements the registration flow.

Consists of the following three steps:

  1. Ask the relying party server for a challenge. At this point the relying party can decide to block the registration (e.g. because such a user already exists). This will result in _backend throwing an Exception.
  2. Ask the _authenticator to create a new passkey and solve the challenge.
  3. Send the solution of the challenge back to the relying party server. For a successful solution the relying party server will respond with Response.

The contents of Response depend on the relying party server (the _backend), but usually it contains some user data (name, email) and some kind of token (e.g. a JWT token)

returns null if the user did cancel the registration

Implementation

Future<Response?> registerWithEmail(Request request) async {
  final initResponse = await _backend.initRegister(request);

  final challenge = initResponse.challenge;
  final user = UserType(
    displayName: initResponse.user.displayName,
    name: initResponse.user.name,
    id: initResponse.user.id,
  );
  final rp = RelyingPartyType(
    name: initResponse.rp.name,
    id: initResponse.rp.id,
  );

  final authSelectionType = AuthenticatorSelectionType(
    authenticatorAttachment:
        initResponse.authenticatorSelection.authenticatorAttachment,
    requireResidentKey:
        initResponse.authenticatorSelection.requireResidentKey,
    residentKey: initResponse.authenticatorSelection.residentKey,
    userVerification: initResponse.authenticatorSelection.userVerification,
  );

  try {
    final authenticatorResponse = await _authenticator.register(
      challenge,
      rp,
      user,
      authSelectionType,
      initResponse.pubKeyCredParams
          ?.map(
            (e) => PubKeyCredParamType(
              alg: e.alg,
              type: e.type,
            ),
          )
          .cast<PubKeyCredParamType>()
          .toList(),
      initResponse.timeout,
      initResponse.attestation,
    );
    final completeRequest = RegistrationCompleteRequest(
      id: authenticatorResponse.id,
      rawId: authenticatorResponse.rawId,
      clientDataJSON: authenticatorResponse.clientDataJSON,
      attestationObject: authenticatorResponse.attestationObject,
    );
    final completeResponse = await _backend.completeRegister(completeRequest);

    return completeResponse;
  } on PlatformException catch (e) {
    switch (e.code) {
      case 'cancelled':
        return null;
      case 'domainNotAssociated':
        throw DomainNotAssociatedException();
      default:
        rethrow;
    }
  }
}