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

Commit

Permalink
Use the hashed v2 lookup API for 3PIDs (element-hq/riot-android#3257)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarty authored and BillCarsonFr committed Sep 24, 2019
1 parent e0e4be4 commit 97f815e
Show file tree
Hide file tree
Showing 20 changed files with 738 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,10 @@ public Builder withHomeServerUri(final Uri homeServerUri) {
* @return this builder
*/
public Builder withIdentityServerUri(@Nullable final Uri identityServerUri) {
if ((null != identityServerUri) && (!"http".equals(identityServerUri.getScheme()) && !"https".equals(identityServerUri.getScheme()))) {
if (identityServerUri != null
&& !identityServerUri.toString().isEmpty()
&& !"http".equals(identityServerUri.getScheme())
&& !"https".equals(identityServerUri.getScheme())) {
throw new RuntimeException("Invalid identity server URI: " + identityServerUri);
}

Expand All @@ -393,7 +396,11 @@ public Builder withIdentityServerUri(@Nullable final Uri identityServerUri) {
throw new RuntimeException("Invalid identity server URI: " + identityServerUri);
}
} else {
mHomeServerConnectionConfig.mIdentityServerUri = identityServerUri;
if (identityServerUri != null && identityServerUri.toString().isEmpty()) {
mHomeServerConnectionConfig.mIdentityServerUri = null;
} else {
mHomeServerConnectionConfig.mIdentityServerUri = identityServerUri;
}
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,28 @@ fun convertFromUTF8(s: String): String? {

return out
}


/**
* Base64 URL conversion methods
*/

fun base64UrlToBase64(base64Url: String?): String? {
return base64Url
?.replace("-".toRegex(), "+")
?.replace("_".toRegex(), "/")
}

fun base64ToBase64Url(base64: String?): String? {
return base64
?.replace("\n".toRegex(), "")
?.replace("\\+".toRegex(), "-")
?.replace("/".toRegex(), "_")
?.replace("=".toRegex(), "")
}

fun base64ToUnpaddedBase64(base64: String?): String? {
return base64
?.replace("\n".toRegex(), "")
?.replace("=".toRegex(), "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.util.Base64;

import org.matrix.androidsdk.core.Log;
import org.matrix.androidsdk.core.StringUtilsKt;
import org.matrix.androidsdk.crypto.model.crypto.EncryptedFileInfo;
import org.matrix.androidsdk.crypto.model.crypto.EncryptedFileKey;

Expand Down Expand Up @@ -113,12 +114,12 @@ public static EncryptionResult encryptAttachment(InputStream attachmentStream, S
result.mEncryptedFileInfo.key.ext = true;
result.mEncryptedFileInfo.key.key_ops = Arrays.asList("encrypt", "decrypt");
result.mEncryptedFileInfo.key.kty = "oct";
result.mEncryptedFileInfo.key.k = base64ToBase64Url(Base64.encodeToString(key, Base64.DEFAULT));
result.mEncryptedFileInfo.key.k = StringUtilsKt.base64ToBase64Url(Base64.encodeToString(key, Base64.DEFAULT));
result.mEncryptedFileInfo.iv = Base64.encodeToString(initVectorBytes, Base64.DEFAULT).replace("\n", "").replace("=", "");
result.mEncryptedFileInfo.v = "v2";

result.mEncryptedFileInfo.hashes = new HashMap<>();
result.mEncryptedFileInfo.hashes.put("sha256", base64ToUnpaddedBase64(Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT)));
result.mEncryptedFileInfo.hashes.put("sha256", StringUtilsKt.base64ToUnpaddedBase64(Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT)));

result.mEncryptedStream = new ByteArrayInputStream(outStream.toByteArray());
outStream.close();
Expand Down Expand Up @@ -183,7 +184,7 @@ public static InputStream decryptAttachment(InputStream attachmentStream, Encryp
ByteArrayOutputStream outStream = new ByteArrayOutputStream();

try {
byte[] key = Base64.decode(base64UrlToBase64(encryptedFileInfo.key.k), Base64.DEFAULT);
byte[] key = Base64.decode(StringUtilsKt.base64UrlToBase64(encryptedFileInfo.key.k), Base64.DEFAULT);
byte[] initVectorBytes = Base64.decode(encryptedFileInfo.iv, Base64.DEFAULT);

Cipher decryptCipher = Cipher.getInstance(CIPHER_ALGORITHM);
Expand All @@ -207,7 +208,7 @@ public static InputStream decryptAttachment(InputStream attachmentStream, Encryp
decodedBytes = decryptCipher.doFinal();
outStream.write(decodedBytes);

String currentDigestValue = base64ToUnpaddedBase64(Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT));
String currentDigestValue = StringUtilsKt.base64ToUnpaddedBase64(Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT));

if (!TextUtils.equals(encryptedFileInfo.hashes.get("sha256"), currentDigestValue)) {
Log.e(LOG_TAG, "## decryptAttachment() : Digest value mismatch");
Expand Down Expand Up @@ -235,36 +236,4 @@ public static InputStream decryptAttachment(InputStream attachmentStream, Encryp

return null;
}

/**
* Base64 URL conversion methods
*/

private static String base64UrlToBase64(String base64Url) {
if (null != base64Url) {
base64Url = base64Url.replaceAll("-", "+");
base64Url = base64Url.replaceAll("_", "/");
}

return base64Url;
}

private static String base64ToBase64Url(String base64) {
if (null != base64) {
base64 = base64.replaceAll("\n", "");
base64 = base64.replaceAll("\\+", "-");
base64 = base64.replaceAll("/", "_");
base64 = base64.replaceAll("=", "");
}
return base64;
}

private static String base64ToUnpaddedBase64(String base64) {
if (null != base64) {
base64 = base64.replaceAll("\n", "");
base64 = base64.replaceAll("=", "");
}

return base64;
}
}
18 changes: 0 additions & 18 deletions matrix-sdk/src/main/java/org/matrix/androidsdk/MXDataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import org.matrix.androidsdk.rest.client.PresenceRestClient;
import org.matrix.androidsdk.rest.client.ProfileRestClient;
import org.matrix.androidsdk.rest.client.RoomsRestClient;
import org.matrix.androidsdk.rest.client.ThirdPidRestClient;
import org.matrix.androidsdk.rest.model.ChunkEvents;
import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.rest.model.ReceiptData;
Expand Down Expand Up @@ -132,7 +131,6 @@ public interface RequestNetworkErrorListener {

private ProfileRestClient mProfileRestClient;
private PresenceRestClient mPresenceRestClient;
private ThirdPidRestClient mThirdPidRestClient;
private RoomsRestClient mRoomsRestClient;
private EventsRestClient mEventsRestClient;
private AccountDataRestClient mAccountDataRestClient;
Expand Down Expand Up @@ -270,22 +268,6 @@ public PresenceRestClient getPresenceRestClient() {
return mPresenceRestClient;
}

/**
* Update the thirdPid Rest client.
*
* @param thirdPidRestClient the REST client
*/
public void setThirdPidRestClient(ThirdPidRestClient thirdPidRestClient) {
mThirdPidRestClient = thirdPidRestClient;
}

/**
* @return the ThirdPid REST client
*/
public ThirdPidRestClient getThirdPidRestClient() {
return mThirdPidRestClient;
}

/**
* Update the rooms Rest client.
*
Expand Down
32 changes: 10 additions & 22 deletions matrix-sdk/src/main/java/org/matrix/androidsdk/MXSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.matrix.androidsdk.data.store.MXStoreListener;
import org.matrix.androidsdk.db.MXLatestChatMessageCache;
import org.matrix.androidsdk.db.MXMediaCache;
import org.matrix.androidsdk.features.identityserver.IdentityServerManager;
import org.matrix.androidsdk.features.identityserver.IdentityServerNotConfiguredException;
import org.matrix.androidsdk.features.terms.TermsManager;
import org.matrix.androidsdk.groups.GroupsManager;
Expand All @@ -84,7 +85,6 @@
import org.matrix.androidsdk.rest.client.PushRulesRestClient;
import org.matrix.androidsdk.rest.client.PushersRestClient;
import org.matrix.androidsdk.rest.client.RoomsRestClient;
import org.matrix.androidsdk.rest.client.ThirdPidRestClient;
import org.matrix.androidsdk.rest.model.CreateRoomParams;
import org.matrix.androidsdk.rest.model.CreateRoomResponse;
import org.matrix.androidsdk.rest.model.Event;
Expand Down Expand Up @@ -146,7 +146,6 @@ public class MXSession implements CryptoSession {
private RoomsRestClient mRoomsRestClient;
private final PushRulesRestClient mPushRulesRestClient;
private PushersRestClient mPushersRestClient;
private final ThirdPidRestClient mThirdPidRestClient;
private final CallRestClient mCallRestClient;
private final AccountDataRestClient mAccountDataRestClient;
private final OpenIdRestClient mOpenIdRestClient;
Expand Down Expand Up @@ -174,6 +173,8 @@ public class MXSession implements CryptoSession {

private TermsManager termsManager;

private IdentityServerManager mIdentityServerManager;

private boolean mIsAliveSession = true;

// online status
Expand Down Expand Up @@ -235,7 +236,6 @@ private MXSession(HomeServerConnectionConfig hsConfig,
mPushersRestClient = new PushersRestClient(hsConfig);
}

mThirdPidRestClient = new ThirdPidRestClient(hsConfig);
mCallRestClient = new CallRestClient(hsConfig);
mAccountDataRestClient = new AccountDataRestClient(hsConfig);
mOpenIdRestClient = new OpenIdRestClient(hsConfig);
Expand Down Expand Up @@ -329,7 +329,6 @@ public void onReadReceiptsLoaded(final String roomId) {
mDataHandler.setDataRetriever(mDataRetriever);
mDataHandler.setProfileRestClient(mProfileRestClient);
mDataHandler.setPresenceRestClient(mPresenceRestClient);
mDataHandler.setThirdPidRestClient(mThirdPidRestClient);
mDataHandler.setRoomsRestClient(mRoomsRestClient);
mDataHandler.setEventsRestClient(mEventsRestClient);
mDataHandler.setAccountDataRestClient(mAccountDataRestClient);
Expand Down Expand Up @@ -357,7 +356,6 @@ public void onReadReceiptsLoaded(final String roomId) {
mPresenceRestClient.setUnsentEventsManager(mUnsentEventsManager);
mRoomsRestClient.setUnsentEventsManager(mUnsentEventsManager);
mPushRulesRestClient.setUnsentEventsManager(mUnsentEventsManager);
mThirdPidRestClient.setUnsentEventsManager(mUnsentEventsManager);
mCallRestClient.setUnsentEventsManager(mUnsentEventsManager);
mAccountDataRestClient.setUnsentEventsManager(mUnsentEventsManager);
mLoginRestClient.setUnsentEventsManager(mUnsentEventsManager);
Expand All @@ -375,6 +373,8 @@ public void onReadReceiptsLoaded(final String roomId) {
mDataHandler.setGroupsManager(mGroupsManager);

termsManager = new TermsManager(this);

mIdentityServerManager = new IdentityServerManager(this, appContext);
}

private void checkIfAlive() {
Expand Down Expand Up @@ -555,9 +555,9 @@ public PushRulesRestClient getBingRulesApiClient() {
return mPushRulesRestClient;
}

public ThirdPidRestClient getThirdPidRestClient() {
public IdentityServerManager getIdentityServerManager() {
checkIfAlive();
return mThirdPidRestClient;
return mIdentityServerManager;
}

public CallRestClient getCallRestClient() {
Expand Down Expand Up @@ -1361,7 +1361,6 @@ public boolean createDirectMessageRoom(final String aParticipantUserId, final St
boolean retCode = false;

if (!TextUtils.isEmpty(aParticipantUserId)) {
retCode = true;
CreateRoomParams params = new CreateRoomParams();

params.addCryptoAlgorithm(algorithm);
Expand All @@ -1371,8 +1370,10 @@ public boolean createDirectMessageRoom(final String aParticipantUserId, final St

if (res) {
aCreateRoomCallBack.onUnexpectedError(new IdentityServerNotConfiguredException());
retCode = false;
} else {
createRoom(params, aCreateRoomCallBack);
retCode = true;
}
}

Expand Down Expand Up @@ -1557,19 +1558,6 @@ public void run() {
}
}

/**
* Retrieve user matrix id from a 3rd party id.
*
* @param address the user id.
* @param media the media.
* @param callback the 3rd party callback
*/
public void lookup3Pid(String address, String media, final ApiCallback<String> callback) {
checkIfAlive();

mThirdPidRestClient.lookup3Pid(address, media, callback);
}

/**
* Retrieve user matrix id from a 3rd party id.
*
Expand All @@ -1580,7 +1568,7 @@ public void lookup3Pid(String address, String media, final ApiCallback<String> c
public void lookup3Pids(List<String> addresses, List<String> mediums, ApiCallback<List<String>> callback) {
checkIfAlive();

mThirdPidRestClient.lookup3Pids(addresses, mediums, callback);
mIdentityServerManager.lookup3Pids(addresses, mediums, callback);
}

/**
Expand Down
Loading

0 comments on commit 97f815e

Please sign in to comment.