Search in sources :

Example 1 with IThreePidSession

use of io.kamax.mxisd.threepid.session.IThreePidSession in project mxisd by kamax-io.

the class SessionMananger method createRemote.

public IThreePidSession createRemote(String sid, String secret) {
    ThreePidSession session = getSessionIfValidated(sid, secret);
    log.info("Creating remote 3PID session for {} with local session [{}] to {}", session.getThreePid(), sid);
    boolean isLocal = isLocal(session.getThreePid());
    PolicySource policy = cfg.getPolicy().getValidation().forIf(isLocal);
    if (!policy.isEnabled() || !policy.toRemote()) {
        throw new NotAllowedException("Validating " + (isLocal ? "local" : "remote") + " 3PID is not allowed");
    }
    log.info("Remote 3PID is allowed by policy");
    List<String> servers = mxCfg.getIdentity().getServers(policy.getToRemote().getServer());
    if (servers.isEmpty()) {
        throw new FeatureNotAvailable("Remote 3PID sessions are enabled but server list is " + "misconstrued (invalid ID or empty list");
    }
    String is = servers.get(0);
    String url = IdentityServerUtils.findIsUrlForDomain(is).orElseThrow(() -> new InternalServerError(is + " could not be resolved to an Identity server"));
    log.info("Will use IS endpoint {}", url);
    String remoteSecret = session.isRemote() ? session.getRemoteSecret() : RandomStringUtils.randomAlphanumeric(16);
    JsonObject body = new JsonObject();
    body.addProperty("client_secret", remoteSecret);
    body.addProperty(session.getThreePid().getMedium(), session.getThreePid().getAddress());
    body.addProperty("send_attempt", session.increaseAndGetRemoteAttempt());
    if (ThreePidMedium.PhoneNumber.is(session.getThreePid().getMedium())) {
        try {
            Phonenumber.PhoneNumber msisdn = phoneUtil.parse("+" + session.getThreePid().getAddress(), null);
            String country = phoneUtil.getRegionCodeForNumber(msisdn).toUpperCase();
            body.addProperty("phone_number", phoneUtil.format(msisdn, PhoneNumberUtil.PhoneNumberFormat.NATIONAL));
            body.addProperty("country", country);
        } catch (NumberParseException e) {
            throw new InternalServerError(e);
        }
    } else {
        body.addProperty(session.getThreePid().getMedium(), session.getThreePid().getAddress());
    }
    log.info("Requesting remote session with attempt {}", session.getRemoteAttempt());
    HttpPost tokenReq = RestClientUtils.post(url + "/_matrix/identity/api/v1/validate/" + session.getThreePid().getMedium() + "/requestToken", body);
    try (CloseableHttpResponse response = client.execute(tokenReq)) {
        int status = response.getStatusLine().getStatusCode();
        if (status < 200 || status >= 300) {
            JsonObject obj = parser.parseOptional(response).orElseThrow(() -> new RemoteIdentityServerException("Status " + status));
            throw new RemoteIdentityServerException(obj.get("errcode").getAsString() + ": " + obj.get("error").getAsString());
        }
        RequestTokenResponse data = new GsonParser().parse(response, RequestTokenResponse.class);
        log.info("Remote Session ID: {}", data.getSid());
        session.setRemoteData(url, data.getSid(), remoteSecret, 1);
        storage.updateThreePidSession(session.getDao());
        log.info("Updated Session {} with remote data", sid);
        return session;
    } catch (IOException e) {
        log.warn("Failed to create remote session with {} for {}: {}", url, session.getThreePid(), e.getMessage());
        throw new RemoteIdentityServerException(e.getMessage());
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) JsonObject(com.google.gson.JsonObject) RequestTokenResponse(io.kamax.mxisd.controller.identity.v1.io.RequestTokenResponse) IOException(java.io.IOException) ThreePidSession(io.kamax.mxisd.threepid.session.ThreePidSession) IThreePidSession(io.kamax.mxisd.threepid.session.IThreePidSession) GsonParser(io.kamax.mxisd.util.GsonParser) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) NumberParseException(com.google.i18n.phonenumbers.NumberParseException) PolicySource(io.kamax.mxisd.config.SessionConfig.Policy.PolicyTemplate.PolicySource) Phonenumber(com.google.i18n.phonenumbers.Phonenumber)

Example 2 with IThreePidSession

use of io.kamax.mxisd.threepid.session.IThreePidSession in project mxisd by kamax-io.

the class RemoteSessionController method requestToken.

@RequestMapping(path = SESSION_REQUEST_TOKEN)
public String requestToken(HttpServletRequest request, @RequestParam String sid, @RequestParam("client_secret") String secret, Model model) {
    log.info("Request {}: {}", request.getMethod(), request.getRequestURL());
    IThreePidSession session = mgr.createRemote(sid, secret);
    model.addAttribute("checkLink", RemoteIdentityAPIv1.getSessionCheck(session.getId(), session.getSecret()));
    return viewCfg.getSession().getRemote().getOnRequest().getSuccess();
}
Also used : IThreePidSession(io.kamax.mxisd.threepid.session.IThreePidSession) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

IThreePidSession (io.kamax.mxisd.threepid.session.IThreePidSession)2 JsonObject (com.google.gson.JsonObject)1 NumberParseException (com.google.i18n.phonenumbers.NumberParseException)1 Phonenumber (com.google.i18n.phonenumbers.Phonenumber)1 PolicySource (io.kamax.mxisd.config.SessionConfig.Policy.PolicyTemplate.PolicySource)1 RequestTokenResponse (io.kamax.mxisd.controller.identity.v1.io.RequestTokenResponse)1 ThreePidSession (io.kamax.mxisd.threepid.session.ThreePidSession)1 GsonParser (io.kamax.mxisd.util.GsonParser)1 IOException (java.io.IOException)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1