get method

  1. @override
Future<Map<String, Object?>?> get(
  1. Map<String, Object?> request
)
override

Runs a WebAuthn assertion. request carries challenge, rpId, allowCredentials and userVerification.

Implementation

@override
Future<Map<String, Object?>?> get(Map<String, Object?> request) async {
  final rpId = request['rpId'] as String;
  final allowed = jsonList(
    request['allowCredentials'],
  ).map((c) => jsonMap(c)['id']).whereType<String>().toSet();
  final candidates = _credentials.where(
    (c) =>
        c.rpId == rpId && (allowed.isEmpty || allowed.contains(c.idB64url)),
  );
  if (candidates.isEmpty) {
    throw const MoonXException('passkey_no_credentials_available');
  }
  // Discoverable requests pick the newest credential, as a single-account
  // device would.
  final credential = candidates.last;
  credential.signCount++;

  final clientData = _clientData(
    'webauthn.get',
    request['challenge'] as String,
  );
  final authData =
      (BytesBuilder()
            ..add(_sha256(utf8.encode(rpId)))
            ..addByte(0x01 | 0x04 | 0x08 | 0x10)
            ..add(_uint32(credential.signCount)))
          .toBytes();
  final signature = _sign(
    credential.privateKey,
    Uint8List.fromList([...authData, ..._sha256(clientData)]),
  );

  return {
    'id': credential.idB64url,
    'rawId': credential.idB64url,
    'type': 'public-key',
    'response': {
      'clientDataJSON': toB64url(clientData),
      'authenticatorData': toB64url(authData),
      'signature': toB64url(signature),
      'userHandle': credential.userHandleB64url,
    },
    'clientExtensionResults': <String, Object?>{},
    'authenticatorAttachment': 'platform',
  };
}