import java.io.InputStream;
import java.util.Set;
import java.util.UUID;
+import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class GroupV2Helper {
private final GroupsV2Api groupsV2Api;
- private final GroupAuthorizationProvider groupAuthorizationProvider;
-
private final SignalServiceAddressResolver addressResolver;
public GroupV2Helper(
final SelfRecipientIdProvider selfRecipientIdProvider,
final GroupsV2Operations groupsV2Operations,
final GroupsV2Api groupsV2Api,
- final GroupAuthorizationProvider groupAuthorizationProvider,
final SignalServiceAddressResolver addressResolver
) {
this.profileKeyCredentialProvider = profileKeyCredentialProvider;
this.selfRecipientIdProvider = selfRecipientIdProvider;
this.groupsV2Operations = groupsV2Operations;
this.groupsV2Api = groupsV2Api;
- this.groupAuthorizationProvider = groupAuthorizationProvider;
this.addressResolver = addressResolver;
}
public DecryptedGroup getDecryptedGroup(final GroupSecretParams groupSecretParams) {
try {
- final var groupsV2AuthorizationString = groupAuthorizationProvider.getAuthorizationForToday(
- groupSecretParams);
+ final var groupsV2AuthorizationString = getGroupAuthForToday(groupSecretParams);
return groupsV2Api.getGroup(groupSecretParams, groupsV2AuthorizationString);
} catch (IOException | VerificationFailedException | InvalidGroupStateException e) {
logger.warn("Failed to retrieve Group V2 info, ignoring: {}", e.getMessage());
return groupsV2Api.getGroupJoinInfo(groupSecretParams,
Optional.fromNullable(password).transform(GroupLinkPassword::serialize),
- groupAuthorizationProvider.getAuthorizationForToday(groupSecretParams));
+ getGroupAuthForToday(groupSecretParams));
}
public Pair<GroupInfoV2, DecryptedGroup> createGroup(
final GroupsV2AuthorizationString groupAuthForToday;
final DecryptedGroup decryptedGroup;
try {
- groupAuthForToday = groupAuthorizationProvider.getAuthorizationForToday(groupSecretParams);
+ groupAuthForToday = getGroupAuthForToday(groupSecretParams);
groupsV2Api.putNewGroup(newGroup, groupAuthForToday);
decryptedGroup = groupsV2Api.getGroup(groupSecretParams, groupAuthForToday);
} catch (IOException | VerificationFailedException | InvalidGroupStateException e) {
final var avatarBytes = readAvatarBytes(avatarFile);
var avatarCdnKey = groupsV2Api.uploadAvatar(avatarBytes,
groupSecretParams,
- groupAuthorizationProvider.getAuthorizationForToday(groupSecretParams));
+ getGroupAuthForToday(groupSecretParams));
change.setModifyAvatar(GroupChange.Actions.ModifyAvatarAction.newBuilder().setAvatar(avatarCdnKey));
}
}
var signedGroupChange = groupsV2Api.patchGroup(changeActions,
- groupAuthorizationProvider.getAuthorizationForToday(groupSecretParams),
+ getGroupAuthForToday(groupSecretParams),
Optional.absent());
return new Pair<>(decryptedGroupState, signedGroupChange);
final var changeActions = change.setRevision(nextRevision).build();
return groupsV2Api.patchGroup(changeActions,
- groupAuthorizationProvider.getAuthorizationForToday(groupSecretParams),
+ getGroupAuthForToday(groupSecretParams),
Optional.fromNullable(password).transform(GroupLinkPassword::serialize));
}
return null;
}
+
+ private static int currentTimeDays() {
+ return (int) TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis());
+ }
+
+ private GroupsV2AuthorizationString getGroupAuthForToday(
+ final GroupSecretParams groupSecretParams
+ ) throws IOException {
+ final var today = currentTimeDays();
+ // Returns credentials for the next 7 days
+ final var credentials = groupsV2Api.getCredentials(today);
+ // TODO cache credentials until they expire
+ var authCredentialResponse = credentials.get(today);
+ final var uuid = addressResolver.resolveSignalServiceAddress(this.selfRecipientIdProvider.getSelfRecipientId())
+ .getUuid()
+ .get();
+ try {
+ return groupsV2Api.getGroupsV2AuthorizationString(uuid, today, groupSecretParams, authCredentialResponse);
+ } catch (VerificationFailedException e) {
+ throw new IOException(e);
+ }
+ }
}