resolveAuthenticatingEnrollment method

String? resolveAuthenticatingEnrollment()

The one enrollment this keyfile authenticates as, for a caller that has no id of its own to supply — a cold start holding nothing but the file.

Null when the file holds no typed authentication material at all: a legacy keyfile, whose APKAM keypair lives in the flat fields, and whose enrollment is the flat enrollmentId.

Throws when several qualify, naming them. It is deliberately not a first-wins default: a keyfile holding two live enrollments is a state the writer refuses to create, so meeting one means something else is wrong, and picking one silently is how a client ends up authenticating as the wrong principal with nothing to point at. The selection stays the caller's — this offers the candidates, not a verdict.

Implementation

String? resolveAuthenticatingEnrollment() {
  final candidates = authenticatableEnrollmentIds.toList();
  if (candidates.isEmpty) return null;
  if (candidates.length > 1) {
    throw AtKeysEnrollmentException(
        'This keyfile holds active authentication material for '
        '${candidates.length} enrollments (${candidates.join(', ')}), so '
        'which one it authenticates as is not derivable. The caller must '
        'name the enrollment it means.');
  }
  return candidates.single;
}