Search in sources :

Example 1 with RemoteHomeServerException

use of io.kamax.mxisd.exception.RemoteHomeServerException in project mxisd by kamax-io.

the class RoomInviteHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) {
    String accessToken = getAccessToken(exchange);
    String whoamiUri = dns.transform(URI.create(exchange.getRequestURL()).resolve(URI.create("/_matrix/client/r0/account/whoami"))).toString();
    log.info("Who Am I URL: {}", whoamiUri);
    HttpGet whoAmIReq = new HttpGet(whoamiUri);
    whoAmIReq.addHeader("Authorization", "Bearer " + accessToken);
    _MatrixID uId;
    try (CloseableHttpResponse whoAmIRes = client.execute(whoAmIReq)) {
        int sc = whoAmIRes.getStatusLine().getStatusCode();
        String body = EntityUtils.toString(whoAmIRes.getEntity());
        if (sc != 200) {
            log.warn("Unable to get caller identity from Homeserver - Status code: {}", sc);
            log.debug("Body: {}", body);
            throw new RemoteHomeServerException(body);
        }
        JsonObject json = GsonUtil.parseObj(body);
        Optional<String> uIdRaw = GsonUtil.findString(json, "user_id");
        if (!uIdRaw.isPresent()) {
            throw new RemoteHomeServerException("No User ID provided when checking identity");
        }
        uId = MatrixID.asAcceptable(uIdRaw.get());
    } catch (IOException e) {
        InternalServerError ex = new InternalServerError(e);
        log.error("Ref {}: Unable to fetch caller identity from Homeserver", ex.getReference());
        throw ex;
    }
    log.info("Processing room invite from {}", uId.getId());
    JsonObject reqBody = parseJsonObject(exchange);
    if (!invMgr.canInvite(uId, reqBody)) {
        throw new NotAllowedException("Your account is not allowed to invite that address");
    }
    log.info("Invite was allowing, relaying to the Homeserver");
    proxyPost(exchange, reqBody, client, dns);
}
Also used : RemoteHomeServerException(io.kamax.mxisd.exception.RemoteHomeServerException) NotAllowedException(io.kamax.mxisd.exception.NotAllowedException) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID) InternalServerError(io.kamax.mxisd.exception.InternalServerError)

Example 2 with RemoteHomeServerException

use of io.kamax.mxisd.exception.RemoteHomeServerException in project mxisd by kamax-io.

the class RegistrationManager method execute.

public RegistrationReply execute(URI target, JsonObject request) {
    HttpPost registerProxyRq = RestClientUtils.post(resolveProxyUrl(target), GsonUtil.get(), request);
    try (CloseableHttpResponse response = client.execute(registerProxyRq)) {
        int status = response.getStatusLine().getStatusCode();
        if (status == 200) {
            // The user managed to register. We check if it had a session
            String sessionId = GsonUtil.findObj(request, "auth").flatMap(auth -> GsonUtil.findString(auth, "session")).orElse("");
            if (StringUtils.isEmpty(sessionId)) {
                // No session ID was provided. This is an edge case we do not support for now as investigation is needed
                // to ensure how and when this happens.
                HttpPost newSessReq = RestClientUtils.post(resolveProxyUrl(target), GsonUtil.get(), new JsonObject());
                try (CloseableHttpResponse newSessRes = client.execute(newSessReq)) {
                    RegistrationReply reply = new RegistrationReply();
                    reply.setStatus(newSessRes.getStatusLine().getStatusCode());
                    reply.setBody(GsonUtil.parseObj(EntityUtils.toString(newSessRes.getEntity())));
                    return reply;
                }
            }
        }
        throw new NotImplementedException("Registration");
    } catch (IOException e) {
        throw new RemoteHomeServerException(e.getMessage());
    }
}
Also used : JsonObject(com.google.gson.JsonObject) HttpPost(org.apache.http.client.methods.HttpPost) GsonUtil(io.kamax.matrix.json.GsonUtil) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Logger(org.slf4j.Logger) URIBuilder(org.apache.http.client.utils.URIBuilder) LoggerFactory(org.slf4j.LoggerFactory) RegisterConfig(io.kamax.mxisd.config.RegisterConfig) IOException(java.io.IOException) StringUtils(org.apache.commons.lang3.StringUtils) NotImplementedException(io.kamax.mxisd.exception.NotImplementedException) ThreePid(io.kamax.matrix.ThreePid) EntityUtils(org.apache.http.util.EntityUtils) RestClientUtils(io.kamax.mxisd.util.RestClientUtils) Objects(java.util.Objects) Matcher(java.util.regex.Matcher) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InvitationManager(io.kamax.mxisd.invitation.InvitationManager) URI(java.net.URI) Pattern(java.util.regex.Pattern) ClientDnsOverwrite(io.kamax.mxisd.dns.ClientDnsOverwrite) RemoteHomeServerException(io.kamax.mxisd.exception.RemoteHomeServerException) HttpPost(org.apache.http.client.methods.HttpPost) RemoteHomeServerException(io.kamax.mxisd.exception.RemoteHomeServerException) NotImplementedException(io.kamax.mxisd.exception.NotImplementedException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException)

Aggregations

JsonObject (com.google.gson.JsonObject)2 RemoteHomeServerException (io.kamax.mxisd.exception.RemoteHomeServerException)2 IOException (java.io.IOException)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 ThreePid (io.kamax.matrix.ThreePid)1 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)1 GsonUtil (io.kamax.matrix.json.GsonUtil)1 RegisterConfig (io.kamax.mxisd.config.RegisterConfig)1 ClientDnsOverwrite (io.kamax.mxisd.dns.ClientDnsOverwrite)1 InternalServerError (io.kamax.mxisd.exception.InternalServerError)1 NotAllowedException (io.kamax.mxisd.exception.NotAllowedException)1 NotImplementedException (io.kamax.mxisd.exception.NotImplementedException)1 InvitationManager (io.kamax.mxisd.invitation.InvitationManager)1 RestClientUtils (io.kamax.mxisd.util.RestClientUtils)1 URI (java.net.URI)1 Objects (java.util.Objects)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 StringUtils (org.apache.commons.lang3.StringUtils)1 HttpGet (org.apache.http.client.methods.HttpGet)1