]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/ProvisioningManager.java
Refactor ReceiveMessageHandler
[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 PathConfig pathConfig = PathConfig.createDefault(settingsPath);
83
84 final ServiceEnvironmentConfig serviceConfiguration = ServiceConfig.getServiceEnvironmentConfig(
85 serviceEnvironment,
86 userAgent);
87
88 return new ProvisioningManager(pathConfig, serviceConfiguration, userAgent);
89 }
90
91 public String getDeviceLinkUri() throws TimeoutException, IOException {
92 String deviceUuid = accountManager.getNewDeviceUuid();
93
94 return new DeviceLinkInfo(deviceUuid, identityKey.getPublicKey().getPublicKey()).createDeviceLinkUri();
95 }
96
97 public String finishDeviceLink(String deviceName) throws IOException, InvalidKeyException, TimeoutException, UserAlreadyExists {
98 SignalServiceAccountManager.NewDeviceRegistrationReturn ret = accountManager.finishNewDeviceRegistration(
99 identityKey,
100 false,
101 true,
102 registrationId,
103 deviceName);
104
105 String username = ret.getNumber();
106 // TODO do this check before actually registering
107 if (SignalAccount.userExists(pathConfig.getDataPath(), username)) {
108 throw new UserAlreadyExists(username, SignalAccount.getFileName(pathConfig.getDataPath(), username));
109 }
110
111 // Create new account with the synced identity
112 byte[] profileKeyBytes = ret.getProfileKey();
113 ProfileKey profileKey;
114 if (profileKeyBytes == null) {
115 profileKey = KeyUtils.createProfileKey();
116 } else {
117 try {
118 profileKey = new ProfileKey(profileKeyBytes);
119 } catch (InvalidInputException e) {
120 throw new IOException("Received invalid profileKey", e);
121 }
122 }
123
124 try (SignalAccount account = SignalAccount.createLinkedAccount(pathConfig.getDataPath(),
125 username,
126 ret.getUuid(),
127 password,
128 ret.getDeviceId(),
129 ret.getIdentity(),
130 registrationId,
131 profileKey)) {
132 account.save();
133
134 try (Manager m = new Manager(account, pathConfig, serviceEnvironmentConfig, userAgent)) {
135
136 try {
137 m.refreshPreKeys();
138 } catch (Exception e) {
139 logger.error("Failed to refresh prekeys.");
140 throw e;
141 }
142
143 try {
144 m.requestSyncGroups();
145 m.requestSyncContacts();
146 m.requestSyncBlocked();
147 m.requestSyncConfiguration();
148 m.requestSyncKeys();
149 } catch (Exception e) {
150 logger.error("Failed to request sync messages from linked device.");
151 throw e;
152 }
153
154 m.close(false);
155 }
156
157 account.save();
158 }
159
160 return username;
161 }
162 }