From d0fd9fb9b0cebaef4cff411809eac46e87df5f40 Mon Sep 17 00:00:00 2001 From: Anatoly Sablin Date: Tue, 17 Sep 2019 23:14:02 +0300 Subject: [PATCH] Get domain from public url. --- .../kamax/mxisd/session/SessionManager.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/kamax/mxisd/session/SessionManager.java b/src/main/java/io/kamax/mxisd/session/SessionManager.java index 004cb58..482ce0f 100644 --- a/src/main/java/io/kamax/mxisd/session/SessionManager.java +++ b/src/main/java/io/kamax/mxisd/session/SessionManager.java @@ -57,6 +57,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.Calendar; @@ -237,12 +239,26 @@ public class SessionManager { notifMgr.sendForUnbind(tpid); } + private String getDomain(String publicUrl) { + URL url; + try { + url = new URL(publicUrl); + } catch (MalformedURLException e) { + log.error("Malformed public url, use as is"); + return publicUrl; + } + int port = url.getPort(); + return url.getHost() + (port != -1 ? ":" + url.getPort() : ""); + } + private void checkAuthorization(String auth, JsonObject reqData) { if (!auth.startsWith("X-Matrix ")) { throw new NotAllowedException("Wrong authorization header"); } - if (StringUtils.isBlank(cfg.getServer().getPublicUrl())) { + String domain = getDomain(cfg.getServer().getPublicUrl()); + + if (StringUtils.isBlank(domain)) { throw new NotAllowedException("Unable to verify request, missing `server.publicUrl` property"); } @@ -284,7 +300,7 @@ public class SessionManager { jsonObject.addProperty("method", "POST"); jsonObject.addProperty("uri", "/_matrix/identity/api/v1/3pid/unbind"); jsonObject.addProperty("origin", origin); - jsonObject.addProperty("destination_is", cfg.getServer().getPublicUrl()); + jsonObject.addProperty("destination_is", domain); jsonObject.add("content", reqData); String canonical = MatrixJson.encodeCanonical(jsonObject);