From 6d6fdcf9b669bf7836e4551433b4d080efbfe8cb Mon Sep 17 00:00:00 2001 From: Gaston Dombiak Date: Mon, 5 Sep 2005 21:57:33 +0000 Subject: [PATCH] Added anonymous authentication. SMACK-84 git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2782 b35dd754-fafc-0310-a699-88a17e54d16e --- .../smack/NonSASLAuthentication.java | 27 +++++++++++++++++++ .../smack/UserAuthentication.java | 21 +++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/source/org/jivesoftware/smack/NonSASLAuthentication.java b/source/org/jivesoftware/smack/NonSASLAuthentication.java index 0cb4af597..57a2a513b 100644 --- a/source/org/jivesoftware/smack/NonSASLAuthentication.java +++ b/source/org/jivesoftware/smack/NonSASLAuthentication.java @@ -98,4 +98,31 @@ class NonSASLAuthentication implements UserAuthentication { return response.getTo(); } + + public String authenticateAnonymously() throws XMPPException { + // Create the authentication packet we'll send to the server. + Authentication auth = new Authentication(); + + PacketCollector collector = + connection.createPacketCollector(new PacketIDFilter(auth.getPacketID())); + // Send the packet. + connection.sendPacket(auth); + // 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("Anonymous login failed."); + } + else if (response.getType() == IQ.Type.ERROR) { + throw new XMPPException(response.getError()); + } + // We're done with the collector, so explicitly cancel it. + collector.cancel(); + + if (response.getTo() != null) { + return response.getTo(); + } + else { + return connection.serviceName + "/" + ((Authentication) response).getResource(); + } + } } diff --git a/source/org/jivesoftware/smack/UserAuthentication.java b/source/org/jivesoftware/smack/UserAuthentication.java index a50066cc7..94f0cf3ec 100644 --- a/source/org/jivesoftware/smack/UserAuthentication.java +++ b/source/org/jivesoftware/smack/UserAuthentication.java @@ -29,6 +29,27 @@ package org.jivesoftware.smack; */ interface UserAuthentication { + /** + * Authenticates the user with the server. This method will return the full JID provided by + * the server. The server may assign a full JID with a username and resource different than + * the requested by this method. + * + * @param username the username that is authenticating with the server. + * @param password the password to send to the server. + * @param resource the desired resource. + * @return the full JID provided by the server while binding a resource for the connection. + * @throws XMPPException if an error occures while authenticating. + */ String authenticate(String username, String password, String resource) throws XMPPException; + + /** + * Performs an anonymous authentication with the server. The server will created a new full JID + * for this connection. An exception will be thrown if the server does not support anonymous + * authentication. + * + * @return the full JID provided by the server while binding a resource for the connection. + * @throws XMPPException if an error occures while authenticating. + */ + String authenticateAnonymously() throws XMPPException; }