KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > sasl > SASLMechanism


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.sasl;
22
23 import org.jivesoftware.smack.SASLAuthentication;
24 import org.jivesoftware.smack.util.StringUtils;
25
26 import java.io.IOException JavaDoc;
27
28 /**
29  * Base class for SASL mechanisms. Subclasses must implement three methods:
30  * <ul>
31  * <li>{@link #getName()} -- returns the common name of the SASL mechanism.</li>
32  * <li>{@link #getAuthenticationText(String, String, String)} -- authentication text to include
33  * in the initial <tt>auth</tt> stanza.</li>
34  * <li>{@link #getChallengeResponse(byte[])} -- to respond challenges made by the server.</li>
35  * </ul>
36  *
37  * @author Gaston Dombiak
38  */

39 public abstract class SASLMechanism {
40
41     private SASLAuthentication saslAuthentication;
42
43     public SASLMechanism(SASLAuthentication saslAuthentication) {
44         super();
45         this.saslAuthentication = saslAuthentication;
46     }
47
48     /**
49      * Builds and sends the <tt>auth</tt> stanza to the server.
50      *
51      * @param username the username of the user being authenticated.
52      * @param host the hostname where the user account resides.
53      * @param password the password of the user.
54      * @throws IOException If a network error occures while authenticating.
55      */

56     public void authenticate(String JavaDoc username, String JavaDoc host, String JavaDoc password) throws IOException JavaDoc {
57         // Build the authentication stanza encoding the authentication text
58
StringBuffer JavaDoc stanza = new StringBuffer JavaDoc();
59         stanza.append("<auth mechanism=\"").append(getName());
60         stanza.append("\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
61         String JavaDoc authenticationText = getAuthenticationText(username, host, password);
62         if (authenticationText != null) {
63             stanza.append(StringUtils.encodeBase64(authenticationText));
64         }
65         stanza.append("</auth>");
66
67         // Send the authentication to the server
68
getSASLAuthentication().send(stanza.toString());
69     }
70
71     /**
72      * The server is challenging the SASL mechanism for the stanza he just sent. Send a
73      * response to the server's challenge.
74      *
75      * @param challenge a base64 encoded string representing the challenge.
76      */

77     public void challengeReceived(String JavaDoc challenge) throws IOException JavaDoc {
78         // Build the challenge response stanza encoding the response text
79
StringBuffer JavaDoc stanza = new StringBuffer JavaDoc();
80         stanza.append("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
81         String JavaDoc authenticationText = getChallengeResponse(StringUtils.decodeBase64(challenge));
82         stanza.append(StringUtils.encodeBase64(authenticationText));
83         stanza.append("</response>");
84
85         // Send the authentication to the server
86
getSASLAuthentication().send(stanza.toString());
87     }
88
89     /**
90      * Returns the response text to send answering the challenge sent by the server. Mechanisms
91      * that will never receive a challenge may redefine this method returning <tt>null</tt>.
92      *
93      * @param bytes the challenge sent by the server.
94      * @return the response text to send to answer the challenge sent by the server.
95      */

96     protected abstract String JavaDoc getChallengeResponse(byte[] bytes);
97
98     /**
99      * Returns the common name of the SASL mechanism. E.g.: PLAIN, DIGEST-MD5 or KERBEROS_V4.
100      *
101      * @return the common name of the SASL mechanism.
102      */

103     protected abstract String JavaDoc getName();
104
105     /**
106      * Returns the authentication text to include in the initial <tt>auth</tt> stanza
107      * or <tt>null</tt> if nothing should be added.
108      *
109      * @param username the username of the user being authenticated.
110      * @param host the hostname where the user account resides.
111      * @param password the password of the user.
112      * @return the authentication text to include in the initial <tt>auth</tt> stanza
113      * or <tt>null</tt> if nothing should be added.
114      */

115     protected abstract String JavaDoc getAuthenticationText(String JavaDoc username, String JavaDoc host, String JavaDoc password);
116
117     protected SASLAuthentication getSASLAuthentication() {
118         return saslAuthentication;
119     }
120 }
121
Popular Tags