Search in sources :

Example 26 with JID

use of org.xmpp.packet.JID in project Openfire by igniterealtime.

the class LocalMUCRoom method memberAdded.

public void memberAdded(AddMember addMember) {
    JID bareJID = addMember.getBareJID();
    removeOwner(bareJID);
    removeAdmin(bareJID);
    removeOutcast(bareJID);
    // Associate the reserved nickname with the bareJID
    members.put(addMember.getBareJID(), addMember.getNickname().toLowerCase());
}
Also used : GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID)

Example 27 with JID

use of org.xmpp.packet.JID in project Openfire by igniterealtime.

the class LocalMUCRoom method addAdmin.

@Override
public List<Presence> addAdmin(JID jid, MUCRole sendRole) throws ForbiddenException, ConflictException {
    final JID bareJID = jid.asBareJID();
    lock.writeLock().lock();
    try {
        MUCRole.Affiliation oldAffiliation = MUCRole.Affiliation.none;
        if (MUCRole.Affiliation.owner != sendRole.getAffiliation()) {
            throw new ForbiddenException();
        }
        // Check that the room always has an owner
        if (owners.contains(bareJID) && owners.size() == 1) {
            throw new ConflictException();
        }
        // Check if user is already an admin
        if (admins.contains(bareJID)) {
            // Do nothing
            return Collections.emptyList();
        }
        admins.add(bareJID);
        // Remove the user from other affiliation lists
        if (removeOwner(bareJID)) {
            oldAffiliation = MUCRole.Affiliation.owner;
        } else if (removeMember(bareJID)) {
            oldAffiliation = MUCRole.Affiliation.member;
        } else if (removeOutcast(bareJID)) {
            oldAffiliation = MUCRole.Affiliation.outcast;
        }
        // Update the DB if the room is persistent
        MUCPersistenceManager.saveAffiliationToDB(this, bareJID, null, MUCRole.Affiliation.admin, oldAffiliation);
    } finally {
        lock.writeLock().unlock();
    }
    // Update other cluster nodes with new affiliation
    CacheFactory.doClusterTask(new AddAffiliation(this, jid.toBareJID(), MUCRole.Affiliation.admin));
    // based on the group(s) of the affected user(s)
    return applyAffiliationChange(getRole(), bareJID, null);
}
Also used : ForbiddenException(org.jivesoftware.openfire.muc.ForbiddenException) MUCRole(org.jivesoftware.openfire.muc.MUCRole) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID) ConflictException(org.jivesoftware.openfire.muc.ConflictException) AddAffiliation(org.jivesoftware.openfire.muc.cluster.AddAffiliation)

Example 28 with JID

use of org.xmpp.packet.JID in project Openfire by igniterealtime.

the class WebDAVLiteServlet method isAuthenticated.

/**
     * Verifies that the user is authenticated via some mechanism such as Basic Auth.  If the
     * authentication fails, this method will alter the HTTP response to include a request for
     * auth and send the unauthorized response back to the client.
     *
     * TODO: Handle some form of special token auth, perhaps provided a room connection?
     * TODO: If it's not a local account, we should try message auth access?  XEP-0070?
     * TODO: Should we support digest auth as well?
     *
     * @param request Object representing the HTTP request.
     * @param response Object representing the HTTP response.
     * @return True or false if the user is authenticated.
     * @throws ServletException If there was a servlet related exception.
     * @throws IOException If there was an IO error while setting the error.
     */
private Boolean isAuthenticated(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String auth = request.getHeader("Authorization");
    JID jid;
    try {
        if (auth == null || !request.getAuthType().equals(HttpServletRequest.BASIC_AUTH)) {
            throw new Exception("No authorization or improper authorization provided.");
        }
        auth = auth.substring(auth.indexOf(" "));
        String decoded = new String(Base64.decode(auth));
        int i = decoded.indexOf(":");
        String username = decoded.substring(0, i);
        if (!username.contains("@")) {
            throw new Exception("Not a valid JID.");
        }
        jid = new JID(username);
        if (XMPPServer.getInstance().isLocal(jid)) {
            String password = decoded.substring(i + 1, decoded.length());
            if (AuthFactory.authenticate(username, password) == null) {
                throw new Exception("Authentication failed.");
            }
        } else {
            // TODO: Authenticate a remote user, probably via message auth.
            throw new Exception("Not a local account.");
        }
        return true;
    } catch (Exception e) {
        /**
             * This covers all possible authentication issues.  Eg:
             * - not enough of auth info passed in
             * - failed auth
             */
        response.setHeader("WWW-Authenticate", "Basic realm=\"Openfire WebDAV\"");
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        return false;
    }
}
Also used : JID(org.xmpp.packet.JID) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 29 with JID

use of org.xmpp.packet.JID in project Openfire by igniterealtime.

the class WebDAVLiteServlet method isAuthorized.

/**
     * Verifies that the authenticated user is a member of a conference service and room, or else
     * they are not entitled to view any of the files in the room.
     *
     * @param request Object representing the HTTP request.
     * @param response Object representing the HTTP response.
     * @param service Subdomain of the conference service they are trying to access files for.
     * @param room Room in the conference service they are trying to access files for.
     * @return True or false if the user is authenticated.
     * @throws ServletException If there was a servlet related exception.
     * @throws IOException If there was an IO error while setting the error.
     */
private Boolean isAuthorized(HttpServletRequest request, HttpServletResponse response, String service, String room) throws ServletException, IOException {
    String auth = request.getHeader("Authorization");
    try {
        if (auth == null || !request.getAuthType().equals(HttpServletRequest.BASIC_AUTH)) {
            throw new Exception("No authorization or improper authorization provided.");
        }
        auth = auth.substring(auth.indexOf(" "));
        String decoded = new String(Base64.decode(auth));
        int i = decoded.indexOf(":");
        String username = decoded.substring(0, i);
        if (!username.contains("@")) {
            throw new Exception("Not a valid JID.");
        }
        final JID bareJID = new JID(username).asBareJID();
        XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(service).getChatRoom(room).getOccupantsByBareJID(bareJID);
        return true;
    } catch (Exception e) {
        /**
             * This covers all possible authorization issues.  Eg:
             * - accessing a room that doesn't exist
             * - accessing a room that user isn't a member of
             */
        response.setHeader("WWW-Authenticate", "Basic realm=\"Openfire WebDAV\"");
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return false;
    }
}
Also used : JID(org.xmpp.packet.JID) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 30 with JID

use of org.xmpp.packet.JID in project Openfire by igniterealtime.

the class JDBCUserProvider method loadUser.

@Override
public User loadUser(String username) throws UserNotFoundException {
    if (username.contains("@")) {
        if (!XMPPServer.getInstance().isLocal(new JID(username))) {
            throw new UserNotFoundException("Cannot load user of remote server: " + username);
        }
        username = username.substring(0, username.lastIndexOf("@"));
    }
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = getConnection();
        pstmt = con.prepareStatement(loadUserSQL);
        pstmt.setString(1, username);
        rs = pstmt.executeQuery();
        if (!rs.next()) {
            throw new UserNotFoundException();
        }
        String name = rs.getString(1);
        String email = rs.getString(2);
        return new User(username, name, email, new Date(), new Date());
    } catch (Exception e) {
        throw new UserNotFoundException(e);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
}
Also used : JID(org.xmpp.packet.JID) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Date(java.util.Date) SQLException(java.sql.SQLException)

Aggregations

JID (org.xmpp.packet.JID)330 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)76 Element (org.dom4j.Element)70 ArrayList (java.util.ArrayList)55 IQ (org.xmpp.packet.IQ)38 Presence (org.xmpp.packet.Presence)38 SQLException (java.sql.SQLException)36 PreparedStatement (java.sql.PreparedStatement)31 Connection (java.sql.Connection)30 Group (org.jivesoftware.openfire.group.Group)30 Date (java.util.Date)28 ResultSet (java.sql.ResultSet)27 Message (org.xmpp.packet.Message)25 GroupJID (org.jivesoftware.openfire.group.GroupJID)23 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)23 NotFoundException (org.jivesoftware.util.NotFoundException)22 Roster (org.jivesoftware.openfire.roster.Roster)21 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)20 RosterItem (org.jivesoftware.openfire.roster.RosterItem)19 User (org.jivesoftware.openfire.user.User)17