Search in sources :

Example 6 with ThreePid

use of io.kamax.matrix.ThreePid in project mxisd by kamax-io.

the class WordpressAuthProvider method authenticate.

@Override
public BackendAuthResult authenticate(_MatrixID mxid, String password) {
    try {
        WordpressAuthData data = wordpress.authenticate(mxid.getLocalPart(), password);
        BackendAuthResult result = new BackendAuthResult();
        if (StringUtils.isNotBlank(data.getUserEmail())) {
            result.withThreePid(new ThreePid("email", data.getUserEmail()));
        }
        result.succeed(mxid.getId(), UserIdType.MatrixID.getId(), data.getUserDisplayName());
        return result;
    } catch (IllegalArgumentException e) {
        log.error("Authentication failed for {}: {}", mxid.getId(), e.getMessage());
        return BackendAuthResult.failure();
    }
}
Also used : BackendAuthResult(io.kamax.mxisd.auth.provider.BackendAuthResult) ThreePid(io.kamax.matrix.ThreePid)

Example 7 with ThreePid

use of io.kamax.matrix.ThreePid in project mxisd by kamax-io.

the class MemoryIdentityStore method find.

@Override
public Optional<SingleLookupReply> find(SingleLookupRequest request) {
    logger.info("Performing lookup {} of type {}", request.getThreePid(), request.getType());
    ThreePid req = new ThreePid(request.getType(), request.getThreePid());
    for (MemoryIdentityConfig id : cfg.getIdentities()) {
        for (MemoryThreePid threepid : id.getThreepids()) {
            if (req.equals(new ThreePid(threepid.getMedium(), threepid.getAddress()))) {
                return Optional.of(new SingleLookupReply(request, new MatrixID(id.getUsername(), mxCfg.getDomain())));
            }
        }
    }
    return Optional.empty();
}
Also used : SingleLookupReply(io.kamax.mxisd.lookup.SingleLookupReply) MemoryIdentityConfig(io.kamax.mxisd.config.memory.MemoryIdentityConfig) io.kamax.matrix._ThreePid(io.kamax.matrix._ThreePid) ThreePid(io.kamax.matrix.ThreePid) MemoryThreePid(io.kamax.mxisd.config.memory.MemoryThreePid) MemoryThreePid(io.kamax.mxisd.config.memory.MemoryThreePid) MatrixID(io.kamax.matrix.MatrixID) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID)

Example 8 with ThreePid

use of io.kamax.matrix.ThreePid in project mxisd by kamax-io.

the class SessionMananger method validateRemote.

public void validateRemote(String sid, String secret) {
    ThreePidSession session = getSessionIfValidated(sid, secret);
    if (!session.isRemote()) {
        throw new NotAllowedException("Cannot remotely validate a local session");
    }
    log.info("Session {} for {}: Validating remote 3PID session {} on {}", sid, session.getThreePid(), session.getRemoteId(), session.getRemoteServer());
    if (session.isRemoteValidated()) {
        log.info("Session {} for {}: Already remotely validated", sid, session.getThreePid());
        return;
    }
    HttpGet validateReq = new HttpGet(session.getRemoteServer() + "/_matrix/identity/api/v1/3pid/getValidated3pid?sid=" + session.getRemoteId() + "&client_secret=" + session.getRemoteSecret());
    try (CloseableHttpResponse response = client.execute(validateReq)) {
        int status = response.getStatusLine().getStatusCode();
        if (status < 200 || status >= 300) {
            throw new RemoteIdentityServerException("Remote identity server returned with status " + status);
        }
        JsonObject o = new GsonParser().parse(response.getEntity().getContent());
        if (o.has("errcode")) {
            String errcode = o.get("errcode").getAsString();
            if (StringUtils.equals("M_SESSION_NOT_VALIDATED", errcode)) {
                throw new SessionNotValidatedException();
            } else if (StringUtils.equals("M_NO_VALID_SESSION", errcode)) {
                throw new SessionUnknownException();
            } else {
                throw new RemoteIdentityServerException("Unknown error while validating Remote 3PID session: " + errcode + " - " + o.get("error").getAsString());
            }
        }
        if (o.has("validated_at")) {
            ThreePid remoteThreePid = new ThreePid(o.get("medium").getAsString(), o.get("address").getAsString());
            if (!session.getThreePid().equals(remoteThreePid)) {
                // sanity check
                throw new InternalServerError("Local 3PID " + session.getThreePid() + " and remote 3PID " + remoteThreePid + " do not match for session " + session.getId());
            }
            log.info("Session {} for {}: Remotely validated successfully", sid, session.getThreePid());
            session.validateRemote();
            storage.updateThreePidSession(session.getDao());
            log.info("Session {} was updated in storage", sid);
        }
    } catch (IOException e) {
        log.warn("Session {} for {}: Failed to validated remotely on {}: {}", sid, session.getThreePid(), session.getRemoteServer(), e.getMessage());
        throw new RemoteIdentityServerException(e.getMessage());
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) JsonObject(com.google.gson.JsonObject) 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) ThreePid(io.kamax.matrix.ThreePid)

Example 9 with ThreePid

use of io.kamax.matrix.ThreePid in project mxisd by kamax-io.

the class PlaceholderNotificationGenerator method populateForInvite.

protected String populateForInvite(IThreePidInviteReply invite, String input) {
    ThreePid tpid = new ThreePid(invite.getInvite().getMedium(), invite.getInvite().getAddress());
    String senderName = invite.getInvite().getProperties().getOrDefault("sender_display_name", "");
    String senderNameOrId = StringUtils.defaultIfBlank(senderName, invite.getInvite().getSender().getId());
    String roomName = invite.getInvite().getProperties().getOrDefault("room_name", "");
    String roomNameOrId = StringUtils.defaultIfBlank(roomName, invite.getInvite().getRoomId());
    return populateForCommon(input, tpid).replace("%SENDER_ID%", invite.getInvite().getSender().getId()).replace("%SENDER_NAME%", senderName).replace("%SENDER_NAME_OR_ID%", senderNameOrId).replace("%INVITE_MEDIUM%", tpid.getMedium()).replace("%INVITE_ADDRESS%", tpid.getAddress()).replace("%ROOM_ID%", invite.getInvite().getRoomId()).replace("%ROOM_NAME%", roomName).replace("%ROOM_NAME_OR_ID%", roomNameOrId);
}
Also used : ThreePid(io.kamax.matrix.ThreePid)

Aggregations

ThreePid (io.kamax.matrix.ThreePid)9 io.kamax.matrix._ThreePid (io.kamax.matrix._ThreePid)3 BackendAuthResult (io.kamax.mxisd.auth.provider.BackendAuthResult)3 JsonObject (com.google.gson.JsonObject)2 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)2 IOException (java.io.IOException)2 NumberParseException (com.google.i18n.phonenumbers.NumberParseException)1 MatrixID (io.kamax.matrix.MatrixID)1 AuthenticatorProvider (io.kamax.mxisd.auth.provider.AuthenticatorProvider)1 MemoryIdentityConfig (io.kamax.mxisd.config.memory.MemoryIdentityConfig)1 MemoryThreePid (io.kamax.mxisd.config.memory.MemoryThreePid)1 SessionEmailTokenRequestJson (io.kamax.mxisd.controller.identity.v1.io.SessionEmailTokenRequestJson)1 SessionPhoneTokenRequestJson (io.kamax.mxisd.controller.identity.v1.io.SessionPhoneTokenRequestJson)1 SingleLookupReply (io.kamax.mxisd.lookup.SingleLookupReply)1 ThreePidMapping (io.kamax.mxisd.lookup.ThreePidMapping)1 IThreePidSession (io.kamax.mxisd.threepid.session.IThreePidSession)1 ThreePidSession (io.kamax.mxisd.threepid.session.ThreePidSession)1 GsonParser (io.kamax.mxisd.util.GsonParser)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1