Search in sources :

Example 1 with IThreePidSessionDao

use of io.kamax.mxisd.storage.dao.IThreePidSessionDao in project mxisd by kamax-io.

the class SessionMananger 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 globally");
    }
    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);
            boolean isLocal = isLocal(tpid);
            log.info("Is 3PID bound to local domain? {}", isLocal);
            // This might need a configuration by medium type?
            PolicySource policySource = policy.forIf(isLocal);
            if (!policySource.isEnabled() || (!policySource.toLocal() && !policySource.toRemote())) {
                log.info("Session for {}: cancelled due to policy", tpid);
                throw new NotAllowedException("Validating " + (isLocal ? "local" : "remote") + " 3PID is not allowed");
            }
            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);
            // This might need a configuration by medium type?
            if (policySource.toLocal()) {
                log.info("Session {} for {}: sending local validation notification", sessionId, tpid);
                notifMgr.sendForValidation(session);
            } else {
                log.info("Session {} for {}: sending remote-only validation notification", sessionId, tpid);
                notifMgr.sendforRemoteValidation(session);
            }
            storage.insertThreePidSession(session.getDao());
            log.info("Stored session {}", sessionId, tpid, server);
            return sessionId;
        }
    }
}
Also used : IThreePidSessionDao(io.kamax.mxisd.storage.dao.IThreePidSessionDao) PolicySource(io.kamax.mxisd.config.SessionConfig.Policy.PolicyTemplate.PolicySource) ThreePidSession(io.kamax.mxisd.threepid.session.ThreePidSession) IThreePidSession(io.kamax.mxisd.threepid.session.IThreePidSession) PolicyTemplate(io.kamax.mxisd.config.SessionConfig.Policy.PolicyTemplate)

Example 2 with IThreePidSessionDao

use of io.kamax.mxisd.storage.dao.IThreePidSessionDao 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

PolicyTemplate (io.kamax.mxisd.config.SessionConfig.Policy.PolicyTemplate)2 IThreePidSessionDao (io.kamax.mxisd.storage.dao.IThreePidSessionDao)2 ThreePidSession (io.kamax.mxisd.threepid.session.ThreePidSession)2 PolicySource (io.kamax.mxisd.config.SessionConfig.Policy.PolicyTemplate.PolicySource)1 NotAllowedException (io.kamax.mxisd.exception.NotAllowedException)1 IThreePidSession (io.kamax.mxisd.threepid.session.IThreePidSession)1