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

Commit

Permalink
Identity server is now optional. Handle null cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarty committed Aug 30, 2019
1 parent 649b0e2 commit cf81e9d
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 176 deletions.
161 changes: 94 additions & 67 deletions vector/src/main/java/im/vector/RegistrationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package im.vector;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

Expand Down Expand Up @@ -261,14 +262,25 @@ public void attemptRegistration(final Context context, final RegistrationListene
if (mRegistrationResponse != null && !TextUtils.isEmpty(mRegistrationResponse.session)) {
AuthParams authParams = null;
if (mPhoneNumber != null && !isCompleted(LoginRestClient.LOGIN_FLOW_TYPE_MSISDN) && !TextUtils.isEmpty(mPhoneNumber.sid)) {
registrationType = LoginRestClient.LOGIN_FLOW_TYPE_MSISDN;
authParams = getThreePidAuthParams(mPhoneNumber.clientSecret, mHsConfig.getIdentityServerUri().getHost(),
mPhoneNumber.sid, LoginRestClient.LOGIN_FLOW_TYPE_MSISDN);
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);
}
} else if (mEmail != null && !isCompleted(LoginRestClient.LOGIN_FLOW_TYPE_EMAIL_IDENTITY)) {
if (TextUtils.isEmpty(mEmail.sid)) {
// Email token needs to be requested before doing validation
Log.d(LOG_TAG, "attemptRegistration: request email validation");
requestValidationToken(context, mEmail, new ThreePidRequestListener() {
@Override
public void onIdentityServerMissing() {
listener.onIdentityServerMissing();
}

@Override
public void onThreePidRequested(ThreePid pid) {
if (!TextUtils.isEmpty(pid.sid)) {
Expand All @@ -289,9 +301,15 @@ public void onThreePidRequestFailed(String errorMessage) {
});
return;
} else {
registrationType = LoginRestClient.LOGIN_FLOW_TYPE_EMAIL_IDENTITY;
authParams = getThreePidAuthParams(mEmail.clientSecret, mHsConfig.getIdentityServerUri().getHost(),
mEmail.sid, LoginRestClient.LOGIN_FLOW_TYPE_EMAIL_IDENTITY);
Uri identityServerUri = mHsConfig.getIdentityServerUri();
if (identityServerUri == null) {
listener.onIdentityServerMissing();
return;
} else {
registrationType = LoginRestClient.LOGIN_FLOW_TYPE_EMAIL_IDENTITY;
authParams = getThreePidAuthParams(mEmail.clientSecret, identityServerUri.getHost(),
mEmail.sid, LoginRestClient.LOGIN_FLOW_TYPE_EMAIL_IDENTITY);
}
}
} else if (!TextUtils.isEmpty(mCaptchaResponse) && !isCompleted(LoginRestClient.LOGIN_FLOW_TYPE_RECAPTCHA)) {
registrationType = LoginRestClient.LOGIN_FLOW_TYPE_RECAPTCHA;
Expand Down Expand Up @@ -712,7 +730,7 @@ private LoginRestClient getLoginRestClient() {
* @return third pid rest client
*/
private ThirdPidRestClient getThirdPidRestClient() {
if (mThirdPidRestClient == null && mHsConfig != null) {
if (mThirdPidRestClient == null && mHsConfig != null && mHsConfig.getIdentityServerUri() != null) {
mThirdPidRestClient = new ThirdPidRestClient(mHsConfig);
}
return mThirdPidRestClient;
Expand Down Expand Up @@ -852,75 +870,80 @@ private AuthParams getCaptchaAuthParams(final String captchaResponse) {
* @param listener
*/
private void requestValidationToken(final Context context, final ThreePid pid, final ThreePidRequestListener listener) {
if (getThirdPidRestClient() != null) {
switch (pid.medium) {
case ThreePid.MEDIUM_EMAIL:
String nextLink = NEXTLINK_BASE_URL + "/#/register?client_secret=" + pid.clientSecret;
nextLink += "&hs_url=" + mHsConfig.getHomeserverUri().toString();
nextLink += "&is_url=" + mHsConfig.getIdentityServerUri().toString();
nextLink += "&session_id=" + mRegistrationResponse.session;
pid.requestEmailValidationToken(getProfileRestClient(), nextLink, true, new ApiCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
listener.onThreePidRequested(pid);
}

@Override
public void onNetworkError(final Exception e) {
warnAfterCertificateError(context, e, pid, listener);
}
Uri identityServerUri = mHsConfig.getIdentityServerUri();
if (identityServerUri == null) {
listener.onIdentityServerMissing();
} else {
if (getThirdPidRestClient() != null) {
switch (pid.medium) {
case ThreePid.MEDIUM_EMAIL:
String nextLink = NEXTLINK_BASE_URL + "/#/register?client_secret=" + pid.clientSecret;
nextLink += "&hs_url=" + mHsConfig.getHomeserverUri().toString();
nextLink += "&is_url=" + identityServerUri.toString();
nextLink += "&session_id=" + mRegistrationResponse.session;
pid.requestEmailValidationToken(getProfileRestClient(), nextLink, true, new ApiCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
listener.onThreePidRequested(pid);
}

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

@Override
public void onMatrixError(MatrixError e) {
String errorMessage = null;
if (TextUtils.equals(MatrixError.THREEPID_IN_USE, e.errcode)) {
errorMessage = build3PidErrorMessage(context, R.string.account_email_already_used_error, null);
} else {
errorMessage = build3PidErrorMessage(context, R.string.account_email_error, e.getLocalizedMessage());
@Override
public void onUnexpectedError(Exception e) {
String errorMessage = build3PidErrorMessage(context, R.string.account_email_error, e.getLocalizedMessage());
listener.onThreePidRequestFailed(errorMessage);
}

listener.onThreePidRequestFailed(errorMessage);
}
});
break;
case ThreePid.MEDIUM_MSISDN:
pid.requestPhoneNumberValidationToken(getProfileRestClient(), true, new ApiCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
mPhoneNumber = pid;
listener.onThreePidRequested(pid);
}
@Override
public void onMatrixError(MatrixError e) {
String errorMessage = null;
if (TextUtils.equals(MatrixError.THREEPID_IN_USE, e.errcode)) {
errorMessage = build3PidErrorMessage(context, R.string.account_email_already_used_error, null);
} else {
errorMessage = build3PidErrorMessage(context, R.string.account_email_error, e.getLocalizedMessage());
}

@Override
public void onNetworkError(final Exception e) {
warnAfterCertificateError(context, e, pid, listener);
}
listener.onThreePidRequestFailed(errorMessage);
}
});
break;
case ThreePid.MEDIUM_MSISDN:
pid.requestPhoneNumberValidationToken(getProfileRestClient(), true, new ApiCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
mPhoneNumber = pid;
listener.onThreePidRequested(pid);
}

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

@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 onUnexpectedError(Exception e) {
String errorMessage = build3PidErrorMessage(context, R.string.account_phone_number_error, e.getLocalizedMessage());
listener.onThreePidRequestFailed(errorMessage);
}

listener.onThreePidRequestFailed(errorMessage);
}
});
break;
@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);
}
});
break;
}
}
}
}
Expand Down Expand Up @@ -1094,6 +1117,8 @@ private interface InternalRegistrationListener {
*/

public interface ThreePidRequestListener {
void onIdentityServerMissing();

void onThreePidRequested(ThreePid pid);

void onThreePidRequestFailed(String errorMessage);
Expand All @@ -1114,6 +1139,8 @@ public interface RegistrationListener {

void onWaitingEmailValidation();

void onIdentityServerMissing();

/**
* @param publicKey the Captcha public key
*/
Expand Down
8 changes: 7 additions & 1 deletion vector/src/main/java/im/vector/VectorApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
Expand Down Expand Up @@ -862,7 +863,12 @@ private void visitSessionVariables() {
final MXSession session = Matrix.getInstance(this).getDefaultSession();
if (session != null) {
mAppAnalytics.visitVariable(7, "Homeserver URL", session.getHomeServerConfig().getHomeserverUri().toString());
mAppAnalytics.visitVariable(8, "Identity Server URL", session.getHomeServerConfig().getIdentityServerUri().toString());
Uri identityServerUri = session.getHomeServerConfig().getIdentityServerUri();
if (identityServerUri == null) {
mAppAnalytics.visitVariable(8, "Identity Server URL", "");
} else {
mAppAnalytics.visitVariable(8, "Identity Server URL", identityServerUri.toString());
}
}
}

Expand Down
Loading

0 comments on commit cf81e9d

Please sign in to comment.