Java Code Examples for org.jivesoftware.openfire.session.Session#STATUS_AUTHENTICATED

The following examples show how to use org.jivesoftware.openfire.session.Session#STATUS_AUTHENTICATED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StanzaHandler.java    From Openfire with Apache License 2.0 6 votes vote down vote up
/**
 * After compression was successful we should open a new stream and offer
 * new stream features such as resource binding and session establishment. Notice that
 * resource binding and session establishment should only be offered to clients (i.e. not
 * to servers or external components)
 */
private void compressionSuccessful() {
    StringBuilder sb = new StringBuilder(340);
    sb.append(getStreamHeader());
    sb.append("<stream:features>");
    // Include SASL mechanisms only if client has not been authenticated
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Include available SASL Mechanisms
        sb.append(SASLAuthentication.getSASLMechanisms(session));
    }
    // Include specific features such as resource binding and session establishment
    // for client sessions
    String specificFeatures = session.getAvailableStreamFeatures();
    if (specificFeatures != null) {
        sb.append(specificFeatures);
    }
    sb.append("</stream:features>");
    connection.deliverRawText(sb.toString());
}
 
Example 2
Source File: SocketReadingMode.java    From Openfire with Apache License 2.0 6 votes vote down vote up
/**
 * After compression was successful we should open a new stream and offer
 * new stream features such as resource binding and session establishment. Notice that
 * resource binding and session establishment should only be offered to clients (i.e. not
 * to servers or external components)
 */
protected void compressionSuccessful() throws XmlPullParserException, IOException
{
    StringBuilder sb = new StringBuilder(340);
    sb.append(geStreamHeader());
    sb.append("<stream:features>");
    // Include SASL mechanisms only if client has not been authenticated
    if (socketReader.session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Include available SASL Mechanisms
        sb.append(SASLAuthentication.getSASLMechanisms(socketReader.session));
    }
    // Include specific features such as resource binding and session establishment
    // for client sessions
    String specificFeatures = socketReader.session.getAvailableStreamFeatures();
    if (specificFeatures != null)
    {
        sb.append(specificFeatures);
    }
    sb.append("</stream:features>");
    socketReader.connection.deliverRawText(sb.toString());
}
 
Example 3
Source File: MultiplexerStanzaHandler.java    From Openfire with Apache License 2.0 6 votes vote down vote up
@Override
protected void processIQ(final IQ packet) {
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Session is not authenticated so return error
        IQ reply = new IQ();
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setID(packet.getID());
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getTo());
        reply.setError(PacketError.Condition.not_authorized);
        session.process(reply);
        return;
    }
    // Process the packet
    packetHandler.handle(packet);
}
 
Example 4
Source File: ComponentStanzaHandler.java    From Openfire with Apache License 2.0 6 votes vote down vote up
@Override
protected void processIQ(IQ packet) throws UnauthorizedException {
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Session is not authenticated so return error
        IQ reply = new IQ();
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setID(packet.getID());
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getTo());
        reply.setError(PacketError.Condition.not_authorized);
        session.process(reply);
        return;
    }
    // Keep track of the component that sent an IQ get/set
    if (packet.getType() == IQ.Type.get || packet.getType() == IQ.Type.set) {
        // Handle subsequent bind packets
        LocalComponentSession componentSession = (LocalComponentSession) session;
        // Get the external component of this session
        LocalComponentSession.LocalExternalComponent component =
                (LocalComponentSession.LocalExternalComponent) componentSession.getExternalComponent();
        component.track(packet);
    }
    super.processIQ(packet);
}
 
Example 5
Source File: S2STestService.java    From Openfire with Apache License 2.0 6 votes vote down vote up
/**
 * Logs the status of the session.
 */
private void logSessionStatus() {
    final DomainPair pair = new DomainPair(XMPPServer.getInstance().getServerInfo().getXMPPDomain(), domain);
    OutgoingServerSession session = XMPPServer.getInstance().getSessionManager().getOutgoingServerSession(pair);
    if (session != null) {
        int connectionStatus = session.getStatus();
        switch(connectionStatus) {
        case Session.STATUS_CONNECTED:
            Log.info("Session is connected.");
            break;
        case Session.STATUS_CLOSED:
            Log.info("Session is closed.");
            break;
        case Session.STATUS_AUTHENTICATED:
            Log.info("Session is authenticated.");
            break;
        }
    } else {
        Log.info("Failed to establish server to server session.");
    }
}
 
Example 6
Source File: AuditorImpl.java    From Openfire with Apache License 2.0 5 votes vote down vote up
public AuditPacket(Packet packet, Session session) {
    element = docFactory.createElement("packet", "http://www.jivesoftware.org");
    creationDate = new Date();
    if (session != null && session.getStreamID() != null) {
        element.addAttribute("streamID", session.getStreamID().toString());
    }
    switch (session == null ? 0 : session.getStatus()) {
        case Session.STATUS_AUTHENTICATED:
            element.addAttribute("status", "auth");
            break;
        case Session.STATUS_CLOSED:
            element.addAttribute("status", "closed");
            break;
        case Session.STATUS_CONNECTED:
            element.addAttribute("status", "connected");
            // This is a workaround. Since we don't want to have an incorrect FROM attribute
            // value we need to clean up the FROM attribute. The FROM attribute will contain
            // an incorrect value since we are setting a fake JID until the user actually
            // authenticates with the server.
            packet.setFrom((String) null);
            break;
        default:
            element.addAttribute("status", "unknown");
            break;
    }
    element.addAttribute("timestamp", auditFormat.format(creationDate));
    element.add(packet.getElement());
}
 
Example 7
Source File: MultiplexerStanzaHandler.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Process stanza sent by a client that is connected to a connection manager. The
 * original stanza is wrapped in the route element. Only a single stanza must be
 * wrapped in the route element.
 *
 * @param packet the route element.
 */
private void processRoute(final Route packet) {
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Session is not authenticated so return error
        Route reply = new Route(packet.getStreamID());
        reply.setID(packet.getID());
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getTo());
        reply.setError(PacketError.Condition.not_authorized);
        session.process(reply);
        return;
    }
    // Process the packet
    packetHandler.route(packet);
}
 
Example 8
Source File: ComponentStanzaHandler.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
protected void processPresence(Presence packet) throws UnauthorizedException {
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Session is not authenticated so return error
        Presence reply = new Presence();
        reply.setID(packet.getID());
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getTo());
        reply.setError(PacketError.Condition.not_authorized);
        session.process(reply);
        return;
    }
    super.processPresence(packet);
}
 
Example 9
Source File: ComponentStanzaHandler.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
protected void processMessage(Message packet) throws UnauthorizedException {
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        // Session is not authenticated so return error
        Message reply = new Message();
        reply.setID(packet.getID());
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getTo());
        reply.setError(PacketError.Condition.not_authorized);
        session.process(reply);
        return;
    }
    super.processMessage(packet);
}
 
Example 10
Source File: StreamManager.java    From Openfire with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to enable Stream Management for the entity identified by the provided JID.
 *
 * @param namespace The namespace that defines what version of SM is to be enabled.
 * @param resume Whether the client is requesting a resumable session.
 */
private void enable( String namespace, boolean resume )
{


    boolean offerResume = allowResume();
    // Ensure that resource binding has occurred.
    if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
        this.namespace = namespace;
        sendUnexpectedError();
        return;
    }

    String smId = null;

    synchronized ( this )
    {
        // Do nothing if already enabled
        if ( isEnabled() )
        {
            sendUnexpectedError();
            return;
        }
        this.namespace = namespace;

        this.resume = resume && offerResume;
        if ( this.resume ) {
            // Create SM-ID.
            smId = StringUtils.encodeBase64( session.getAddress().getResource() + "\0" + session.getStreamID().getID());
        }
    }

    // Send confirmation to the requestee.
    Element enabled = new DOMElement(QName.get("enabled", namespace));
    if (this.resume) {
        enabled.addAttribute("resume", "true");
        enabled.addAttribute("id", smId);
        if ( !namespace.equals(NAMESPACE_V2) && LOCATION_ENABLED.getValue() ) {
            // OF-1925: Hint clients to do resumes at the same cluster node.
            enabled.addAttribute("location", XMPPServer.getInstance().getServerInfo().getHostname());
        }

        // OF-1926: Tell clients how long they can be detached.
        if ( MAX_SERVER_ENABLED.getValue() ) {
            final int sessionDetachTime = XMPPServer.getInstance().getSessionManager().getSessionDetachTime();
            if ( sessionDetachTime > 0 ) {
                enabled.addAttribute("max", String.valueOf(sessionDetachTime/1000));
            }
        }
    }
    session.deliverRawText(enabled.asXML());
}
 
Example 11
Source File: StreamManager.java    From Openfire with Apache License 2.0 4 votes vote down vote up
private void startResume(String namespace, String previd, long h) {
    Log.debug("Attempting resumption for {}, h={}", previd, h);
    this.namespace = namespace;
    // Ensure that resource binding has NOT occurred.
    if (!allowResume() ) {
        Log.debug("Unable to process session resumption attempt, as session {} is in a state where session resumption is not allowed.", session);
        sendUnexpectedError();
        return;
    }
    if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
        Log.debug("Unable to process session resumption attempt, as session {} is not authenticated.", session);
        sendUnexpectedError();
        return;
    }
    AuthToken authToken = null;
    // Ensure that resource binding has occurred.
    if (session instanceof ClientSession) {
        authToken = ((LocalClientSession) session).getAuthToken();
    }
    if (authToken == null) {
        Log.debug("Unable to process session resumption attempt, as session {} does not provide any auth context.", session);
        sendUnexpectedError();
        return;
    }
    // Decode previd.
    String resource;
    String streamId;
    try {
        StringTokenizer toks = new StringTokenizer(new String(StringUtils.decodeBase64(previd), StandardCharsets.UTF_8), "\0");
        resource = toks.nextToken();
        streamId = toks.nextToken();
    } catch (Exception e) {
        Log.debug("Exception from previd decode:", e);
        sendUnexpectedError();
        return;
    }
    final JID fullJid;
    if ( authToken.isAnonymous() ){
        fullJid = new JID(resource, authToken.getDomain(), resource, true);
    } else {
        fullJid = new JID(authToken.getUsername(), authToken.getDomain(), resource, true);
    }
    Log.debug("Resuming session for '{}'. Current session: {}", fullJid, session.getStreamID());

    // Locate existing session.
    LocalClientSession otherSession = (LocalClientSession)XMPPServer.getInstance().getRoutingTable().getClientRoute(fullJid);
    if (otherSession == null) {
        sendError(new PacketError(PacketError.Condition.item_not_found));
        return;
    }
    if (!otherSession.getStreamID().getID().equals(streamId)) {
        sendError(new PacketError(PacketError.Condition.item_not_found));
        return;
    }
    Log.debug("Found existing session for '{}', checking status", fullJid);
    // Previd identifies proper session. Now check SM status
    if (!otherSession.getStreamManager().resume) {
        Log.debug("Not allowing a client of '{}' to resume a session, the session to be resumed does not have the stream management resumption feature enabled.", fullJid);
        sendError(new PacketError(PacketError.Condition.unexpected_request));
        return;
    }
    if (otherSession.getStreamManager().namespace == null) {
        Log.debug("Not allowing a client of '{}' to resume a session, the session to be resumed disabled SM functionality as a response to an earlier error.", fullJid);
        sendError(new PacketError(PacketError.Condition.unexpected_request));
        return;
    }
    if (!otherSession.getStreamManager().namespace.equals(namespace)) {
        Log.debug("Not allowing a client of '{}' to resume a session, the session to be resumed used a different version ({}) of the session management resumption feature as compared to the version that's requested now: {}.", fullJid, otherSession.getStreamManager().namespace, namespace);
        sendError(new PacketError(PacketError.Condition.unexpected_request));
        return;
    }
    if (!otherSession.getStreamManager().validateClientAcknowledgement(h)) {
        Log.debug("Not allowing a client of '{}' to resume a session, as it reports it received more stanzas from us than that we've send it.", fullJid);
        sendError(new PacketError(PacketError.Condition.unexpected_request));
        return;
    }
    if (!otherSession.isDetached()) {
        Log.debug("Existing session {} of '{}' is not detached; detaching.", otherSession.getStreamID(), fullJid);
        Connection oldConnection = otherSession.getConnection();
        otherSession.setDetached();
        oldConnection.close();
    }
    Log.debug("Attaching to other session '{}' of '{}'.", otherSession.getStreamID(), fullJid);
    // If we're all happy, re-attach the connection from the pre-existing session to the new session, discarding the old session.
    otherSession.reattach(session, h);
    Log.debug("Perform resumption of session {} for '{}', using connection from session {}", otherSession.getStreamID(), fullJid, session.getStreamID());
}