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