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
;
19 import org
.asamk
.signal
.manager
.api
.CaptchaRequiredException
;
20 import org
.asamk
.signal
.manager
.api
.IncorrectPinException
;
21 import org
.asamk
.signal
.manager
.api
.NonNormalizedPhoneNumberException
;
22 import org
.asamk
.signal
.manager
.api
.PinLockedException
;
23 import org
.asamk
.signal
.manager
.api
.RateLimitException
;
24 import org
.asamk
.signal
.manager
.api
.UpdateProfile
;
25 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
26 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
27 import org
.asamk
.signal
.manager
.helper
.AccountFileUpdater
;
28 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
29 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
30 import org
.asamk
.signal
.manager
.util
.NumberVerificationUtils
;
31 import org
.asamk
.signal
.manager
.util
.Utils
;
32 import org
.signal
.libsignal
.usernames
.BaseUsernameException
;
33 import org
.slf4j
.Logger
;
34 import org
.slf4j
.LoggerFactory
;
35 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
36 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
37 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
38 import org
.whispersystems
.signalservice
.api
.push
.ACI
;
39 import org
.whispersystems
.signalservice
.api
.push
.PNI
;
40 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
41 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.AlreadyVerifiedException
;
42 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.DeprecatedVersionException
;
43 import org
.whispersystems
.signalservice
.internal
.push
.VerifyAccountResponse
;
44 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
46 import java
.io
.IOException
;
47 import java
.util
.function
.Consumer
;
49 class RegistrationManagerImpl
implements RegistrationManager
{
51 private final static Logger logger
= LoggerFactory
.getLogger(RegistrationManagerImpl
.class);
53 private SignalAccount account
;
54 private final PathConfig pathConfig
;
55 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
56 private final String userAgent
;
57 private final Consumer
<Manager
> newManagerListener
;
59 private final SignalServiceAccountManager accountManager
;
60 private final PinHelper pinHelper
;
61 private final AccountFileUpdater accountFileUpdater
;
63 RegistrationManagerImpl(
64 SignalAccount account
,
65 PathConfig pathConfig
,
66 ServiceEnvironmentConfig serviceEnvironmentConfig
,
68 Consumer
<Manager
> newManagerListener
,
69 AccountFileUpdater accountFileUpdater
71 this.account
= account
;
72 this.pathConfig
= pathConfig
;
73 this.accountFileUpdater
= accountFileUpdater
;
74 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
75 this.userAgent
= userAgent
;
76 this.newManagerListener
= newManagerListener
;
78 GroupsV2Operations groupsV2Operations
;
80 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()),
81 ServiceConfig
.GROUP_MAX_SIZE
);
82 } catch (Throwable ignored
) {
83 groupsV2Operations
= null;
85 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
86 new DynamicCredentialsProvider(
87 // Using empty UUID, because registering doesn't work otherwise
88 null, null, account
.getNumber(), account
.getPassword(), SignalServiceAddress
.DEFAULT_DEVICE_ID
),
91 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
92 final var keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
93 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
94 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
95 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
97 final var fallbackKeyBackupServices
= serviceEnvironmentConfig
.getFallbackKeyBackupConfigs()
99 .map(config
-> accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
100 config
.getEnclaveName(),
101 config
.getServiceId(),
102 config
.getMrenclave(),
105 this.pinHelper
= new PinHelper(keyBackupService
, fallbackKeyBackupServices
);
109 public void register(
110 boolean voiceVerification
, String captcha
111 ) throws IOException
, CaptchaRequiredException
, NonNormalizedPhoneNumberException
, RateLimitException
{
112 if (account
.isRegistered()
113 && account
.getServiceEnvironment() != null
114 && account
.getServiceEnvironment() != serviceEnvironmentConfig
.getType()) {
115 throw new IOException("Account is registered in another environment: " + account
.getServiceEnvironment());
119 if (account
.getAci() != null && attemptReactivateAccount()) {
123 String sessionId
= NumberVerificationUtils
.handleVerificationSession(accountManager
,
124 account
.getSessionId(account
.getNumber()),
125 id
-> account
.setSessionId(account
.getNumber(), id
),
128 NumberVerificationUtils
.requestVerificationCode(accountManager
, sessionId
, voiceVerification
);
129 } catch (DeprecatedVersionException e
) {
130 logger
.debug("Signal-Server returned deprecated version exception", e
);
136 public void verifyAccount(
137 String verificationCode
, String pin
138 ) throws IOException
, PinLockedException
, IncorrectPinException
{
139 var sessionId
= account
.getSessionId(account
.getNumber());
140 final var result
= NumberVerificationUtils
.verifyNumber(sessionId
,
144 this::verifyAccountWithCode
);
145 final var response
= result
.first();
146 final var masterKey
= result
.second();
147 if (masterKey
== null) {
151 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
152 final var aci
= ACI
.parseOrNull(response
.getUuid());
153 final var pni
= PNI
.parseOrNull(response
.getPni());
154 account
.finishRegistration(aci
, pni
, masterKey
, pin
);
155 accountFileUpdater
.updateAccountIdentifiers(account
.getNumber(), aci
);
157 ManagerImpl m
= null;
159 m
= new ManagerImpl(account
, pathConfig
, accountFileUpdater
, serviceEnvironmentConfig
, userAgent
);
163 if (response
.isStorageCapable()) {
164 m
.retrieveRemoteStorage();
166 // Set an initial empty profile so user can be added to groups
168 m
.updateProfile(UpdateProfile
.newBuilder().build());
169 } catch (NoClassDefFoundError e
) {
170 logger
.warn("Failed to set default profile: {}", e
.getMessage());
174 m
.refreshCurrentUsername();
175 } catch (IOException
| BaseUsernameException e
) {
176 logger
.warn("Failed to refresh current username", e
);
179 if (newManagerListener
!= null) {
180 newManagerListener
.accept(m
);
191 public void deleteLocalAccountData() throws IOException
{
192 account
.deleteAccountData();
193 accountFileUpdater
.removeAccount();
198 public boolean isRegistered() {
199 return account
.isRegistered();
202 private boolean attemptReactivateAccount() {
204 final var accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
205 account
.getCredentialsProvider(),
208 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
209 accountManager
.setAccountAttributes(account
.getAccountAttributes(null));
210 account
.setRegistered(true);
211 logger
.info("Reactivated existing account, verify is not necessary.");
212 if (newManagerListener
!= null) {
213 final var m
= new ManagerImpl(account
,
216 serviceEnvironmentConfig
,
219 newManagerListener
.accept(m
);
222 } catch (IOException e
) {
223 logger
.debug("Failed to reactivate account");
228 private VerifyAccountResponse
verifyAccountWithCode(
229 final String sessionId
, final String verificationCode
, final String registrationLock
230 ) throws IOException
{
232 Utils
.handleResponseException(accountManager
.verifyAccount(verificationCode
, sessionId
));
233 } catch (AlreadyVerifiedException e
) {
234 // Already verified so can continue registering
236 return Utils
.handleResponseException(accountManager
.registerAccount(sessionId
,
238 account
.getAccountAttributes(registrationLock
),
243 public void close() {
244 if (account
!= null) {