1 package org
.asamk
.signal
.manager
.util
;
3 import org
.asamk
.signal
.manager
.api
.CaptchaRequiredException
;
4 import org
.asamk
.signal
.manager
.api
.IncorrectPinException
;
5 import org
.asamk
.signal
.manager
.api
.NonNormalizedPhoneNumberException
;
6 import org
.asamk
.signal
.manager
.api
.Pair
;
7 import org
.asamk
.signal
.manager
.api
.PinLockedException
;
8 import org
.asamk
.signal
.manager
.api
.RateLimitException
;
9 import org
.asamk
.signal
.manager
.api
.VerificationMethodNotAvailableException
;
10 import org
.asamk
.signal
.manager
.helper
.PinHelper
;
11 import org
.slf4j
.Logger
;
12 import org
.slf4j
.LoggerFactory
;
13 import org
.whispersystems
.signalservice
.api
.kbs
.MasterKey
;
14 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.NoSuchSessionException
;
15 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.NonSuccessfulResponseCodeException
;
16 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.PushChallengeRequiredException
;
17 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.TokenNotAcceptedException
;
18 import org
.whispersystems
.signalservice
.api
.registration
.RegistrationApi
;
19 import org
.whispersystems
.signalservice
.internal
.push
.LockedException
;
20 import org
.whispersystems
.signalservice
.internal
.push
.PushServiceSocket
.VerificationCodeTransport
;
21 import org
.whispersystems
.signalservice
.internal
.push
.RegistrationSessionMetadataResponse
;
22 import org
.whispersystems
.signalservice
.internal
.push
.VerifyAccountResponse
;
24 import java
.io
.IOException
;
25 import java
.util
.Locale
;
26 import java
.util
.function
.Consumer
;
28 public class NumberVerificationUtils
{
30 private static final Logger logger
= LoggerFactory
.getLogger(NumberVerificationUtils
.class);
32 public static String
handleVerificationSession(
33 RegistrationApi registrationApi
,
35 Consumer
<String
> sessionIdSaver
,
36 boolean voiceVerification
,
38 ) throws CaptchaRequiredException
, IOException
, RateLimitException
, VerificationMethodNotAvailableException
{
39 RegistrationSessionMetadataResponse sessionResponse
;
41 sessionResponse
= getValidSession(registrationApi
, sessionId
);
42 } catch (PushChallengeRequiredException
|
43 org
.whispersystems
.signalservice
.api
.push
.exceptions
.CaptchaRequiredException e
) {
44 if (captcha
!= null) {
45 sessionResponse
= submitCaptcha(registrationApi
, sessionId
, captcha
);
47 throw new CaptchaRequiredException("Captcha Required");
51 sessionId
= sessionResponse
.getBody().getId();
52 sessionIdSaver
.accept(sessionId
);
54 if (sessionResponse
.getBody().getVerified()) {
58 if (sessionResponse
.getBody().getAllowedToRequestCode()) {
62 final var nextAttempt
= voiceVerification
63 ? sessionResponse
.getBody().getNextCall()
64 : sessionResponse
.getBody().getNextSms();
65 if (nextAttempt
== null) {
66 throw new VerificationMethodNotAvailableException();
67 } else if (nextAttempt
> 0) {
68 final var timestamp
= sessionResponse
.getHeaders().getTimestamp() + nextAttempt
* 1000;
69 throw new RateLimitException(timestamp
);
72 final var nextVerificationAttempt
= sessionResponse
.getBody().getNextVerificationAttempt();
73 if (nextVerificationAttempt
!= null && nextVerificationAttempt
> 0) {
74 final var timestamp
= sessionResponse
.getHeaders().getTimestamp() + nextVerificationAttempt
* 1000;
75 throw new CaptchaRequiredException(timestamp
);
78 if (sessionResponse
.getBody().getRequestedInformation().contains("captcha")) {
79 if (captcha
!= null) {
80 sessionResponse
= submitCaptcha(registrationApi
, sessionId
, captcha
);
82 if (!sessionResponse
.getBody().getAllowedToRequestCode()) {
83 throw new CaptchaRequiredException("Captcha Required");
90 public static void requestVerificationCode(
91 RegistrationApi registrationApi
,
93 boolean voiceVerification
94 ) throws IOException
, CaptchaRequiredException
, NonNormalizedPhoneNumberException
{
95 final var locale
= Utils
.getDefaultLocale(Locale
.US
);
96 final var response
= registrationApi
.requestSmsVerificationCode(sessionId
,
99 voiceVerification ? VerificationCodeTransport
.VOICE
: VerificationCodeTransport
.SMS
);
101 Utils
.handleResponseException(response
);
102 } catch (org
.whispersystems
.signalservice
.api
.push
.exceptions
.CaptchaRequiredException e
) {
103 throw new CaptchaRequiredException(e
.getMessage(), e
);
104 } catch (org
.whispersystems
.signalservice
.api
.push
.exceptions
.NonNormalizedPhoneNumberException e
) {
105 throw new NonNormalizedPhoneNumberException("Phone number is not normalized ("
107 + "). Expected normalized: "
108 + e
.getNormalizedNumber(), e
);
112 public static Pair
<VerifyAccountResponse
, MasterKey
> verifyNumber(
114 String verificationCode
,
118 ) throws IOException
, PinLockedException
, IncorrectPinException
{
119 verificationCode
= verificationCode
.replace("-", "");
121 final var response
= verifier
.verify(sessionId
, verificationCode
, null);
123 return new Pair
<>(response
, null);
124 } catch (LockedException e
) {
126 throw new PinLockedException(e
.getTimeRemaining());
129 final var registrationLockData
= pinHelper
.getRegistrationLockData(pin
, e
);
130 if (registrationLockData
== null) {
134 var registrationLock
= registrationLockData
.getMasterKey().deriveRegistrationLock();
135 VerifyAccountResponse response
;
137 response
= verifier
.verify(sessionId
, verificationCode
, registrationLock
);
138 } catch (LockedException _e
) {
139 throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
142 return new Pair
<>(response
, registrationLockData
.getMasterKey());
146 private static RegistrationSessionMetadataResponse
validateSession(
147 final RegistrationApi registrationApi
,
148 final String sessionId
149 ) throws IOException
{
150 if (sessionId
== null || sessionId
.isEmpty()) {
151 throw new NoSuchSessionException();
153 return Utils
.handleResponseException(registrationApi
.getRegistrationSessionStatus(sessionId
));
156 private static RegistrationSessionMetadataResponse
requestValidSession(
157 final RegistrationApi registrationApi
158 ) throws IOException
{
159 return Utils
.handleResponseException(registrationApi
.createRegistrationSession(null, "", ""));
162 private static RegistrationSessionMetadataResponse
getValidSession(
163 final RegistrationApi registrationApi
,
164 final String sessionId
165 ) throws IOException
{
167 return validateSession(registrationApi
, sessionId
);
168 } catch (NoSuchSessionException e
) {
169 logger
.debug("No registration session, creating new one.");
170 return requestValidSession(registrationApi
);
174 private static RegistrationSessionMetadataResponse
submitCaptcha(
175 RegistrationApi registrationApi
,
178 ) throws IOException
, CaptchaRequiredException
{
179 captcha
= captcha
== null ?
null : captcha
.replace("signalcaptcha://", "");
181 return Utils
.handleResponseException(registrationApi
.submitCaptchaToken(sessionId
, captcha
));
182 } catch (PushChallengeRequiredException
|
183 org
.whispersystems
.signalservice
.api
.push
.exceptions
.CaptchaRequiredException
|
184 TokenNotAcceptedException _e
) {
185 throw new CaptchaRequiredException("Captcha not accepted");
186 } catch (NonSuccessfulResponseCodeException e
) {
188 throw new CaptchaRequiredException("Captcha has invalid format");
194 public interface Verifier
{
196 VerifyAccountResponse
verify(
198 String verificationCode
,
199 String registrationLock
200 ) throws IOException
;