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
.slf4j
.Logger
;
33 import org
.slf4j
.LoggerFactory
;
34 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
35 import org
.whispersystems
.signalservice
.api
.groupsv2
.ClientZkOperations
;
36 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupsV2Operations
;
37 import org
.whispersystems
.signalservice
.api
.push
.ACI
;
38 import org
.whispersystems
.signalservice
.api
.push
.PNI
;
39 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
40 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.AlreadyVerifiedException
;
41 import org
.whispersystems
.signalservice
.internal
.push
.VerifyAccountResponse
;
42 import org
.whispersystems
.signalservice
.internal
.util
.DynamicCredentialsProvider
;
44 import java
.io
.IOException
;
45 import java
.util
.function
.Consumer
;
47 class RegistrationManagerImpl
implements RegistrationManager
{
49 private final static Logger logger
= LoggerFactory
.getLogger(RegistrationManagerImpl
.class);
51 private SignalAccount account
;
52 private final PathConfig pathConfig
;
53 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
54 private final String userAgent
;
55 private final Consumer
<Manager
> newManagerListener
;
57 private final SignalServiceAccountManager accountManager
;
58 private final PinHelper pinHelper
;
59 private final AccountFileUpdater accountFileUpdater
;
61 RegistrationManagerImpl(
62 SignalAccount account
,
63 PathConfig pathConfig
,
64 ServiceEnvironmentConfig serviceEnvironmentConfig
,
66 Consumer
<Manager
> newManagerListener
,
67 AccountFileUpdater accountFileUpdater
69 this.account
= account
;
70 this.pathConfig
= pathConfig
;
71 this.accountFileUpdater
= accountFileUpdater
;
72 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
73 this.userAgent
= userAgent
;
74 this.newManagerListener
= newManagerListener
;
76 GroupsV2Operations groupsV2Operations
;
78 groupsV2Operations
= new GroupsV2Operations(ClientZkOperations
.create(serviceEnvironmentConfig
.getSignalServiceConfiguration()),
79 ServiceConfig
.GROUP_MAX_SIZE
);
80 } catch (Throwable ignored
) {
81 groupsV2Operations
= null;
83 this.accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
84 new DynamicCredentialsProvider(
85 // Using empty UUID, because registering doesn't work otherwise
86 null, null, account
.getNumber(), account
.getPassword(), SignalServiceAddress
.DEFAULT_DEVICE_ID
),
89 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
90 final var keyBackupService
= accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
91 serviceEnvironmentConfig
.getKeyBackupConfig().getEnclaveName(),
92 serviceEnvironmentConfig
.getKeyBackupConfig().getServiceId(),
93 serviceEnvironmentConfig
.getKeyBackupConfig().getMrenclave(),
95 final var fallbackKeyBackupServices
= serviceEnvironmentConfig
.getFallbackKeyBackupConfigs()
97 .map(config
-> accountManager
.getKeyBackupService(ServiceConfig
.getIasKeyStore(),
98 config
.getEnclaveName(),
99 config
.getServiceId(),
100 config
.getMrenclave(),
103 this.pinHelper
= new PinHelper(keyBackupService
, fallbackKeyBackupServices
);
107 public void register(
108 boolean voiceVerification
, String captcha
109 ) throws IOException
, CaptchaRequiredException
, NonNormalizedPhoneNumberException
, RateLimitException
{
110 if (account
.isRegistered()
111 && account
.getServiceEnvironment() != null
112 && account
.getServiceEnvironment() != serviceEnvironmentConfig
.getType()) {
113 throw new IOException("Account is registered in another environment: " + account
.getServiceEnvironment());
116 if (account
.getAci() != null && attemptReactivateAccount()) {
120 String sessionId
= NumberVerificationUtils
.handleVerificationSession(accountManager
,
121 account
.getSessionId(account
.getNumber()),
122 id
-> account
.setSessionId(account
.getNumber(), id
),
125 NumberVerificationUtils
.requestVerificationCode(accountManager
, sessionId
, voiceVerification
);
129 public void verifyAccount(
130 String verificationCode
, String pin
131 ) throws IOException
, PinLockedException
, IncorrectPinException
{
132 var sessionId
= account
.getSessionId(account
.getNumber());
133 final var result
= NumberVerificationUtils
.verifyNumber(sessionId
,
137 this::verifyAccountWithCode
);
138 final var response
= result
.first();
139 final var masterKey
= result
.second();
140 if (masterKey
== null) {
144 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
145 final var aci
= ACI
.parseOrNull(response
.getUuid());
146 final var pni
= PNI
.parseOrNull(response
.getPni());
147 account
.finishRegistration(aci
, pni
, masterKey
, pin
);
148 accountFileUpdater
.updateAccountIdentifiers(account
.getNumber(), aci
);
150 ManagerImpl m
= null;
152 m
= new ManagerImpl(account
, pathConfig
, accountFileUpdater
, serviceEnvironmentConfig
, userAgent
);
156 if (response
.isStorageCapable()) {
157 m
.retrieveRemoteStorage();
159 // Set an initial empty profile so user can be added to groups
161 m
.updateProfile(UpdateProfile
.newBuilder().build());
162 } catch (NoClassDefFoundError e
) {
163 logger
.warn("Failed to set default profile: {}", e
.getMessage());
166 if (newManagerListener
!= null) {
167 newManagerListener
.accept(m
);
178 public void deleteLocalAccountData() throws IOException
{
179 account
.deleteAccountData();
180 accountFileUpdater
.removeAccount();
185 public boolean isRegistered() {
186 return account
.isRegistered();
189 private boolean attemptReactivateAccount() {
191 final var accountManager
= new SignalServiceAccountManager(serviceEnvironmentConfig
.getSignalServiceConfiguration(),
192 account
.getCredentialsProvider(),
195 ServiceConfig
.AUTOMATIC_NETWORK_RETRY
);
196 accountManager
.setAccountAttributes(account
.getAccountAttributes(null));
197 account
.setRegistered(true);
198 logger
.info("Reactivated existing account, verify is not necessary.");
199 if (newManagerListener
!= null) {
200 final var m
= new ManagerImpl(account
,
203 serviceEnvironmentConfig
,
206 newManagerListener
.accept(m
);
209 } catch (IOException e
) {
210 logger
.debug("Failed to reactivate account");
215 private VerifyAccountResponse
verifyAccountWithCode(
216 final String sessionId
, final String verificationCode
, final String registrationLock
217 ) throws IOException
{
219 Utils
.handleResponseException(accountManager
.verifyAccount(verificationCode
, sessionId
));
220 } catch (AlreadyVerifiedException e
) {
221 // Already verified so can continue registering
223 return Utils
.handleResponseException(accountManager
.registerAccount(sessionId
,
225 account
.getAccountAttributes(registrationLock
),
230 public void close() {
231 if (account
!= null) {