Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
#3300 MSC2290
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr committed Oct 1, 2019
1 parent dab4220 commit ad9b90e
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 326 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Features:
- Privacy: Remove the ability to set an IS at login/registration (#3264)
- Privacy: Allow password reset when no IS (#3261)
- Privacy: Allow email registration when no IS (#3260)
- Privacy: Separate Add and Bind for 3PID #3300

Improvements:
-
Expand Down
3 changes: 1 addition & 2 deletions vector/src/main/java/im/vector/LoginHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,9 @@ public void submitEmailTokenValidation(final Context aCtx,
final String aClientSecret,
final String aSid,
final ApiCallback<Boolean> aRespCallback) {
final ThreePid pid = new ThreePid(null, ThreePid.MEDIUM_EMAIL);
ThirdPidRestClient restClient = new ThirdPidRestClient(aHomeServerConfig);

pid.submitValidationToken(restClient, aToken, aClientSecret, aSid, new UnrecognizedCertApiCallback<Boolean>(aHomeServerConfig, aRespCallback) {
restClient.submitValidationToken(ThreePid.MEDIUM_EMAIL, aToken, aClientSecret, aSid, new UnrecognizedCertApiCallback<Boolean>(aHomeServerConfig, aRespCallback) {
@Override
public void onSuccess(Boolean info) {
aRespCallback.onSuccess(info);
Expand Down
137 changes: 79 additions & 58 deletions vector/src/main/java/im/vector/RegistrationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
import org.matrix.androidsdk.core.Log;
import org.matrix.androidsdk.core.callback.ApiCallback;
import org.matrix.androidsdk.core.model.MatrixError;
import org.matrix.androidsdk.features.identityserver.IdentityServerManager;
import org.matrix.androidsdk.login.RegistrationToolsKt;
import org.matrix.androidsdk.rest.client.LoginRestClient;
import org.matrix.androidsdk.rest.client.ProfileRestClient;
import org.matrix.androidsdk.rest.client.ThirdPidRestClient;
import org.matrix.androidsdk.rest.model.RequestEmailValidationResponse;
import org.matrix.androidsdk.rest.model.RequestPhoneNumberValidationResponse;
import org.matrix.androidsdk.rest.model.login.AuthParams;
import org.matrix.androidsdk.rest.model.login.AuthParamsCaptcha;
import org.matrix.androidsdk.rest.model.login.AuthParamsLoginPassword;
Expand Down Expand Up @@ -261,18 +264,18 @@ public void attemptRegistration(final Context context, final RegistrationListene
final String registrationType;
if (mRegistrationResponse != null && !TextUtils.isEmpty(mRegistrationResponse.session)) {
AuthParams authParams = null;
if (mPhoneNumber != null && !isCompleted(LoginRestClient.LOGIN_FLOW_TYPE_MSISDN) && !TextUtils.isEmpty(mPhoneNumber.sid)) {
if (mPhoneNumber != null && !isCompleted(LoginRestClient.LOGIN_FLOW_TYPE_MSISDN) && !TextUtils.isEmpty(mPhoneNumber.getSid())) {
Uri identityServerUri = mHsConfig.getIdentityServerUri();
if (identityServerUri == null) {
listener.onIdentityServerMissing();
return;
} else {
registrationType = LoginRestClient.LOGIN_FLOW_TYPE_MSISDN;
authParams = getThreePidAuthParams(mPhoneNumber.clientSecret, identityServerUri.getHost(),
mPhoneNumber.sid, LoginRestClient.LOGIN_FLOW_TYPE_MSISDN);
authParams = getThreePidAuthParams(mPhoneNumber.getClientSecret(), identityServerUri.getHost(),
mPhoneNumber.getSid(), LoginRestClient.LOGIN_FLOW_TYPE_MSISDN);
}
} else if (mEmail != null && !isCompleted(LoginRestClient.LOGIN_FLOW_TYPE_EMAIL_IDENTITY)) {
if (TextUtils.isEmpty(mEmail.sid)) {
if (TextUtils.isEmpty(mEmail.getSid())) {
// Email token needs to be requested before doing validation
Log.d(LOG_TAG, "attemptRegistration: request email validation");
requestValidationToken(context, mEmail, new ThreePidRequestListener() {
Expand All @@ -283,7 +286,7 @@ public void onIdentityServerMissing() {

@Override
public void onThreePidRequested(ThreePid pid) {
if (!TextUtils.isEmpty(pid.sid)) {
if (!TextUtils.isEmpty(pid.getSid())) {
// The session id for the email validation has just been received.
// We trigger here a new registration request without delay to attach the current username
// and the pwd to the registration session.
Expand All @@ -307,8 +310,8 @@ public void onThreePidRequestFailed(String errorMessage) {
return;
} else {
registrationType = LoginRestClient.LOGIN_FLOW_TYPE_EMAIL_IDENTITY;
authParams = getThreePidAuthParams(mEmail.clientSecret, identityServerUri.getHost(),
mEmail.sid, LoginRestClient.LOGIN_FLOW_TYPE_EMAIL_IDENTITY);
authParams = getThreePidAuthParams(mEmail.getClientSecret(), identityServerUri.getHost(),
mEmail.getSid(), LoginRestClient.LOGIN_FLOW_TYPE_EMAIL_IDENTITY);
}
}
} else if (!TextUtils.isEmpty(mCaptchaResponse) && !isCompleted(LoginRestClient.LOGIN_FLOW_TYPE_RECAPTCHA)) {
Expand Down Expand Up @@ -590,27 +593,31 @@ public boolean canSkipThreePid() {
*/
public void submitValidationToken(final String token, final ThreePid pid, final ThreePidValidationListener listener) {
if (getThirdPidRestClient() != null) {
pid.submitValidationToken(getThirdPidRestClient(), token, pid.clientSecret, pid.sid, new ApiCallback<Boolean>() {
@Override
public void onSuccess(Boolean isSuccess) {
listener.onThreePidValidated(isSuccess);
}
getThirdPidRestClient().submitValidationToken(
pid.getMedium(),
token,
pid.getClientSecret(),
pid.getSid(), new ApiCallback<Boolean>() {
@Override
public void onSuccess(Boolean isSuccess) {
listener.onThreePidValidated(isSuccess);
}

@Override
public void onNetworkError(Exception e) {
listener.onThreePidValidated(false);
}
@Override
public void onNetworkError(Exception e) {
listener.onThreePidValidated(false);
}

@Override
public void onMatrixError(MatrixError e) {
listener.onThreePidValidated(false);
}
@Override
public void onMatrixError(MatrixError e) {
listener.onThreePidValidated(false);
}

@Override
public void onUnexpectedError(Exception e) {
listener.onThreePidValidated(false);
}
});
@Override
public void onUnexpectedError(Exception e) {
listener.onThreePidValidated(false);
}
});
}
}

Expand Down Expand Up @@ -642,7 +649,7 @@ public ThreePid getEmailThreePid() {
* @param listener
*/
public void addPhoneNumberThreePid(final Context context, final String phoneNumber, final String countryCode, final ThreePidRequestListener listener) {
final ThreePid pid = new ThreePid(phoneNumber, countryCode, ThreePid.MEDIUM_MSISDN);
final ThreePid pid = ThreePid.Companion.fromPhoneNumber(phoneNumber, countryCode);
requestValidationToken(context, pid, listener);
}

Expand Down Expand Up @@ -908,19 +915,25 @@ private void doRequestValidationToken(final Context context,
final @Nullable String isUrl,
final ThreePidRequestListener listener) {
if (getThirdPidRestClient() != null) {
switch (pid.medium) {
switch (pid.getMedium()) {
case ThreePid.MEDIUM_EMAIL:
String nextLink = NEXTLINK_BASE_URL + "/#/register?client_secret=" + pid.clientSecret;
String nextLink = NEXTLINK_BASE_URL + "/#/register?client_secret=" + pid.getClientSecret();
nextLink += "&hs_url=" + mHsConfig.getHomeserverUri().toString();
if (isUrl != null) {
nextLink += "&is_url=" + isUrl;
}
nextLink += "&session_id=" + mRegistrationResponse.session;
pid.requestEmailValidationToken(
null,
getProfileRestClient(), nextLink, true, new ApiCallback<Void>() {

getProfileRestClient().requestEmailValidationToken(IdentityServerManager.Companion.removeProtocol(isUrl),
pid.getEmailAddress(),
pid.getClientSecret(),
pid.getSendAttempt(),
nextLink,
true,
new ApiCallback<RequestEmailValidationResponse>() {
@Override
public void onSuccess(Void aVoid) {
public void onSuccess(RequestEmailValidationResponse info) {
pid.setSid(info.sid);
listener.onThreePidRequested(pid);
}

Expand Down Expand Up @@ -949,36 +962,44 @@ public void onMatrixError(MatrixError e) {
});
break;
case ThreePid.MEDIUM_MSISDN:
pid.requestPhoneNumberValidationToken(null, getProfileRestClient(), true, new ApiCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
mPhoneNumber = pid;
listener.onThreePidRequested(pid);
}
getProfileRestClient().requestPhoneNumberValidationToken(
IdentityServerManager.Companion.removeProtocol(isUrl),
pid.getPhoneNumber(),
pid.getCountry(),
pid.getClientSecret(),
pid.getSendAttempt(),
true,
new ApiCallback<RequestPhoneNumberValidationResponse>() {
@Override
public void onSuccess(RequestPhoneNumberValidationResponse info) {
pid.setSid(info.sid);
mPhoneNumber = pid;
listener.onThreePidRequested(pid);
}

@Override
public void onNetworkError(final Exception e) {
warnAfterCertificateError(context, e, pid, listener);
}
@Override
public void onNetworkError(final Exception e) {
warnAfterCertificateError(context, e, pid, listener);
}

@Override
public void onUnexpectedError(Exception e) {
String errorMessage = build3PidErrorMessage(context, R.string.account_phone_number_error, e.getLocalizedMessage());
listener.onThreePidRequestFailed(errorMessage);
}
@Override
public void onUnexpectedError(Exception e) {
String errorMessage = build3PidErrorMessage(context, R.string.account_phone_number_error, e.getLocalizedMessage());
listener.onThreePidRequestFailed(errorMessage);
}

@Override
public void onMatrixError(MatrixError e) {
String errorMessage = null;
if (TextUtils.equals(MatrixError.THREEPID_IN_USE, e.errcode)) {
errorMessage = build3PidErrorMessage(context, R.string.account_phone_number_already_used_error, null);
} else {
errorMessage = build3PidErrorMessage(context, R.string.account_phone_number_error, e.mReason);
}
@Override
public void onMatrixError(MatrixError e) {
String errorMessage = null;
if (TextUtils.equals(MatrixError.THREEPID_IN_USE, e.errcode)) {
errorMessage = build3PidErrorMessage(context, R.string.account_phone_number_already_used_error, null);
} else {
errorMessage = build3PidErrorMessage(context, R.string.account_phone_number_error, e.mReason);
}

listener.onThreePidRequestFailed(errorMessage);
}
});
listener.onThreePidRequestFailed(errorMessage);
}
});
break;
}
}
Expand Down
17 changes: 8 additions & 9 deletions vector/src/main/java/im/vector/activity/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -1308,11 +1308,10 @@ public void onSuccess(ThreePid thirdPid) {
mMode = MODE_FORGOT_PASSWORD_WAITING_VALIDATION;
refreshDisplay(true);

mForgotPid = new ThreePidCredentials();
mForgotPid.clientSecret = thirdPid.getClientSecret();
mForgotPid.idServer = identityServerUri.getHost();
mForgotPid.sid = thirdPid.getSid();
}
mForgotPid = new ThreePidCredentials();
mForgotPid.clientSecret = thirdPid.getClientSecret();
mForgotPid.idServer = identityServerHost;
mForgotPid.sid = thirdPid.getSid();
}
}

Expand Down Expand Up @@ -2281,7 +2280,7 @@ private void restoreSavedData(@NonNull Bundle savedInstanceState) {
mUniversalLinkUri = savedInstanceState.getParcelable(VectorUniversalLinkReceiver.EXTRA_UNIVERSAL_LINK_URI);
}

mPendingEmailValidation = (ThreePid) savedInstanceState.getSerializable(SAVED_CREATION_EMAIL_THREEPID);
mPendingEmailValidation = savedInstanceState.getParcelable(SAVED_CREATION_EMAIL_THREEPID);
}

@Override
Expand All @@ -2302,7 +2301,7 @@ public void onSaveInstanceState(Bundle savedInstanceState) {
// Retrieve the current email three pid
ThreePid email3pid = mRegistrationManager.getEmailThreePid();
if (null != email3pid) {
savedInstanceState.putSerializable(SAVED_CREATION_EMAIL_THREEPID, email3pid);
savedInstanceState.putParcelable(SAVED_CREATION_EMAIL_THREEPID, email3pid);
}
}

Expand Down Expand Up @@ -2667,7 +2666,7 @@ void submitThreePids() {

if (!TextUtils.isEmpty(email)) {
// Communicate email to singleton (will be validated later on)
mRegistrationManager.addEmailThreePid(new ThreePid(email, ThreePid.MEDIUM_EMAIL));
mRegistrationManager.addEmailThreePid(ThreePid.Companion.fromEmail(email));
}

if (mRegistrationPhoneNumberHandler.getPhoneNumber() != null) {
Expand All @@ -2684,7 +2683,7 @@ public void onIdentityServerMissing() {
@Override
public void onThreePidRequested(ThreePid pid) {
enableLoadingScreen(false);
if (!TextUtils.isEmpty(pid.sid)) {
if (!TextUtils.isEmpty(pid.getSid())) {
onPhoneNumberSidReceived(pid);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,11 @@ private void addPhoneNumber(final Phonenumber.PhoneNumber phoneNumber) {
final String e164phone = PhoneNumberUtils.getE164format(phoneNumber);
// Extract from phone number object instead of using mCurrentRegionCode just in case
final String countryCode = PhoneNumberUtil.getInstance().getRegionCodeForCountryCode(phoneNumber.getCountryCode());
final ThreePid pid = new ThreePid(e164phone, countryCode, ThreePid.MEDIUM_MSISDN);
final ThreePid pid = ThreePid.Companion.fromPhoneNumber(e164phone, countryCode);

Uri identityServer = null;
if (mSession.getIdentityServerManager().getIdentityServerUrl() != null) {
identityServer = Uri.parse(mSession.getIdentityServerManager().getIdentityServerUrl());
}
mSession.getMyUser().requestPhoneNumberValidationToken(identityServer,pid, new ApiCallback<Void>() {
mSession.getIdentityServerManager().startAddSessionForPhoneNumber(pid,null, new ApiCallback<ThreePid>() {
@Override
public void onSuccess(Void info) {
public void onSuccess(ThreePid pid) {
hideWaitingView();
Intent intent = PhoneNumberVerificationActivity.getIntent(PhoneNumberAdditionActivity.this,
mSession.getCredentials().userId, pid);
Expand Down
Loading

0 comments on commit ad9b90e

Please sign in to comment.