2 Copyright (C) 2015-2021 AsamK and contributors
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org
.asamk
.signal
.manager
;
19 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
20 import org
.asamk
.signal
.manager
.config
.ServiceEnvironment
;
21 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
22 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
23 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
24 import org
.signal
.zkgroup
.InvalidInputException
;
25 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
26 import org
.slf4j
.Logger
;
27 import org
.slf4j
.LoggerFactory
;
28 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
29 import org
.whispersystems
.libsignal
.InvalidKeyException
;
30 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
31 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
32 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
33 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
34 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
35 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
36 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
37 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
40 import java
.io
.IOException
;
41 import java
.util
.concurrent
.TimeoutException
;
43 public class ProvisioningManager
{
45 private final static Logger logger
= LoggerFactory
.getLogger(ProvisioningManager
.class);
47 private final PathConfig pathConfig
;
48 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
49 private final String userAgent
;
51 private final SignalServiceAccountManager accountManager
;
52 private final IdentityKeyPair identityKey
;
53 private final int registrationId
;
54 private final String password
;
56 ProvisioningManager(PathConfig pathConfig
, ServiceEnvironmentConfig serviceEnvironmentConfig
, String userAgent
) {
57 this.pathConfig
= pathConfig
;
58 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
59 this.userAgent
= userAgent
;
61 identityKey
= KeyUtils
.generateIdentityKeyPair();
62 registrationId
= KeyHelper
.generateRegistrationId(false);
63 password
= KeyUtils
.createPassword();
64 final SleepTimer timer
= new UptimeSleepTimer();
65 GroupsV2Operations groupsV2Operations
;
67 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()));
68 } catch (Throwable ignored
) {
69 groupsV2Operations
= null;
71 accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
72 new DynamicCredentialsProvider(null, null, password
, SignalServiceAddress
.DEFAULT_DEVICE_ID
),
75 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
,
79 public static ProvisioningManager
init(
80 File settingsPath
, ServiceEnvironment serviceEnvironment
, String userAgent
82 var pathConfig
= PathConfig
.createDefault(settingsPath
);
84 final var serviceConfiguration
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
86 return new ProvisioningManager(pathConfig
, serviceConfiguration
, userAgent
);
89 public String
getDeviceLinkUri() throws TimeoutException
, IOException
{
90 var deviceUuid
= accountManager
.getNewDeviceUuid();
92 return new DeviceLinkInfo(deviceUuid
, identityKey
.getPublicKey().getPublicKey()).createDeviceLinkUri();
95 public Manager
finishDeviceLink(String deviceName
) throws IOException
, InvalidKeyException
, TimeoutException
, UserAlreadyExists
{
96 var ret
= accountManager
.finishNewDeviceRegistration(identityKey
, false, true, registrationId
, deviceName
);
98 var username
= ret
.getNumber();
99 // TODO do this check before actually registering
100 if (SignalAccount
.userExists(pathConfig
.getDataPath(), username
)) {
101 throw new UserAlreadyExists(username
, SignalAccount
.getFileName(pathConfig
.getDataPath(), username
));
104 // Create new account with the synced identity
105 var profileKeyBytes
= ret
.getProfileKey();
106 ProfileKey profileKey
;
107 if (profileKeyBytes
== null) {
108 profileKey
= KeyUtils
.createProfileKey();
111 profileKey
= new ProfileKey(profileKeyBytes
);
112 } catch (InvalidInputException e
) {
113 throw new IOException("Received invalid profileKey", e
);
117 SignalAccount account
= null;
119 account
= SignalAccount
.createLinkedAccount(pathConfig
.getDataPath(),
131 m
= new Manager(account
, pathConfig
, serviceEnvironmentConfig
, userAgent
);
135 } catch (Exception e
) {
136 logger
.error("Failed to refresh prekeys.");
141 m
.requestSyncGroups();
142 m
.requestSyncContacts();
143 m
.requestSyncBlocked();
144 m
.requestSyncConfiguration();
146 } catch (Exception e
) {
147 logger
.error("Failed to request sync messages from linked device.");
153 final var result
= m
;
164 if (account
!= null) {