1 package org
.asamk
.signal
.manager
.helper
;
3 import org
.asamk
.signal
.manager
.AvatarStore
;
4 import org
.asamk
.signal
.manager
.SignalDependencies
;
5 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
6 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
7 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
8 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
9 import org
.asamk
.signal
.manager
.util
.IOUtils
;
10 import org
.asamk
.signal
.manager
.util
.ProfileUtils
;
11 import org
.asamk
.signal
.manager
.util
.Utils
;
12 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
13 import org
.signal
.zkgroup
.profiles
.ProfileKeyCredential
;
14 import org
.slf4j
.Logger
;
15 import org
.slf4j
.LoggerFactory
;
16 import org
.whispersystems
.libsignal
.IdentityKey
;
17 import org
.whispersystems
.libsignal
.InvalidKeyException
;
18 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
19 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
20 import org
.whispersystems
.signalservice
.api
.profiles
.ProfileAndCredential
;
21 import org
.whispersystems
.signalservice
.api
.profiles
.SignalServiceProfile
;
22 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
23 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.NotFoundException
;
24 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.PushNetworkException
;
25 import org
.whispersystems
.signalservice
.api
.services
.ProfileService
;
28 import java
.io
.IOException
;
29 import java
.io
.OutputStream
;
30 import java
.nio
.file
.Files
;
31 import java
.util
.Base64
;
32 import java
.util
.Date
;
33 import java
.util
.List
;
34 import java
.util
.Objects
;
37 import io
.reactivex
.rxjava3
.core
.Maybe
;
38 import io
.reactivex
.rxjava3
.core
.Single
;
40 public final class ProfileHelper
{
42 private final static Logger logger
= LoggerFactory
.getLogger(ProfileHelper
.class);
44 private final SignalAccount account
;
45 private final SignalDependencies dependencies
;
46 private final AvatarStore avatarStore
;
47 private final UnidentifiedAccessProvider unidentifiedAccessProvider
;
48 private final SignalServiceAddressResolver addressResolver
;
51 final SignalAccount account
,
52 final SignalDependencies dependencies
,
53 final AvatarStore avatarStore
,
54 final UnidentifiedAccessProvider unidentifiedAccessProvider
,
55 final SignalServiceAddressResolver addressResolver
57 this.account
= account
;
58 this.dependencies
= dependencies
;
59 this.avatarStore
= avatarStore
;
60 this.unidentifiedAccessProvider
= unidentifiedAccessProvider
;
61 this.addressResolver
= addressResolver
;
64 public Profile
getRecipientProfile(RecipientId recipientId
) {
65 return getRecipientProfile(recipientId
, false);
68 public void refreshRecipientProfile(RecipientId recipientId
) {
69 getRecipientProfile(recipientId
, true);
72 public List
<ProfileKeyCredential
> getRecipientProfileKeyCredential(List
<RecipientId
> recipientIds
) {
73 final var profileFetches
= recipientIds
.stream().map(recipientId
-> {
74 var profileKeyCredential
= account
.getProfileStore().getProfileKeyCredential(recipientId
);
75 if (profileKeyCredential
!= null) {
79 return retrieveProfile(recipientId
,
80 SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
).onErrorComplete();
81 }).filter(Objects
::nonNull
).toList();
82 Maybe
.merge(profileFetches
).blockingSubscribe();
84 return recipientIds
.stream().map(r
-> account
.getProfileStore().getProfileKeyCredential(r
)).toList();
87 public ProfileKeyCredential
getRecipientProfileKeyCredential(RecipientId recipientId
) {
88 var profileKeyCredential
= account
.getProfileStore().getProfileKeyCredential(recipientId
);
89 if (profileKeyCredential
!= null) {
90 return profileKeyCredential
;
94 blockingGetProfile(retrieveProfile(recipientId
, SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
));
95 } catch (IOException e
) {
96 logger
.warn("Failed to retrieve profile key credential, ignoring: {}", e
.getMessage());
100 return account
.getProfileStore().getProfileKeyCredential(recipientId
);
104 * @param givenName if null, the previous givenName will be kept
105 * @param familyName if null, the previous familyName will be kept
106 * @param about if null, the previous about text will be kept
107 * @param aboutEmoji if null, the previous about emoji will be kept
108 * @param avatar if avatar is null the image from the local avatar store is used (if present),
110 public void setProfile(
111 String givenName
, final String familyName
, String about
, String aboutEmoji
, Optional
<File
> avatar
112 ) throws IOException
{
113 setProfile(true, givenName
, familyName
, about
, aboutEmoji
, avatar
);
116 public void setProfile(
117 boolean uploadProfile
,
119 final String familyName
,
122 Optional
<File
> avatar
123 ) throws IOException
{
124 var profile
= getRecipientProfile(account
.getSelfRecipientId());
125 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
126 if (givenName
!= null) {
127 builder
.withGivenName(givenName
);
129 if (familyName
!= null) {
130 builder
.withFamilyName(familyName
);
133 builder
.withAbout(about
);
135 if (aboutEmoji
!= null) {
136 builder
.withAboutEmoji(aboutEmoji
);
138 var newProfile
= builder
.build();
141 try (final var streamDetails
= avatar
== null
142 ? avatarStore
.retrieveProfileAvatar(account
.getSelfAddress())
143 : avatar
.isPresent() ? Utils
.createStreamDetailsFromFile(avatar
.get()) : null) {
144 final var avatarPath
= dependencies
.getAccountManager()
145 .setVersionedProfile(account
.getAci(),
146 account
.getProfileKey(),
147 newProfile
.getInternalServiceName(),
148 newProfile
.getAbout() == null ?
"" : newProfile
.getAbout(),
149 newProfile
.getAboutEmoji() == null ?
"" : newProfile
.getAboutEmoji(),
152 List
.of(/* TODO */));
153 builder
.withAvatarUrlPath(avatarPath
.orNull());
154 newProfile
= builder
.build();
158 if (avatar
!= null) {
159 if (avatar
.isPresent()) {
160 avatarStore
.storeProfileAvatar(account
.getSelfAddress(),
161 outputStream
-> IOUtils
.copyFileToStream(avatar
.get(), outputStream
));
163 avatarStore
.deleteProfileAvatar(account
.getSelfAddress());
166 account
.getProfileStore().storeProfile(account
.getSelfRecipientId(), newProfile
);
169 public List
<Profile
> getRecipientProfile(List
<RecipientId
> recipientIds
) {
170 final var profileFetches
= recipientIds
.stream().map(recipientId
-> {
171 var profile
= account
.getProfileStore().getProfile(recipientId
);
172 if (!isProfileRefreshRequired(profile
)) {
176 return retrieveProfile(recipientId
, SignalServiceProfile
.RequestType
.PROFILE
).onErrorComplete();
177 }).filter(Objects
::nonNull
).toList();
178 Maybe
.merge(profileFetches
).blockingSubscribe();
180 return recipientIds
.stream().map(r
-> account
.getProfileStore().getProfile(r
)).toList();
183 private Profile
getRecipientProfile(RecipientId recipientId
, boolean force
) {
184 var profile
= account
.getProfileStore().getProfile(recipientId
);
186 if (!force
&& !isProfileRefreshRequired(profile
)) {
191 blockingGetProfile(retrieveProfile(recipientId
, SignalServiceProfile
.RequestType
.PROFILE
));
192 } catch (IOException e
) {
193 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
196 return account
.getProfileStore().getProfile(recipientId
);
199 private boolean isProfileRefreshRequired(final Profile profile
) {
200 if (profile
== null) {
203 // Profiles are cached for 6h before retrieving them again, unless forced
204 final var now
= System
.currentTimeMillis();
205 return now
- profile
.getLastUpdateTimestamp() >= 6 * 60 * 60 * 1000;
208 private SignalServiceProfile
retrieveProfileSync(String username
) throws IOException
{
209 final var locale
= Utils
.getDefaultLocale();
210 return dependencies
.getMessageReceiver().retrieveProfileByUsername(username
, Optional
.absent(), locale
);
213 private Profile
decryptProfileAndDownloadAvatar(
214 final RecipientId recipientId
, final ProfileKey profileKey
, final SignalServiceProfile encryptedProfile
216 final var avatarPath
= encryptedProfile
.getAvatar();
217 downloadProfileAvatar(recipientId
, avatarPath
, profileKey
);
219 return ProfileUtils
.decryptProfile(profileKey
, encryptedProfile
);
222 public void downloadProfileAvatar(
223 final RecipientId recipientId
, final String avatarPath
, final ProfileKey profileKey
225 var profile
= account
.getProfileStore().getProfile(recipientId
);
226 if (profile
== null || !Objects
.equals(avatarPath
, profile
.getAvatarUrlPath())) {
227 downloadProfileAvatar(addressResolver
.resolveSignalServiceAddress(recipientId
), avatarPath
, profileKey
);
228 var builder
= profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
);
229 account
.getProfileStore().storeProfile(recipientId
, builder
.withAvatarUrlPath(avatarPath
).build());
233 private ProfileAndCredential
blockingGetProfile(Single
<ProfileAndCredential
> profile
) throws IOException
{
235 return profile
.blockingGet();
236 } catch (RuntimeException e
) {
237 if (e
.getCause() instanceof PushNetworkException
) {
238 throw (PushNetworkException
) e
.getCause();
239 } else if (e
.getCause() instanceof NotFoundException
) {
240 throw (NotFoundException
) e
.getCause();
242 throw new IOException(e
);
247 private Single
<ProfileAndCredential
> retrieveProfile(
248 RecipientId recipientId
, SignalServiceProfile
.RequestType requestType
250 var unidentifiedAccess
= getUnidentifiedAccess(recipientId
);
251 var profileKey
= Optional
.fromNullable(account
.getProfileStore().getProfileKey(recipientId
));
253 final var address
= addressResolver
.resolveSignalServiceAddress(recipientId
);
254 return retrieveProfile(address
, profileKey
, unidentifiedAccess
, requestType
).doOnSuccess(p
-> {
255 final var encryptedProfile
= p
.getProfile();
257 if (requestType
== SignalServiceProfile
.RequestType
.PROFILE_AND_CREDENTIAL
) {
258 final var profileKeyCredential
= p
.getProfileKeyCredential().orNull();
259 account
.getProfileStore().storeProfileKeyCredential(recipientId
, profileKeyCredential
);
262 final var profile
= account
.getProfileStore().getProfile(recipientId
);
264 Profile newProfile
= null;
265 if (profileKey
.isPresent()) {
266 newProfile
= decryptProfileAndDownloadAvatar(recipientId
, profileKey
.get(), encryptedProfile
);
269 if (newProfile
== null) {
271 profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
)
272 ).withLastUpdateTimestamp(System
.currentTimeMillis())
273 .withUnidentifiedAccessMode(ProfileUtils
.getUnidentifiedAccessMode(encryptedProfile
, null))
274 .withCapabilities(ProfileUtils
.getCapabilities(encryptedProfile
))
278 account
.getProfileStore().storeProfile(recipientId
, newProfile
);
281 var newIdentity
= account
.getIdentityKeyStore()
282 .saveIdentity(recipientId
,
283 new IdentityKey(Base64
.getDecoder().decode(encryptedProfile
.getIdentityKey())),
287 account
.getSessionStore().archiveSessions(recipientId
);
288 account
.getSenderKeyStore().deleteSharedWith(recipientId
);
290 } catch (InvalidKeyException ignored
) {
291 logger
.warn("Got invalid identity key in profile for {}",
292 addressResolver
.resolveSignalServiceAddress(recipientId
).getIdentifier());
295 logger
.warn("Failed to retrieve profile, ignoring: {}", e
.getMessage());
296 final var profile
= account
.getProfileStore().getProfile(recipientId
);
297 final var newProfile
= (
298 profile
== null ? Profile
.newBuilder() : Profile
.newBuilder(profile
)
299 ).withLastUpdateTimestamp(System
.currentTimeMillis())
300 .withUnidentifiedAccessMode(Profile
.UnidentifiedAccessMode
.UNKNOWN
)
301 .withCapabilities(Set
.of())
304 account
.getProfileStore().storeProfile(recipientId
, newProfile
);
308 private Single
<ProfileAndCredential
> retrieveProfile(
309 SignalServiceAddress address
,
310 Optional
<ProfileKey
> profileKey
,
311 Optional
<UnidentifiedAccess
> unidentifiedAccess
,
312 SignalServiceProfile
.RequestType requestType
314 final var profileService
= dependencies
.getProfileService();
315 final var locale
= Utils
.getDefaultLocale();
317 return profileService
.getProfile(address
, profileKey
, unidentifiedAccess
, requestType
, locale
).map(pair
-> {
318 var processor
= new ProfileService
.ProfileResponseProcessor(pair
);
319 if (processor
.hasResult()) {
320 return processor
.getResult();
321 } else if (processor
.notFound()) {
322 throw new NotFoundException("Profile not found");
324 throw pair
.getExecutionError()
325 .or(pair
.getApplicationError())
326 .or(new IOException("Unknown error while retrieving profile"));
331 private void downloadProfileAvatar(
332 SignalServiceAddress address
, String avatarPath
, ProfileKey profileKey
334 if (avatarPath
== null) {
336 avatarStore
.deleteProfileAvatar(address
);
337 } catch (IOException e
) {
338 logger
.warn("Failed to delete local profile avatar, ignoring: {}", e
.getMessage());
344 avatarStore
.storeProfileAvatar(address
,
345 outputStream
-> retrieveProfileAvatar(avatarPath
, profileKey
, outputStream
));
346 } catch (Throwable e
) {
347 if (e
instanceof AssertionError
&& e
.getCause() instanceof InterruptedException
) {
348 Thread
.currentThread().interrupt();
350 logger
.warn("Failed to download profile avatar, ignoring: {}", e
.getMessage());
354 private void retrieveProfileAvatar(
355 String avatarPath
, ProfileKey profileKey
, OutputStream outputStream
356 ) throws IOException
{
357 var tmpFile
= IOUtils
.createTempFile();
358 try (var input
= dependencies
.getMessageReceiver()
359 .retrieveProfileAvatar(avatarPath
,
362 ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
)) {
363 // Use larger buffer size to prevent AssertionError: Need: 12272 but only have: 8192 ...
364 IOUtils
.copyStream(input
, outputStream
, (int) ServiceConfig
.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE
);
367 Files
.delete(tmpFile
.toPath());
368 } catch (IOException e
) {
369 logger
.warn("Failed to delete received profile avatar temp file “{}”, ignoring: {}",
376 private Optional
<UnidentifiedAccess
> getUnidentifiedAccess(RecipientId recipientId
) {
377 var unidentifiedAccess
= unidentifiedAccessProvider
.getAccessFor(recipientId
, true);
379 if (unidentifiedAccess
.isPresent()) {
380 return unidentifiedAccess
.get().getTargetUnidentifiedAccess();
383 return Optional
.absent();