1 package org
.asamk
.signal
.manager
.helper
;
3 import org
.asamk
.signal
.manager
.DeviceLinkInfo
;
4 import org
.asamk
.signal
.manager
.SignalDependencies
;
5 import org
.asamk
.signal
.manager
.api
.InvalidDeviceLinkException
;
6 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
7 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
8 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
9 import org
.slf4j
.Logger
;
10 import org
.slf4j
.LoggerFactory
;
11 import org
.whispersystems
.libsignal
.InvalidKeyException
;
12 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
13 import org
.whispersystems
.signalservice
.api
.push
.ACI
;
14 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.AuthorizationFailedException
;
15 import org
.whispersystems
.signalservice
.api
.util
.DeviceNameUtil
;
17 import java
.io
.IOException
;
18 import java
.util
.concurrent
.TimeUnit
;
20 public class AccountHelper
{
22 private final static Logger logger
= LoggerFactory
.getLogger(AccountHelper
.class);
24 private final Context context
;
25 private final SignalAccount account
;
26 private final SignalDependencies dependencies
;
28 private Callable unregisteredListener
;
30 public AccountHelper(final Context context
) {
31 this.account
= context
.getAccount();
32 this.dependencies
= context
.getDependencies();
33 this.context
= context
;
36 public void setUnregisteredListener(final Callable unregisteredListener
) {
37 this.unregisteredListener
= unregisteredListener
;
40 public void checkAccountState() throws IOException
{
41 if (account
.getLastReceiveTimestamp() == 0) {
42 logger
.info("The Signal protocol expects that incoming messages are regularly received.");
44 var diffInMilliseconds
= System
.currentTimeMillis() - account
.getLastReceiveTimestamp();
45 long days
= TimeUnit
.DAYS
.convert(diffInMilliseconds
, TimeUnit
.MILLISECONDS
);
48 "Messages have been last received {} days ago. The Signal protocol expects that incoming messages are regularly received.",
53 context
.getPreKeyHelper().refreshPreKeysIfNecessary();
54 if (account
.getAci() == null) {
55 account
.setAci(ACI
.parseOrNull(dependencies
.getAccountManager().getWhoAmI().getAci()));
57 updateAccountAttributes();
58 } catch (AuthorizationFailedException e
) {
59 account
.setRegistered(false);
64 public void setDeviceName(String deviceName
) {
65 final var privateKey
= account
.getIdentityKeyPair().getPrivateKey();
66 final var encryptedDeviceName
= DeviceNameUtil
.encryptDeviceName(deviceName
, privateKey
);
67 account
.setEncryptedDeviceName(encryptedDeviceName
);
70 public void updateAccountAttributes() throws IOException
{
71 dependencies
.getAccountManager()
72 .setAccountAttributes(account
.getEncryptedDeviceName(),
74 account
.getLocalRegistrationId(),
77 account
.getPinMasterKey() == null ?
null : account
.getPinMasterKey().deriveRegistrationLock(),
78 account
.getSelfUnidentifiedAccessKey(),
79 account
.isUnrestrictedUnidentifiedAccess(),
80 ServiceConfig
.capabilities
,
81 account
.isDiscoverableByPhoneNumber());
84 public void addDevice(DeviceLinkInfo deviceLinkInfo
) throws IOException
, InvalidDeviceLinkException
{
85 var identityKeyPair
= account
.getIdentityKeyPair();
86 var verificationCode
= dependencies
.getAccountManager().getNewDeviceVerificationCode();
89 dependencies
.getAccountManager()
90 .addDevice(deviceLinkInfo
.deviceIdentifier(),
91 deviceLinkInfo
.deviceKey(),
93 Optional
.of(account
.getProfileKey().serialize()),
95 } catch (InvalidKeyException e
) {
96 throw new InvalidDeviceLinkException("Invalid device link", e
);
98 account
.setMultiDevice(true);
101 public void removeLinkedDevices(long deviceId
) throws IOException
{
102 dependencies
.getAccountManager().removeDevice(deviceId
);
103 var devices
= dependencies
.getAccountManager().getDevices();
104 account
.setMultiDevice(devices
.size() > 1);
107 public void setRegistrationPin(String pin
) throws IOException
{
108 final var masterKey
= account
.getPinMasterKey() != null
109 ? account
.getPinMasterKey()
110 : KeyUtils
.createMasterKey();
112 context
.getPinHelper().setRegistrationLockPin(pin
, masterKey
);
114 account
.setRegistrationLockPin(pin
, masterKey
);
117 public void removeRegistrationPin() throws IOException
{
119 context
.getPinHelper().removeRegistrationLockPin();
121 account
.setRegistrationLockPin(null, null);
124 public void unregister() throws IOException
{
125 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
126 // If this is the master device, other users can't send messages to this number anymore.
127 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
128 dependencies
.getAccountManager().setGcmId(Optional
.absent());
130 account
.setRegistered(false);
131 unregisteredListener
.call();
134 public void deleteAccount() throws IOException
{
136 context
.getPinHelper().removeRegistrationLockPin();
137 } catch (IOException e
) {
138 logger
.warn("Failed to remove registration lock pin");
140 account
.setRegistrationLockPin(null, null);
142 dependencies
.getAccountManager().deleteAccount();
144 account
.setRegistered(false);
145 unregisteredListener
.call();
148 public interface Callable
{