initDelegations method

Future<DecryptedAccessLogDto> initDelegations(
  1. UserDto user,
  2. CryptoConfig<DecryptedAccessLogDto, AccessLogDto> config, {
  3. Set<String>? delegates = null,
})

Implementation

Future<DecryptedAccessLogDto> initDelegations(UserDto user, CryptoConfig<DecryptedAccessLogDto, AccessLogDto> config, { Set<String>? delegates = null }) async {
  final Uuid uuid = Uuid();

  Set<String> delegationKeys = Set.from(delegates ?? <String>{})
    ..addAll(user.autoDelegations["all"] ?? <String>{})
    ..addAll(user.autoDelegations["medicalInformation"] ?? <String>{});
  final ek = uuid.v4(options: {'rng': UuidUtil.cryptoRNG});
  final sfk = uuid.v4(options: {'rng': UuidUtil.cryptoRNG});

  responsible = user.dataOwnerId()!;
  author = user.id;
  delegations = await (delegationKeys..add(user.dataOwnerId()!)).fold(Future.value({...delegations}), (m, d) async {
    final acc = await m;
    final keyAndOwner = await config.crypto.encryptAESKeyForHcp(user.dataOwnerId()!, d, id, sfk);
    return acc..addEntries([
        MapEntry(d, {
          DelegationDto(owner: user.dataOwnerId(), delegatedTo: d, key: keyAndOwner.item1)
        })
      ]);
  });

  encryptionKeys = await (delegationKeys..add(user.dataOwnerId()!)).fold(Future.value({...encryptionKeys}), (m, d) async {
    final acc = await m;
    final keyAndOwner = await config.crypto.encryptAESKeyForHcp(user.dataOwnerId()!, d, id, ek);
    return acc..addEntries([
        MapEntry(d, {
          DelegationDto(owner: user.dataOwnerId(), delegatedTo: d, key: keyAndOwner.item1)
        })
      ]);
  });
  return this;
}