powered by Jive Software

Test if an account exists on the jabber server

Is there now a solution to find out if an account with a specific username still exists on the jabber server?

I want to differ if the login is not possible because the password is wrong or the account don’'t exist.

Thanks for help!

andre

I had the same problem, nobody replied when I posted this

question.

I ended up modifying the XMPPConnection.login()

method so the error messages were different for an account

not existing vs. a wrong password. I’'ve only tested it with one server implementation, so it might not work for you.

My modified method follows (only a couple lines were changed, I marked them with “GW”).

George


public synchronized void login(String username, String password, String resource)

throws XMPPException {

if (!isConnected()) {

throw new IllegalStateException(“Not connected to server.”);

}

if (authenticated) {

throw new IllegalStateException(“Already logged in to server.”);

}

// If we send an authentication packet in “get” mode with just the username,

// the server will return the list of authentication protocols it supports.

Authentication discoveryAuth = new Authentication();

discoveryAuth.setType(IQ.Type.GET);

discoveryAuth.setUsername(username);

PacketCollector collector =

packetReader.createPacketCollector(new PacketIDFilter(discoveryAuth.getPacketID()));

// Send the packet

packetWriter.sendPacket(discoveryAuth);

// Wait up to a certain number of seconds for a response from the server.

IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());

if (response == null) {

throw new XMPPException(“No response from the server.”);

}

// If the server replied with an error, throw an exception.

else if (response.getType() == IQ.Type.ERROR) {

XMPPError errXMPP = new XMPPError(401,“Account does not exist”);//GW;

throw new XMPPException(errXMPP);

//throw new XMPPException(response.getError());

}

// Otherwise, no error so continue processing.

Authentication authTypes = (Authentication) response;

collector.cancel();

// Now, create the authentication packet we’'ll send to the server.

Authentication auth = new Authentication();

auth.setUsername(username);

// Figure out if we should use digest or plain text authentication.

if (authTypes.getDigest() != null) {

auth.setDigest(connectionID, password);

}

else if (authTypes.getPassword() != null) {

auth.setPassword(password);

}

else {

throw new XMPPException(“Server does not support compatible authentication mechanism.”);

}

auth.setResource(resource);

collector = packetReader.createPacketCollector(new PacketIDFilter(auth.getPacketID()));

// Send the packet.

packetWriter.sendPacket(auth);

// Wait up to a certain number of seconds for a response from the server.

response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());

if (response == null) {

throw new XMPPException(“Authentication failed.”);

}

else if (response.getType() == IQ.Type.ERROR) {

XMPPError errXMPP = new XMPPError(401,“Wrong Password”);//GW

throw new XMPPException(errXMPP);

//throw new XMPPException(response.getError());

}

// Set the user.

if (response.getTo() != null) {

this.user = response.getTo();

}

else {

this.user = username + “@” + this.host;

if (resource != null) {

this.user += “/” + resource;

}

}

// We’'re done with the collector, so explicitly cancel it.

collector.cancel();

// Create the roster.

this.roster = new Roster(this);

roster.reload();

// Set presence to online.

packetWriter.sendPacket(new Presence(Presence.Type.AVAILABLE));

// Indicate that we’'re now authenticated.

authenticated = true;

anonymous = false;

// If debugging is enabled, change the the debug window title to include the

// name we are now logged-in as.

// If DEBUG_ENABLED was set to true AFTER the connection was created the debugger

// will be null

if (DEBUG_ENABLED && debugger != null) {

debugger.userHasLogged(user);

}

}

Andre,

For security reasons XMPP does not provide a way to differentiate if the login was not possible because the password was wrong or the account didn’'t exist.

When you try to log into the server you can receive three possible errors:

. Error code 401 - Incorrect Credentials (bad username or password)

. Error code 409 - Resource Conflict (trying to log in twice using the same resource)

. Error code 406 - Required Information Not Provided

The posted code in the previous message won’'t work because a compliant XMPP server should not return an error if the username does not exist while requesting authentication fields from the server. Instead, the server should return the normal authentication fields. For more information follow url=http://www.jabber.org/jeps/jep-0078.htmlthis link /url.

Regards,

– Gato