Exemplo n.º 1
0
  /**
   * Adds a registration with this transport, or updates an existing one.
   *
   * @param jid JID of user to add registration to.
   * @param username Legacy username of registration.
   * @param password Legacy password of registration.
   * @param nickname Legacy nickname of registration.
   * @param noRosterItem True if the transport is not to show up in the user's roster.
   * @throws UserNotFoundException if registration or roster not found.
   * @throws IllegalAccessException if jid is not from this server.
   * @throws IllegalArgumentException if username is not valid for this transport type.
   */
  public void addNewRegistration(
      JID jid, String username, String password, String nickname, Boolean noRosterItem)
      throws UserNotFoundException, IllegalAccessException {
    Log.debug("Adding or updating registration for : " + jid.toString() + " / " + username);

    if (!XMPPServer.getInstance().isLocal(jid)) {
      throw new IllegalAccessException(
          "Domain of jid registering does not match domain of server.");
    }

    if (!parent.isUsernameValid(username)) {
      throw new IllegalArgumentException(
          "Username specified is not valid for this transport type.");
    }

    final Collection<Registration> registrations =
        RegistrationManager.getInstance().getRegistrations(jid, parent.transportType);
    boolean foundReg = false;
    boolean triggerRestart = false;
    for (final Registration registration : registrations) {
      if (!registration.getUsername().equals(username)) {
        Log.debug("Deleting existing registration before" + " creating a new one: " + registration);
        RegistrationManager.getInstance().deleteRegistration(registration);
      } else {
        Log.debug("Existing registration found that can be updated: " + registration);
        if ((registration.getPassword() != null && password == null)
            || (registration.getPassword() == null && password != null)
            || (registration.getPassword() != null
                && password != null
                && !registration.getPassword().equals(password))) {
          Log.debug("Updating password for existing registration: " + registration);
          registration.setPassword(password);
          triggerRestart = true;
        }
        if ((registration.getNickname() != null && nickname == null)
            || (registration.getNickname() == null && nickname != null)
            || (registration.getNickname() != null
                && nickname != null
                && !registration.getNickname().equals(nickname))) {
          Log.debug("Updating nickname for existing registration: " + registration);
          registration.setNickname(nickname);
          triggerRestart = true;
        }
        foundReg = true;
      }

      // if a change was made to the registration, restart it.
      if (triggerRestart) {
        try {
          Log.debug(
              "An existing registration was "
                  + "updated. Restarting the related session: "
                  + registration);
          final TransportSession relatedSession =
              parent.sessionManager.getSession(registration.getJID().getNode());
          parent.registrationLoggedOut(relatedSession);
        } catch (NotFoundException e) {
          // No worries, move on.
        }
      }
    }

    if (!foundReg) {
      RegistrationManager.getInstance()
          .createRegistration(jid, parent.transportType, username, password, nickname);
      triggerRestart = true;
    }

    if (triggerRestart) {
      Log.debug("Clean up any leftover roster items " + "from other transports for: " + jid);
      try {
        parent.cleanUpRoster(jid, !noRosterItem);
      } catch (UserNotFoundException ee) {
        throw new UserNotFoundException("Unable to find roster.");
      }
    }

    if (!noRosterItem) {
      try {
        Log.debug("Adding Transport roster item to the roster of: " + jid);
        parent.addOrUpdateRosterItem(jid, parent.getJID(), parent.getDescription(), "Transports");
      } catch (UserNotFoundException e) {
        throw new UserNotFoundException("User not registered with server.");
      }
    } else {
      Log.debug(
          "Not adding Transport roster item to the roster of: "
              + jid
              + " (as this was explicitly requested).");
    }
  }
Exemplo n.º 2
0
  /**
   * Handles a IQ-register 'get' request, which is to be interpreted as a request for a registration
   * form template. The template will be prefilled with data, if the requestee has a current
   * registration with the gateway.
   *
   * @param packet the IQ-register 'get' stanza.
   * @throws UnauthorizedException if the user is not allowed to make use of the gateway.
   */
  private void getRegistrationForm(IQ packet) throws UnauthorizedException {
    final JID from = packet.getFrom();
    final IQ result = IQ.createResultIQ(packet);

    // search for existing registrations
    String curUsername = null;
    String curPassword = null;
    String curNickname = null;
    Boolean registered = false;
    final Collection<Registration> registrations =
        RegistrationManager.getInstance().getRegistrations(from, parent.transportType);
    if (registrations.iterator().hasNext()) {
      Registration registration = registrations.iterator().next();
      curUsername = registration.getUsername();
      curPassword = registration.getPassword();
      curNickname = registration.getNickname();
      registered = true;
    }

    // Verify that the user is allowed to make use of the gateway.
    if (!registered && !parent.permissionManager.hasAccess(from)) {
      // User does not have permission to register with transport.
      // We want to allow them to change settings if they are already
      // registered.
      throw new UnauthorizedException(
          LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbyacls", "kraken"));
    }

    // generate a template registration form.
    final Element response =
        DocumentHelper.createElement(QName.get("query", NameSpace.IQ_REGISTER));
    final DataForm form = new DataForm(DataForm.Type.form);
    form.addInstruction(parent.getTerminologyRegistration());

    final FormField usernameField = form.addField();
    usernameField.setLabel(parent.getTerminologyUsername());
    usernameField.setVariable("username");
    usernameField.setType(FormField.Type.text_single);
    if (curUsername != null) {
      usernameField.addValue(curUsername);
    }

    final FormField passwordField = form.addField();
    passwordField.setLabel(parent.getTerminologyPassword());
    passwordField.setVariable("password");
    passwordField.setType(FormField.Type.text_private);
    if (curPassword != null) {
      passwordField.addValue(curPassword);
    }

    final String nicknameTerm = parent.getTerminologyNickname();
    if (nicknameTerm != null) {
      FormField nicknameField = form.addField();
      nicknameField.setLabel(nicknameTerm);
      nicknameField.setVariable("nick");
      nicknameField.setType(FormField.Type.text_single);
      if (curNickname != null) {
        nicknameField.addValue(curNickname);
      }
    }

    response.add(form.getElement());
    response.addElement("instructions").addText(parent.getTerminologyRegistration());

    // prefill the template with existing data if a registration already
    // exists.
    if (registered) {
      response.addElement("registered");
      response.addElement("username").addText(curUsername);
      if (curPassword == null) {
        response.addElement("password");
      } else {
        response.addElement("password").addText(curPassword);
      }
      if (nicknameTerm != null) {
        if (curNickname == null) {
          response.addElement("nick");
        } else {
          response.addElement("nick").addText(curNickname);
        }
      }
    } else {
      response.addElement("username");
      response.addElement("password");
      if (nicknameTerm != null) {
        response.addElement("nick");
      }
    }

    // Add special indicator for rosterless gateway handling.
    response.addElement("x").addNamespace("", NameSpace.IQ_GATEWAY_REGISTER);

    result.setChildElement(response);

    parent.sendPacket(result);
  }