]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/ProvisioningManager.java
Return a Manager from ProvisioningManager and RegistrationManager when finished
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / ProvisioningManager.java
1 /*
2 Copyright (C) 2015-2021 AsamK and contributors
3
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.
8
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.
13
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/>.
16 */
17 package org.asamk.signal.manager;
18
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;
38
39 import java.io.File;
40 import java.io.IOException;
41 import java.util.concurrent.TimeoutException;
42
43 public class ProvisioningManager {
44
45 private final static Logger logger = LoggerFactory.getLogger(ProvisioningManager.class);
46
47 private final PathConfig pathConfig;
48 private final ServiceEnvironmentConfig serviceEnvironmentConfig;
49 private final String userAgent;
50
51 private final SignalServiceAccountManager accountManager;
52 private final IdentityKeyPair identityKey;
53 private final int registrationId;
54 private final String password;
55
56 ProvisioningManager(PathConfig pathConfig, ServiceEnvironmentConfig serviceEnvironmentConfig, String userAgent) {
57 this.pathConfig = pathConfig;
58 this.serviceEnvironmentConfig = serviceEnvironmentConfig;
59 this.userAgent = userAgent;
60
61 identityKey = KeyUtils.generateIdentityKeyPair();
62 registrationId = KeyHelper.generateRegistrationId(false);
63 password = KeyUtils.createPassword();
64 final SleepTimer timer = new UptimeSleepTimer();
65 GroupsV2Operations groupsV2Operations;
66 try {
67 groupsV2Operations = new GroupsV2Operations(ClientZkOperations.create(serviceEnvironmentConfig.getSignalServiceConfiguration()));
68 } catch (Throwable ignored) {
69 groupsV2Operations = null;
70 }
71 accountManager = new SignalServiceAccountManager(serviceEnvironmentConfig.getSignalServiceConfiguration(),
72 new DynamicCredentialsProvider(null, null, password, SignalServiceAddress.DEFAULT_DEVICE_ID),
73 userAgent,
74 groupsV2Operations,
75 ServiceConfig.AUTOMATIC_NETWORK_RETRY,
76 timer);
77 }
78
79 public static ProvisioningManager init(
80 File settingsPath, ServiceEnvironment serviceEnvironment, String userAgent
81 ) {
82 var pathConfig = PathConfig.createDefault(settingsPath);
83
84 final var serviceConfiguration = ServiceConfig.getServiceEnvironmentConfig(serviceEnvironment, userAgent);
85
86 return new ProvisioningManager(pathConfig, serviceConfiguration, userAgent);
87 }
88
89 public String getDeviceLinkUri() throws TimeoutException, IOException {
90 var deviceUuid = accountManager.getNewDeviceUuid();
91
92 return new DeviceLinkInfo(deviceUuid, identityKey.getPublicKey().getPublicKey()).createDeviceLinkUri();
93 }
94
95 public Manager finishDeviceLink(String deviceName) throws IOException, InvalidKeyException, TimeoutException, UserAlreadyExists {
96 var ret = accountManager.finishNewDeviceRegistration(identityKey, false, true, registrationId, deviceName);
97
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));
102 }
103
104 // Create new account with the synced identity
105 var profileKeyBytes = ret.getProfileKey();
106 ProfileKey profileKey;
107 if (profileKeyBytes == null) {
108 profileKey = KeyUtils.createProfileKey();
109 } else {
110 try {
111 profileKey = new ProfileKey(profileKeyBytes);
112 } catch (InvalidInputException e) {
113 throw new IOException("Received invalid profileKey", e);
114 }
115 }
116
117 SignalAccount account = null;
118 try {
119 account = SignalAccount.createLinkedAccount(pathConfig.getDataPath(),
120 username,
121 ret.getUuid(),
122 password,
123 ret.getDeviceId(),
124 ret.getIdentity(),
125 registrationId,
126 profileKey);
127 account.save();
128
129 Manager m = null;
130 try {
131 m = new Manager(account, pathConfig, serviceEnvironmentConfig, userAgent);
132
133 try {
134 m.refreshPreKeys();
135 } catch (Exception e) {
136 logger.error("Failed to refresh prekeys.");
137 throw e;
138 }
139
140 try {
141 m.requestSyncGroups();
142 m.requestSyncContacts();
143 m.requestSyncBlocked();
144 m.requestSyncConfiguration();
145 m.requestSyncKeys();
146 } catch (Exception e) {
147 logger.error("Failed to request sync messages from linked device.");
148 throw e;
149 }
150
151 account.save();
152
153 final var result = m;
154 account = null;
155 m = null;
156
157 return result;
158 } finally {
159 if (m != null) {
160 m.close();
161 }
162 }
163 } finally {
164 if (account != null) {
165 account.close();
166 }
167 }
168 }
169 }