MSC2140 Add hash configuration.

This commit is contained in:
Anatoly Sablin
2019-11-06 00:20:39 +03:00
parent 14ad4435bc
commit 8d346037b7
4 changed files with 42 additions and 14 deletions

View File

@@ -2,6 +2,7 @@ package io.kamax.mxisd.http.undertow.handler.identity.v2;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.kamax.mxisd.config.HashingConfig;
import io.kamax.mxisd.hash.HashManager;
import io.kamax.mxisd.http.undertow.handler.BasicHttpHandler;
import io.undertow.server.HttpServerExchange;
@@ -11,7 +12,6 @@ public class HashDetailsHandler extends BasicHttpHandler {
public static final String PATH = "/_matrix/identity/v2/hash_details";
private final HashManager hashManager;
private volatile JsonObject response = null;
public HashDetailsHandler(HashManager hashManager) {
this.hashManager = hashManager;
@@ -23,20 +23,16 @@ public class HashDetailsHandler extends BasicHttpHandler {
}
private JsonObject getResponse() {
if (response == null) {
synchronized (this) {
if (response == null) {
response = new JsonObject();
response.addProperty("lookup_pepper", hashManager.getHashEngine().getPepper());
JsonArray algorithms = new JsonArray();
algorithms.add("none");
if (hashManager.getConfig().isEnabled()) {
algorithms.add("sha256");
}
response.add("algorithms", algorithms);
}
JsonObject response = new JsonObject();
response.addProperty("lookup_pepper", hashManager.getHashEngine().getPepper());
JsonArray algorithms = new JsonArray();
HashingConfig config = hashManager.getConfig();
if (config.isEnabled()) {
for (HashingConfig.Algorithm algorithm : config.getAlgorithms()) {
algorithms.add(algorithm.name().toLowerCase());
}
}
response.add("algorithms", algorithms);
return response;
}
}

View File

@@ -18,8 +18,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.kamax.mxisd.http.undertow.handler.identity.v1;
package io.kamax.mxisd.http.undertow.handler.identity.v2;
import io.kamax.mxisd.config.HashingConfig;
import io.kamax.mxisd.exception.InvalidParamException;
import io.kamax.mxisd.exception.InvalidPepperException;
import io.kamax.mxisd.hash.HashManager;
@@ -51,6 +52,7 @@ public class HashLookupHandler extends LookupHandler implements ApiHandler {
public HashLookupHandler(LookupStrategy strategy, HashManager hashManager) {
this.strategy = strategy;
this.hashManager = hashManager;
}
@Override
@@ -61,6 +63,10 @@ public class HashLookupHandler extends LookupHandler implements ApiHandler {
log.info("Got bulk lookup request from {} with client {} - Is recursive? {}",
lookupRequest.getRequester(), lookupRequest.getUserAgent(), lookupRequest.isRecursive());
if (!hashManager.getConfig().isEnabled()) {
throw new InvalidParamException();
}
if (!hashManager.getHashEngine().getPepper().equals(input.getPepper())) {
throw new InvalidPepperException();
}
@@ -78,6 +84,10 @@ public class HashLookupHandler extends LookupHandler implements ApiHandler {
}
private void noneAlgorithm(HttpServerExchange exchange, HashLookupRequest request, ClientHashLookupRequest input) throws Exception {
if (!hashManager.getConfig().getAlgorithms().contains(HashingConfig.Algorithm.NONE)) {
throw new InvalidParamException();
}
BulkLookupRequest bulkLookupRequest = new BulkLookupRequest();
List<ThreePidMapping> mappings = new ArrayList<>();
for (String address : input.getAddresses()) {
@@ -100,6 +110,10 @@ public class HashLookupHandler extends LookupHandler implements ApiHandler {
}
private void sha256Algorithm(HttpServerExchange exchange, HashLookupRequest request, ClientHashLookupRequest input) {
if (!hashManager.getConfig().getAlgorithms().contains(HashingConfig.Algorithm.SHA256)) {
throw new InvalidParamException();
}
ClientHashLookupAnswer answer = new ClientHashLookupAnswer();
for (Pair<String, ThreePidMapping> pair : hashManager.getHashStorage().find(request.getHashes())) {
answer.getMappings().put(pair.getKey(), pair.getValue().getMxid());