connect static method

Future<TomSecretsAdminClient> connect(
  1. TomSecretsRuntime runtime,
  2. List<int> adminKey, {
  3. Map<String, Object?> client = const {'name' : 'tsm'},
})

Connects to the admin endpoint of runtime and proves both sides hold adminKey; throws service_impostor when the service's mac2 is wrong, denied when the service refuses the key.

Implementation

static Future<TomSecretsAdminClient> connect(
  TomSecretsRuntime runtime,
  List<int> adminKey, {
  Map<String, Object?> client = const {'name': 'tsm'},
}) async {
  final admin = await _open(runtime);
  final challenge = await admin._next();
  final serviceNonce = base64.decode(challenge['serviceNonce']! as String);
  final start = challenge['serviceStart']! as int;
  final adminNonce = tomSecretsRandomBytes();
  admin._send({
    'op': 'auth',
    'adminNonce': base64.encode(adminNonce),
    'mac': base64.encode(
      tomSecretsChallengeMac(
        adminKey,
        tomSecretsAdminLabel,
        serviceNonce,
        adminNonce,
        start,
      ),
    ),
    'client': client,
  });
  final answer = await admin._next();
  admin._throwIfError(answer);
  final expected = tomSecretsChallengeMac(
    adminKey,
    tomSecretsServiceLabel,
    serviceNonce,
    adminNonce,
    start,
  );
  final mac2 = base64.decode(answer['mac2'] as String? ?? '');
  if (answer['op'] != 'authed' ||
      !tomSecretsConstantTimeEquals(mac2, expected)) {
    await admin.close();
    throw TomSecretsAdminException(
      'service_impostor',
      'the service could not prove it holds the admin key',
    );
  }
  admin
    .._serviceStart = start
    .._bundleKey = tomSecretsBundleKey(adminKey, serviceNonce, adminNonce)
    .._listen();
  return admin;
}