Search in sources :

Example 6 with NotAllowedException

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

the class SessionManager method create.

public String create(String server, ThreePid tpid, String secret, int attempt, String nextLink) {
    PolicyTemplate policy = cfg.getPolicy().getValidation();
    if (!policy.isEnabled()) {
        throw new NotAllowedException("Validating 3PID is disabled");
    }
    synchronized (this) {
        log.info("Server {} is asking to create session for {} (Attempt #{}) - Next link: {}", server, tpid, attempt, nextLink);
        Optional<IThreePidSessionDao> dao = storage.findThreePidSession(tpid, secret);
        if (dao.isPresent()) {
            ThreePidSession session = new ThreePidSession(dao.get());
            log.info("We already have a session for {}: {}", tpid, session.getId());
            if (session.getAttempt() < attempt) {
                log.info("Received attempt {} is greater than stored attempt {}, sending validation communication", attempt, session.getAttempt());
                notifMgr.sendForValidation(session);
                log.info("Sent validation notification to {}", tpid);
                session.increaseAttempt();
                storage.updateThreePidSession(session.getDao());
            }
            return session.getId();
        } else {
            log.info("No existing session for {}", tpid);
            String sessionId;
            do {
                sessionId = Long.toString(System.currentTimeMillis());
            } while (storage.getThreePidSession(sessionId).isPresent());
            String token = RandomStringUtils.randomNumeric(6);
            ThreePidSession session = new ThreePidSession(sessionId, server, tpid, secret, attempt, nextLink, token);
            log.info("Generated new session {} to validate {} from server {}", sessionId, tpid, server);
            storage.insertThreePidSession(session.getDao());
            log.info("Stored session {}", sessionId);
            log.info("Session {} for {}: sending validation notification", sessionId, tpid);
            notifMgr.sendForValidation(session);
            return sessionId;
        }
    }
}
Also used : IThreePidSessionDao(io.kamax.mxisd.storage.dao.IThreePidSessionDao) NotAllowedException(io.kamax.mxisd.exception.NotAllowedException) ThreePidSession(io.kamax.mxisd.threepid.session.ThreePidSession) PolicyTemplate(io.kamax.mxisd.config.SessionConfig.Policy.PolicyTemplate)

Aggregations

NotAllowedException (io.kamax.mxisd.exception.NotAllowedException)6 JsonObject (com.google.gson.JsonObject)3 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)3 ThreePidSession (io.kamax.mxisd.threepid.session.ThreePidSession)3 ThreePid (io.kamax.matrix.ThreePid)2 BadRequestException (io.kamax.mxisd.exception.BadRequestException)2 PolicyTemplate (io.kamax.mxisd.config.SessionConfig.Policy.PolicyTemplate)1 InternalServerError (io.kamax.mxisd.exception.InternalServerError)1 RemoteHomeServerException (io.kamax.mxisd.exception.RemoteHomeServerException)1 SessionPhoneTokenRequestJson (io.kamax.mxisd.http.io.identity.SessionPhoneTokenRequestJson)1 SingleLookupReply (io.kamax.mxisd.lookup.SingleLookupReply)1 SingleLookupRequest (io.kamax.mxisd.lookup.SingleLookupRequest)1 IThreePidSessionDao (io.kamax.mxisd.storage.dao.IThreePidSessionDao)1 IOException (java.io.IOException)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1