]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/manager/ProvisioningManager.java
Extract JsonWriter for json output
[signal-cli] / 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.storage.SignalAccount;
20 import org.asamk.signal.manager.util.KeyUtils;
21 import org.signal.zkgroup.InvalidInputException;
22 import org.signal.zkgroup.profiles.ProfileKey;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.whispersystems.libsignal.IdentityKeyPair;
26 import org.whispersystems.libsignal.InvalidKeyException;
27 import org.whispersystems.libsignal.util.KeyHelper;
28 import org.whispersystems.signalservice.api.SignalServiceAccountManager;
29 import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations;
30 import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations;
31 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
32 import org.whispersystems.signalservice.api.util.SleepTimer;
33 import org.whispersystems.signalservice.api.util.UptimeSleepTimer;
34 import org.whispersystems.signalservice.internal.configuration.SignalServiceConfiguration;
35 import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.concurrent.TimeoutException;
40
41 public class ProvisioningManager {
42
43 private final static Logger logger = LoggerFactory.getLogger(Manager.class);
44
45 private final PathConfig pathConfig;
46 private final SignalServiceConfiguration serviceConfiguration;
47 private final String userAgent;
48
49 private final SignalServiceAccountManager accountManager;
50 private final IdentityKeyPair identityKey;
51 private final int registrationId;
52 private final String password;
53
54 public ProvisioningManager(File settingsPath, SignalServiceConfiguration serviceConfiguration, String userAgent) {
55 this.pathConfig = PathConfig.createDefault(settingsPath);
56 this.serviceConfiguration = serviceConfiguration;
57 this.userAgent = userAgent;
58
59 identityKey = KeyUtils.generateIdentityKeyPair();
60 registrationId = KeyHelper.generateRegistrationId(false);
61 password = KeyUtils.createPassword();
62 final SleepTimer timer = new UptimeSleepTimer();
63 GroupsV2Operations groupsV2Operations;
64 try {
65 groupsV2Operations = new GroupsV2Operations(ClientZkOperations.create(serviceConfiguration));
66 } catch (Throwable ignored) {
67 groupsV2Operations = null;
68 }
69 accountManager = new SignalServiceAccountManager(serviceConfiguration,
70 new DynamicCredentialsProvider(null, null, password, null, SignalServiceAddress.DEFAULT_DEVICE_ID),
71 userAgent,
72 groupsV2Operations,
73 timer);
74 }
75
76 public String getDeviceLinkUri() throws TimeoutException, IOException {
77 String deviceUuid = accountManager.getNewDeviceUuid();
78
79 return new DeviceLinkInfo(deviceUuid, identityKey.getPublicKey().getPublicKey()).createDeviceLinkUri();
80 }
81
82 public String finishDeviceLink(String deviceName) throws IOException, InvalidKeyException, TimeoutException, UserAlreadyExists {
83 String signalingKey = KeyUtils.createSignalingKey();
84 SignalServiceAccountManager.NewDeviceRegistrationReturn ret = accountManager.finishNewDeviceRegistration(
85 identityKey,
86 signalingKey,
87 false,
88 true,
89 registrationId,
90 deviceName);
91
92 String username = ret.getNumber();
93 // TODO do this check before actually registering
94 if (SignalAccount.userExists(pathConfig.getDataPath(), username)) {
95 throw new UserAlreadyExists(username, SignalAccount.getFileName(pathConfig.getDataPath(), username));
96 }
97
98 // Create new account with the synced identity
99 byte[] profileKeyBytes = ret.getProfileKey();
100 ProfileKey profileKey;
101 if (profileKeyBytes == null) {
102 profileKey = KeyUtils.createProfileKey();
103 } else {
104 try {
105 profileKey = new ProfileKey(profileKeyBytes);
106 } catch (InvalidInputException e) {
107 throw new IOException("Received invalid profileKey", e);
108 }
109 }
110
111 try (SignalAccount account = SignalAccount.createLinkedAccount(pathConfig.getDataPath(),
112 username,
113 ret.getUuid(),
114 password,
115 ret.getDeviceId(),
116 ret.getIdentity(),
117 registrationId,
118 signalingKey,
119 profileKey)) {
120 account.save();
121
122 try (Manager m = new Manager(account, pathConfig, serviceConfiguration, userAgent)) {
123
124 try {
125 m.refreshPreKeys();
126 } catch (Exception e) {
127 logger.error("Failed to refresh prekeys.");
128 throw e;
129 }
130
131 try {
132 m.requestSyncGroups();
133 m.requestSyncContacts();
134 m.requestSyncBlocked();
135 m.requestSyncConfiguration();
136 } catch (Exception e) {
137 logger.error("Failed to request sync messages from linked device.");
138 throw e;
139 }
140
141 m.close(false);
142 }
143
144 account.save();
145 }
146
147 return username;
148 }
149 }