2 Copyright (C) 2015-2022 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
.internal
;
19 import org
.asamk
.signal
.manager
.Manager
;
20 import org
.asamk
.signal
.manager
.RegistrationManager
;
21 import org
.asamk
.signal
.manager
.api
.CaptchaRequiredException
;
22 import org
.asamk
.signal
.manager
.api
.IncorrectPinException
;
23 import org
.asamk
.signal
.manager
.api
.NonNormalizedPhoneNumberException
;
24 import org
.asamk
.signal
.manager
.api
.PinLockedException
;
25 import org
.asamk
.signal
.manager
.api
.RateLimitException
;
26 import org
.asamk
.signal
.manager
.api
.UpdateProfile
;
27 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
28 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
29 import org
.asamk
.signal
.manager
.helper
.AccountFileUpdater
;
30 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
31 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
32 import org
.asamk
.signal
.manager
.util
.NumberVerificationUtils
;
33 import org
.asamk
.signal
.manager
.util
.Utils
;
34 import org
.signal
.libsignal
.usernames
.BaseUsernameException
;
35 import org
.slf4j
.Logger
;
36 import org
.slf4j
.LoggerFactory
;
37 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
38 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
39 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
40 import org
.whispersystems
.signalservice
.api
.push
.ACI
;
41 import org
.whispersystems
.signalservice
.api
.push
.PNI
;
42 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
43 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.AlreadyVerifiedException
;
44 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.DeprecatedVersionException
;
45 import org
.whispersystems
.signalservice
.internal
.push
.VerifyAccountResponse
;
46 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
48 import java
.io
.IOException
;
49 import java
.util
.function
.Consumer
;
51 public class RegistrationManagerImpl
implements RegistrationManager
{
53 private final static Logger logger
= LoggerFactory
.getLogger(RegistrationManagerImpl
.class);
55 private SignalAccount account
;
56 private final PathConfig pathConfig
;
57 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
58 private final String userAgent
;
59 private final Consumer
<Manager
> newManagerListener
;
61 private final SignalServiceAccountManager accountManager
;
62 private final PinHelper pinHelper
;
63 private final AccountFileUpdater accountFileUpdater
;
65 public RegistrationManagerImpl(
66 SignalAccount account
,
67 PathConfig pathConfig
,
68 ServiceEnvironmentConfig serviceEnvironmentConfig
,
70 Consumer
<Manager
> newManagerListener
,
71 AccountFileUpdater accountFileUpdater
73 this.account
= account
;
74 this.pathConfig
= pathConfig
;
75 this.accountFileUpdater
= accountFileUpdater
;
76 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
77 this.userAgent
= userAgent
;
78 this.newManagerListener
= newManagerListener
;
80 GroupsV2Operations groupsV2Operations
;
82 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()),
83 ServiceConfig
.GROUP_MAX_SIZE
);
84 } catch (Throwable ignored
) {
85 groupsV2Operations
= null;
87 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
88 new DynamicCredentialsProvider(
89 // Using empty UUID, because registering doesn't work otherwise
90 null, null, account
.getNumber(), account
.getPassword(), SignalServiceAddress
.DEFAULT_DEVICE_ID
),
93 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
94 final var keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
95 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
96 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
97 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
99 final var fallbackKeyBackupServices
= serviceEnvironmentConfig
.getFallbackKeyBackupConfigs()
101 .map(config
-> accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
102 config
.getEnclaveName(),
103 config
.getServiceId(),
104 config
.getMrenclave(),
107 this.pinHelper
= new PinHelper(keyBackupService
, fallbackKeyBackupServices
);
111 public void register(
112 boolean voiceVerification
, String captcha
113 ) throws IOException
, CaptchaRequiredException
, NonNormalizedPhoneNumberException
, RateLimitException
{
114 if (account
.isRegistered()
115 && account
.getServiceEnvironment() != null
116 && account
.getServiceEnvironment() != serviceEnvironmentConfig
.getType()) {
117 throw new IOException("Account is registered in another environment: " + account
.getServiceEnvironment());
121 if (account
.getAci() != null && attemptReactivateAccount()) {
125 String sessionId
= NumberVerificationUtils
.handleVerificationSession(accountManager
,
126 account
.getSessionId(account
.getNumber()),
127 id
-> account
.setSessionId(account
.getNumber(), id
),
130 NumberVerificationUtils
.requestVerificationCode(accountManager
, sessionId
, voiceVerification
);
131 } catch (DeprecatedVersionException e
) {
132 logger
.debug("Signal-Server returned deprecated version exception", e
);
138 public void verifyAccount(
139 String verificationCode
, String pin
140 ) throws IOException
, PinLockedException
, IncorrectPinException
{
141 var sessionId
= account
.getSessionId(account
.getNumber());
142 final var result
= NumberVerificationUtils
.verifyNumber(sessionId
,
146 this::verifyAccountWithCode
);
147 final var response
= result
.first();
148 final var masterKey
= result
.second();
149 if (masterKey
== null) {
153 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
154 final var aci
= ACI
.parseOrNull(response
.getUuid());
155 final var pni
= PNI
.parseOrNull(response
.getPni());
156 account
.finishRegistration(aci
, pni
, masterKey
, pin
);
157 accountFileUpdater
.updateAccountIdentifiers(account
.getNumber(), aci
);
159 ManagerImpl m
= null;
161 m
= new ManagerImpl(account
, pathConfig
, accountFileUpdater
, serviceEnvironmentConfig
, userAgent
);
165 if (response
.isStorageCapable()) {
166 m
.retrieveRemoteStorage();
168 // Set an initial empty profile so user can be added to groups
170 m
.updateProfile(UpdateProfile
.newBuilder().build());
171 } catch (NoClassDefFoundError e
) {
172 logger
.warn("Failed to set default profile: {}", e
.getMessage());
176 m
.refreshCurrentUsername();
177 } catch (IOException
| BaseUsernameException e
) {
178 logger
.warn("Failed to refresh current username", e
);
181 if (newManagerListener
!= null) {
182 newManagerListener
.accept(m
);
193 public void deleteLocalAccountData() throws IOException
{
194 account
.deleteAccountData();
195 accountFileUpdater
.removeAccount();
200 public boolean isRegistered() {
201 return account
.isRegistered();
204 private boolean attemptReactivateAccount() {
206 final var accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
207 account
.getCredentialsProvider(),
210 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
211 accountManager
.setAccountAttributes(account
.getAccountAttributes(null));
212 account
.setRegistered(true);
213 logger
.info("Reactivated existing account, verify is not necessary.");
214 if (newManagerListener
!= null) {
215 final var m
= new ManagerImpl(account
,
218 serviceEnvironmentConfig
,
221 newManagerListener
.accept(m
);
224 } catch (IOException e
) {
225 logger
.debug("Failed to reactivate account");
230 private VerifyAccountResponse
verifyAccountWithCode(
231 final String sessionId
, final String verificationCode
, final String registrationLock
232 ) throws IOException
{
234 Utils
.handleResponseException(accountManager
.verifyAccount(verificationCode
, sessionId
));
235 } catch (AlreadyVerifiedException e
) {
236 // Already verified so can continue registering
238 return Utils
.handleResponseException(accountManager
.registerAccount(sessionId
,
240 account
.getAccountAttributes(registrationLock
),
245 public void close() {
246 if (account
!= null) {