Search in sources :

Example 71 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class UserServicePluginNG method addRosterItem.

/**
	 * Adds the roster item.
	 *
	 * @param username
	 *            the username
	 * @param rosterItemEntity
	 *            the roster item entity
	 * @throws ServiceException
	 *             the service exception
	 * @throws UserAlreadyExistsException
	 *             the user already exists exception
	 * @throws SharedGroupException
	 *             the shared group exception
	 * @throws UserNotFoundException
	 *             the user not found exception
	 */
public void addRosterItem(String username, RosterItemEntity rosterItemEntity) throws ServiceException, UserAlreadyExistsException, SharedGroupException, UserNotFoundException {
    Roster roster = getUserRoster(username);
    if (rosterItemEntity.getJid() == null) {
        throw new ServiceException("JID is null", "JID", "IllegalArgumentException", Response.Status.BAD_REQUEST);
    }
    JID jid = new JID(rosterItemEntity.getJid());
    try {
        roster.getRosterItem(jid);
        throw new UserAlreadyExistsException(jid.toBareJID());
    } catch (UserNotFoundException e) {
    // Roster item does not exist. Try to add it.
    }
    if (roster != null) {
        RosterItem rosterItem = roster.createRosterItem(jid, rosterItemEntity.getNickname(), rosterItemEntity.getGroups(), false, true);
        UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
        rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
        roster.updateRosterItem(rosterItem);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Roster(org.jivesoftware.openfire.roster.Roster) ServiceException(org.jivesoftware.openfire.exceptions.ServiceException) JID(org.xmpp.packet.JID) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Example 72 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class XMPPCallbackHandler method handle.

@Override
public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    String realm;
    String name = null;
    for (Callback callback : callbacks) {
        if (callback instanceof RealmCallback) {
            ((RealmCallback) callback).setText(XMPPServer.getInstance().getServerInfo().getXMPPDomain());
        } else if (callback instanceof NameCallback) {
            name = ((NameCallback) callback).getName();
            if (name == null) {
                name = ((NameCallback) callback).getDefaultName();
            }
        //Log.debug("XMPPCallbackHandler: NameCallback: " + name);
        } else if (callback instanceof PasswordCallback) {
            try {
                // Get the password from the UserProvider. Some UserProviders may not support
                // this operation
                ((PasswordCallback) callback).setPassword(AuthFactory.getPassword(name).toCharArray());
            //Log.debug("XMPPCallbackHandler: PasswordCallback");
            } catch (UserNotFoundException | UnsupportedOperationException e) {
                throw new IOException(e.toString());
            }
        } else if (callback instanceof VerifyPasswordCallback) {
            //Log.debug("XMPPCallbackHandler: VerifyPasswordCallback");
            VerifyPasswordCallback vpcb = (VerifyPasswordCallback) callback;
            try {
                AuthToken at = AuthFactory.authenticate(name, new String(vpcb.getPassword()));
                vpcb.setVerified((at != null));
            } catch (Exception e) {
                vpcb.setVerified(false);
            }
        } else if (callback instanceof AuthorizeCallback) {
            //Log.debug("XMPPCallbackHandler: AuthorizeCallback");
            AuthorizeCallback authCallback = ((AuthorizeCallback) callback);
            // Principal that authenticated
            String principal = authCallback.getAuthenticationID();
            // Username requested (not full JID)
            String username = authCallback.getAuthorizationID();
            // a lot of users to fail to log in if their clients is sending an incorrect value
            if (username != null && username.contains("@")) {
                username = username.substring(0, username.lastIndexOf("@"));
            }
            if (principal.equals(username)) {
                //client perhaps made no request, get default username
                username = AuthorizationManager.map(principal);
                if (Log.isDebugEnabled()) {
                //Log.debug("XMPPCallbackHandler: no username requested, using " + username);
                }
            }
            if (AuthorizationManager.authorize(username, principal)) {
                if (Log.isDebugEnabled()) {
                //Log.debug("XMPPCallbackHandler: " + principal + " authorized to " + username);
                }
                authCallback.setAuthorized(true);
                authCallback.setAuthorizedID(username);
            } else {
                if (Log.isDebugEnabled()) {
                //Log.debug("XMPPCallbackHandler: " + principal + " not authorized to " + username);
                }
                authCallback.setAuthorized(false);
            }
        } else {
            if (Log.isDebugEnabled()) {
            //Log.debug("XMPPCallbackHandler: Callback: " + callback.getClass().getSimpleName());
            }
            throw new UnsupportedCallbackException(callback, "Unrecognized Callback");
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) VerifyPasswordCallback(org.jivesoftware.openfire.sasl.VerifyPasswordCallback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) RealmCallback(javax.security.sasl.RealmCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) VerifyPasswordCallback(org.jivesoftware.openfire.sasl.VerifyPasswordCallback) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) VerifyPasswordCallback(org.jivesoftware.openfire.sasl.VerifyPasswordCallback) AuthToken(org.jivesoftware.openfire.auth.AuthToken) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RealmCallback(javax.security.sasl.RealmCallback)

Example 73 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class IQPEPHandler method handleIQ.

// *****************************************************************
// *** Generic module management ends here. From this point on   ***
// *** more specific PEP related implementation after this point ***
// *****************************************************************
/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jivesoftware.openfire.handler.IQHandler#handleIQ(org.xmpp.packet.IQ)
	 */
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
    // Do nothing if server is not enabled
    if (!isEnabled()) {
        IQ reply = IQ.createResultIQ(packet);
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.service_unavailable);
        return reply;
    }
    final JID senderJID = packet.getFrom();
    if (packet.getTo() == null) {
        // packet addressed to service itself (not to a node/user)
        final String jidFrom = senderJID.toBareJID();
        packet = packet.createCopy();
        packet.setTo(jidFrom);
        if (packet.getType() == IQ.Type.set) {
            PEPService pepService = pepServiceManager.getPEPService(jidFrom);
            // If no service exists yet for jidFrom, create one.
            if (pepService == null) {
                try {
                    pepService = pepServiceManager.create(senderJID);
                } catch (IllegalArgumentException ex) {
                    final IQ reply = IQ.createResultIQ(packet);
                    reply.setChildElement(packet.getChildElement().createCopy());
                    reply.setError(PacketError.Condition.not_allowed);
                    return reply;
                }
                // Probe presences
                pepServiceManager.start(pepService);
                // PEPService.
                try {
                    final RosterManager rm = XMPPServer.getInstance().getRosterManager();
                    final Roster roster = rm.getRoster(senderJID.getNode());
                    for (final RosterItem item : roster.getRosterItems()) {
                        if (item.getSubStatus() == RosterItem.SUB_BOTH || item.getSubStatus() == RosterItem.SUB_FROM) {
                            createSubscriptionToPEPService(pepService, item.getJid(), senderJID);
                        }
                    }
                } catch (UserNotFoundException e) {
                // Do nothing
                }
            }
            // If publishing a node, and the node doesn't exist, create it.
            final Element childElement = packet.getChildElement();
            final Element publishElement = childElement.element("publish");
            if (publishElement != null) {
                final String nodeID = publishElement.attributeValue("node");
                // TODO: Implement XEP-0084
                if (nodeID.startsWith("http://www.xmpp.org/extensions/xep-0084.html")) {
                    IQ reply = IQ.createResultIQ(packet);
                    reply.setChildElement(packet.getChildElement().createCopy());
                    reply.setError(PacketError.Condition.feature_not_implemented);
                    return reply;
                }
                if (pepService.getNode(nodeID) == null) {
                    // Create the node
                    final JID creator = new JID(jidFrom);
                    final LeafNode newNode = new LeafNode(pepService, pepService.getRootCollectionNode(), nodeID, creator);
                    newNode.addOwner(creator);
                    newNode.saveToDB();
                }
            }
            // Process with PubSub as usual.
            pepServiceManager.process(pepService, packet);
        } else if (packet.getType() == IQ.Type.get) {
            final PEPService pepService = pepServiceManager.getPEPService(jidFrom);
            if (pepService != null) {
                pepServiceManager.process(pepService, packet);
            } else {
                // Process with PubSub using a dummyService. In the case where an IQ packet is sent to
                // a user who does not have a PEP service, we wish to utilize the error reporting flow
                // already present in the PubSubEngine. This gives the illusion that every user has a
                // PEP service, as required by the specification.
                PEPService dummyService = new PEPService(XMPPServer.getInstance(), senderJID.toBareJID());
                pepServiceManager.process(dummyService, packet);
            }
        }
    } else if (packet.getType() == IQ.Type.get || packet.getType() == IQ.Type.set) {
        // packet was addressed to a node.
        final String jidTo = packet.getTo().toBareJID();
        final PEPService pepService = pepServiceManager.getPEPService(jidTo);
        if (pepService != null) {
            pepServiceManager.process(pepService, packet);
        } else {
            // Process with PubSub using a dummyService. In the case where an IQ packet is sent to
            // a user who does not have a PEP service, we wish to utilize the error reporting flow
            // already present in the PubSubEngine. This gives the illusion that every user has a
            // PEP service, as required by the specification.
            PEPService dummyService = new PEPService(XMPPServer.getInstance(), senderJID.toBareJID());
            pepServiceManager.process(dummyService, packet);
        }
    } else {
        // Ignore IQ packets of type 'error' or 'result'.
        return null;
    }
    // Other error flows were handled in pubSubEngine.process(...)
    return null;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) JID(org.xmpp.packet.JID) Roster(org.jivesoftware.openfire.roster.Roster) Element(org.dom4j.Element) LeafNode(org.jivesoftware.openfire.pubsub.LeafNode) IQ(org.xmpp.packet.IQ) RosterManager(org.jivesoftware.openfire.roster.RosterManager)

Example 74 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class PEPService method sendNotification.

@Override
public void sendNotification(Node node, Message message, JID recipientJID) {
    message.setTo(recipientJID);
    message.setFrom(getAddress());
    message.setID(StringUtils.randomString(8));
    // If the recipient subscribed with a bare JID and this PEPService can retrieve
    // presence information for the recipient, collect all of their full JIDs and
    // send the notification to each below.
    Set<JID> recipientFullJIDs = new HashSet<>();
    if (XMPPServer.getInstance().isLocal(recipientJID)) {
        if (recipientJID.getResource() == null) {
            for (ClientSession clientSession : SessionManager.getInstance().getSessions(recipientJID.getNode())) {
                recipientFullJIDs.add(clientSession.getAddress());
            }
        }
    } else {
        // Since recipientJID is not local, try to get presence info from cached known remote
        // presences.
        // TODO: OF-605 the old code depends on a cache that would contain presence state on all (?!) JIDS on all (?!)
        // remote domains. As we cannot depend on this information to be correct (even if we could ensure that this
        // potentially unlimited amount of data would indeed be manageable in the first place), this code was removed.
        recipientFullJIDs.add(recipientJID);
    }
    if (recipientFullJIDs.isEmpty()) {
        router.route(message);
        return;
    }
    for (JID recipientFullJID : recipientFullJIDs) {
        // to the service owner.
        try {
            JID publisher = null;
            // Get the ID of the node that had an item published to or retracted from.
            Element itemsElement = message.getElement().element("event").element("items");
            String nodeID = itemsElement.attributeValue("node");
            // Get the ID of the item that was published or retracted.
            String itemID = null;
            Element itemElement = itemsElement.element("item");
            if (itemElement == null) {
                Element retractElement = itemsElement.element("retract");
                if (retractElement != null) {
                    itemID = retractElement.attributeValue("id");
                }
            } else {
                itemID = itemElement.attributeValue("id");
            }
            // Check if the recipientFullJID is interested in notifications for this node.
            // If the recipient has not yet requested any notification filtering, continue and send
            // the notification.
            EntityCapabilities entityCaps = entityCapsManager.getEntityCapabilities(recipientFullJID);
            if (entityCaps != null) {
                if (!entityCaps.containsFeature(nodeID + "+notify")) {
                    return;
                }
            }
            // This full JID will be used as the "replyto" address in the addressing extension.
            if (node.isCollectionNode()) {
                for (Node leafNode : node.getNodes()) {
                    if (leafNode.getNodeID().equals(nodeID)) {
                        publisher = leafNode.getPublishedItem(itemID).getPublisher();
                        // Ensure the recipientJID has access to receive notifications for items published to the leaf node.
                        AccessModel accessModel = leafNode.getAccessModel();
                        if (!accessModel.canAccessItems(leafNode, recipientFullJID, publisher)) {
                            return;
                        }
                        break;
                    }
                }
            } else {
                publisher = node.getPublishedItem(itemID).getPublisher();
            }
            // Ensure the recipient is subscribed to the service owner's (publisher's) presence.
            if (canProbePresence(publisher, recipientFullJID)) {
                Element addresses = DocumentHelper.createElement(QName.get("addresses", "http://jabber.org/protocol/address"));
                Element address = addresses.addElement("address");
                address.addAttribute("type", "replyto");
                address.addAttribute("jid", publisher.toString());
                Message extendedMessage = message.createCopy();
                extendedMessage.addExtension(new PacketExtension(addresses));
                extendedMessage.setTo(recipientFullJID);
                router.route(extendedMessage);
            }
        } catch (IndexOutOfBoundsException e) {
        // Do not add addressing extension to message.
        } catch (UserNotFoundException e) {
            // Do not add addressing extension to message.
            router.route(message);
        } catch (NullPointerException e) {
            try {
                if (canProbePresence(getAddress(), recipientFullJID)) {
                    message.setTo(recipientFullJID);
                }
            } catch (UserNotFoundException e1) {
            // Do nothing
            }
            router.route(message);
        }
    }
}
Also used : PacketExtension(org.xmpp.packet.PacketExtension) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) AccessModel(org.jivesoftware.openfire.pubsub.models.AccessModel) Element(org.dom4j.Element) CollectionNode(org.jivesoftware.openfire.pubsub.CollectionNode) Node(org.jivesoftware.openfire.pubsub.Node) EntityCapabilities(org.jivesoftware.openfire.entitycaps.EntityCapabilities) ClientSession(org.jivesoftware.openfire.session.ClientSession) HashSet(java.util.HashSet)

Example 75 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class PresenceAccess method canSubscribe.

@Override
public boolean canSubscribe(Node node, JID owner, JID subscriber) {
    // Let node owners and sysadmins always subcribe to the node
    if (node.isAdmin(owner)) {
        return true;
    }
    XMPPServer server = XMPPServer.getInstance();
    for (JID nodeOwner : node.getOwners()) {
        // Give access to the owner of the roster :)
        if (nodeOwner.equals(owner)) {
            return true;
        }
        // Check that the node owner is a local user
        if (server.isLocal(nodeOwner)) {
            try {
                Roster roster = server.getRosterManager().getRoster(nodeOwner.getNode());
                RosterItem item = roster.getRosterItem(owner);
                // Check that the subscriber is subscribe to the node owner's presence
                return item != null && (RosterItem.SUB_BOTH == item.getSubStatus() || RosterItem.SUB_FROM == item.getSubStatus());
            } catch (UserNotFoundException e) {
            // Do nothing
            }
        } else {
            // Owner of the node is a remote user. This should never happen.
            Log.warn("Node with access model Presence has a remote user as owner: " + node.getNodeID());
        }
    }
    return false;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) XMPPServer(org.jivesoftware.openfire.XMPPServer) JID(org.xmpp.packet.JID) Roster(org.jivesoftware.openfire.roster.Roster)

Aggregations

UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)118 JID (org.xmpp.packet.JID)50 Element (org.dom4j.Element)28 Roster (org.jivesoftware.openfire.roster.Roster)27 RosterItem (org.jivesoftware.openfire.roster.RosterItem)26 User (org.jivesoftware.openfire.user.User)25 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)23 IQ (org.xmpp.packet.IQ)15 ArrayList (java.util.ArrayList)14 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)12 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)11 Group (org.jivesoftware.openfire.group.Group)10 UserManager (org.jivesoftware.openfire.user.UserManager)10 Workgroup (org.jivesoftware.xmpp.workgroup.Workgroup)10 Presence (org.xmpp.packet.Presence)10 NotFoundException (org.jivesoftware.util.NotFoundException)9 SQLException (java.sql.SQLException)8 List (java.util.List)8 IOException (java.io.IOException)7 Connection (java.sql.Connection)7