KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > NonSASLAuthentication


1 /**
2  * $RCSfile$
3  * $Revision: $
4  * $Date: $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smack;
22
23 import org.jivesoftware.smack.filter.PacketIDFilter;
24 import org.jivesoftware.smack.packet.Authentication;
25 import org.jivesoftware.smack.packet.IQ;
26
27 /**
28  * Implementation of JEP-0078: Non-SASL Authentication. Follow the following
29  * <a HREF=http://www.jabber.org/jeps/jep-0078.html>link</a> to obtain more
30  * information about the JEP.
31  *
32  * @author Gaston Dombiak
33  */

34 class NonSASLAuthentication implements UserAuthentication {
35
36     private XMPPConnection connection;
37
38     public NonSASLAuthentication(XMPPConnection connection) {
39         super();
40         this.connection = connection;
41     }
42
43     public String JavaDoc authenticate(String JavaDoc username, String JavaDoc password, String JavaDoc resource) throws
44             XMPPException {
45         // If we send an authentication packet in "get" mode with just the username,
46
// the server will return the list of authentication protocols it supports.
47
Authentication discoveryAuth = new Authentication();
48         discoveryAuth.setType(IQ.Type.GET);
49         discoveryAuth.setUsername(username);
50
51         PacketCollector collector =
52             connection.createPacketCollector(new PacketIDFilter(discoveryAuth.getPacketID()));
53         // Send the packet
54
connection.sendPacket(discoveryAuth);
55         // Wait up to a certain number of seconds for a response from the server.
56
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
57         if (response == null) {
58             throw new XMPPException("No response from the server.");
59         }
60         // If the server replied with an error, throw an exception.
61
else if (response.getType() == IQ.Type.ERROR) {
62             throw new XMPPException(response.getError());
63         }
64         // Otherwise, no error so continue processing.
65
Authentication authTypes = (Authentication) response;
66         collector.cancel();
67
68         // Now, create the authentication packet we'll send to the server.
69
Authentication auth = new Authentication();
70         auth.setUsername(username);
71
72         // Figure out if we should use digest or plain text authentication.
73
if (authTypes.getDigest() != null) {
74             auth.setDigest(connection.getConnectionID(), password);
75         }
76         else if (authTypes.getPassword() != null) {
77             auth.setPassword(password);
78         }
79         else {
80             throw new XMPPException("Server does not support compatible authentication mechanism.");
81         }
82
83         auth.setResource(resource);
84
85         collector = connection.createPacketCollector(new PacketIDFilter(auth.getPacketID()));
86         // Send the packet.
87
connection.sendPacket(auth);
88         // Wait up to a certain number of seconds for a response from the server.
89
response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
90         if (response == null) {
91             throw new XMPPException("Authentication failed.");
92         }
93         else if (response.getType() == IQ.Type.ERROR) {
94             throw new XMPPException(response.getError());
95         }
96         // We're done with the collector, so explicitly cancel it.
97
collector.cancel();
98
99         return response.getTo();
100     }
101 }
102
Popular Tags