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
.internal
.push
.VerifyAccountResponse
;
43 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
45 import java
.io
.IOException
;
46 import java
.util
.function
.Consumer
;
48 class RegistrationManagerImpl
implements RegistrationManager
{
50 private final static Logger logger
= LoggerFactory
.getLogger(RegistrationManagerImpl
.class);
52 private SignalAccount account
;
53 private final PathConfig pathConfig
;
54 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
55 private final String userAgent
;
56 private final Consumer
<Manager
> newManagerListener
;
58 private final SignalServiceAccountManager accountManager
;
59 private final PinHelper pinHelper
;
60 private final AccountFileUpdater accountFileUpdater
;
62 RegistrationManagerImpl(
63 SignalAccount account
,
64 PathConfig pathConfig
,
65 ServiceEnvironmentConfig serviceEnvironmentConfig
,
67 Consumer
<Manager
> newManagerListener
,
68 AccountFileUpdater accountFileUpdater
70 this.account
= account
;
71 this.pathConfig
= pathConfig
;
72 this.accountFileUpdater
= accountFileUpdater
;
73 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
74 this.userAgent
= userAgent
;
75 this.newManagerListener
= newManagerListener
;
77 GroupsV2Operations groupsV2Operations
;
79 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()),
80 ServiceConfig
.GROUP_MAX_SIZE
);
81 } catch (Throwable ignored
) {
82 groupsV2Operations
= null;
84 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
85 new DynamicCredentialsProvider(
86 // Using empty UUID, because registering doesn't work otherwise
87 null, null, account
.getNumber(), account
.getPassword(), SignalServiceAddress
.DEFAULT_DEVICE_ID
),
90 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
91 final var keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
92 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
93 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
94 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
96 final var fallbackKeyBackupServices
= serviceEnvironmentConfig
.getFallbackKeyBackupConfigs()
98 .map(config
-> accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
99 config
.getEnclaveName(),
100 config
.getServiceId(),
101 config
.getMrenclave(),
104 this.pinHelper
= new PinHelper(keyBackupService
, fallbackKeyBackupServices
);
108 public void register(
109 boolean voiceVerification
, String captcha
110 ) throws IOException
, CaptchaRequiredException
, NonNormalizedPhoneNumberException
, RateLimitException
{
111 if (account
.isRegistered()
112 && account
.getServiceEnvironment() != null
113 && account
.getServiceEnvironment() != serviceEnvironmentConfig
.getType()) {
114 throw new IOException("Account is registered in another environment: " + account
.getServiceEnvironment());
117 if (account
.getAci() != null && attemptReactivateAccount()) {
121 String sessionId
= NumberVerificationUtils
.handleVerificationSession(accountManager
,
122 account
.getSessionId(account
.getNumber()),
123 id
-> account
.setSessionId(account
.getNumber(), id
),
126 NumberVerificationUtils
.requestVerificationCode(accountManager
, sessionId
, voiceVerification
);
130 public void verifyAccount(
131 String verificationCode
, String pin
132 ) throws IOException
, PinLockedException
, IncorrectPinException
{
133 var sessionId
= account
.getSessionId(account
.getNumber());
134 final var result
= NumberVerificationUtils
.verifyNumber(sessionId
,
138 this::verifyAccountWithCode
);
139 final var response
= result
.first();
140 final var masterKey
= result
.second();
141 if (masterKey
== null) {
145 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
146 final var aci
= ACI
.parseOrNull(response
.getUuid());
147 final var pni
= PNI
.parseOrNull(response
.getPni());
148 account
.finishRegistration(aci
, pni
, masterKey
, pin
);
149 accountFileUpdater
.updateAccountIdentifiers(account
.getNumber(), aci
);
151 ManagerImpl m
= null;
153 m
= new ManagerImpl(account
, pathConfig
, accountFileUpdater
, serviceEnvironmentConfig
, userAgent
);
157 if (response
.isStorageCapable()) {
158 m
.retrieveRemoteStorage();
160 // Set an initial empty profile so user can be added to groups
162 m
.updateProfile(UpdateProfile
.newBuilder().build());
163 } catch (NoClassDefFoundError e
) {
164 logger
.warn("Failed to set default profile: {}", e
.getMessage());
168 m
.refreshCurrentUsername();
169 } catch (IOException
| BaseUsernameException e
) {
170 logger
.warn("Failed to refresh current username", e
);
173 if (newManagerListener
!= null) {
174 newManagerListener
.accept(m
);
185 public void deleteLocalAccountData() throws IOException
{
186 account
.deleteAccountData();
187 accountFileUpdater
.removeAccount();
192 public boolean isRegistered() {
193 return account
.isRegistered();
196 private boolean attemptReactivateAccount() {
198 final var accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
199 account
.getCredentialsProvider(),
202 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
203 accountManager
.setAccountAttributes(account
.getAccountAttributes(null));
204 account
.setRegistered(true);
205 logger
.info("Reactivated existing account, verify is not necessary.");
206 if (newManagerListener
!= null) {
207 final var m
= new ManagerImpl(account
,
210 serviceEnvironmentConfig
,
213 newManagerListener
.accept(m
);
216 } catch (IOException e
) {
217 logger
.debug("Failed to reactivate account");
222 private VerifyAccountResponse
verifyAccountWithCode(
223 final String sessionId
, final String verificationCode
, final String registrationLock
224 ) throws IOException
{
226 Utils
.handleResponseException(accountManager
.verifyAccount(verificationCode
, sessionId
));
227 } catch (AlreadyVerifiedException e
) {
228 // Already verified so can continue registering
230 return Utils
.handleResponseException(accountManager
.registerAccount(sessionId
,
232 account
.getAccountAttributes(registrationLock
),
237 public void close() {
238 if (account
!= null) {