Home | History | Annotate | Download | only in packet
      1 /**
      2  * $RCSfile$
      3  * $Revision$
      4  * $Date$
      5  *
      6  * Copyright 2003-2007 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.packet;
     22 
     23 import org.jivesoftware.smack.util.StringUtils;
     24 
     25 /**
     26  * Authentication packet, which can be used to login to a XMPP server as well
     27  * as discover login information from the server.
     28  */
     29 public class Authentication extends IQ {
     30 
     31     private String username = null;
     32     private String password = null;
     33     private String digest = null;
     34     private String resource = null;
     35 
     36     /**
     37      * Create a new authentication packet. By default, the packet will be in
     38      * "set" mode in order to perform an actual authentication with the server.
     39      * In order to send a "get" request to get the available authentication
     40      * modes back from the server, change the type of the IQ packet to "get":
     41      * <p/>
     42      * <p><tt>setType(IQ.Type.GET);</tt>
     43      */
     44     public Authentication() {
     45         setType(IQ.Type.SET);
     46     }
     47 
     48     /**
     49      * Returns the username, or <tt>null</tt> if the username hasn't been sent.
     50      *
     51      * @return the username.
     52      */
     53     public String getUsername() {
     54         return username;
     55     }
     56 
     57     /**
     58      * Sets the username.
     59      *
     60      * @param username the username.
     61      */
     62     public void setUsername(String username) {
     63         this.username = username;
     64     }
     65 
     66     /**
     67      * Returns the plain text password or <tt>null</tt> if the password hasn't
     68      * been set.
     69      *
     70      * @return the password.
     71      */
     72     public String getPassword() {
     73         return password;
     74     }
     75 
     76     /**
     77      * Sets the plain text password.
     78      *
     79      * @param password the password.
     80      */
     81     public void setPassword(String password) {
     82         this.password = password;
     83     }
     84 
     85     /**
     86      * Returns the password digest or <tt>null</tt> if the digest hasn't
     87      * been set. Password digests offer a more secure alternative for
     88      * authentication compared to plain text. The digest is the hex-encoded
     89      * SHA-1 hash of the connection ID plus the user's password. If the
     90      * digest and password are set, digest authentication will be used. If
     91      * only one value is set, the respective authentication mode will be used.
     92      *
     93      * @return the digest of the user's password.
     94      */
     95     public String getDigest() {
     96         return digest;
     97     }
     98 
     99     /**
    100      * Sets the digest value using a connection ID and password. Password
    101      * digests offer a more secure alternative for authentication compared to
    102      * plain text. The digest is the hex-encoded SHA-1 hash of the connection ID
    103      * plus the user's password. If the digest and password are set, digest
    104      * authentication will be used. If only one value is set, the respective
    105      * authentication mode will be used.
    106      *
    107      * @param connectionID the connection ID.
    108      * @param password     the password.
    109      * @see org.jivesoftware.smack.Connection#getConnectionID()
    110      */
    111     public void setDigest(String connectionID, String password) {
    112         this.digest = StringUtils.hash(connectionID + password);
    113     }
    114 
    115     /**
    116      * Sets the digest value directly. Password digests offer a more secure
    117      * alternative for authentication compared to plain text. The digest is
    118      * the hex-encoded SHA-1 hash of the connection ID plus the user's password.
    119      * If the digest and password are set, digest authentication will be used.
    120      * If only one value is set, the respective authentication mode will be used.
    121      *
    122      * @param digest the digest, which is the SHA-1 hash of the connection ID
    123      *               the user's password, encoded as hex.
    124      * @see org.jivesoftware.smack.Connection#getConnectionID()
    125      */
    126     public void setDigest(String digest) {
    127         this.digest = digest;
    128     }
    129 
    130     /**
    131      * Returns the resource or <tt>null</tt> if the resource hasn't been set.
    132      *
    133      * @return the resource.
    134      */
    135     public String getResource() {
    136         return resource;
    137     }
    138 
    139     /**
    140      * Sets the resource.
    141      *
    142      * @param resource the resource.
    143      */
    144     public void setResource(String resource) {
    145         this.resource = resource;
    146     }
    147 
    148     public String getChildElementXML() {
    149         StringBuilder buf = new StringBuilder();
    150         buf.append("<query xmlns=\"jabber:iq:auth\">");
    151         if (username != null) {
    152             if (username.equals("")) {
    153                 buf.append("<username/>");
    154             }
    155             else {
    156                 buf.append("<username>").append(username).append("</username>");
    157             }
    158         }
    159         if (digest != null) {
    160             if (digest.equals("")) {
    161                 buf.append("<digest/>");
    162             }
    163             else {
    164                 buf.append("<digest>").append(digest).append("</digest>");
    165             }
    166         }
    167         if (password != null && digest == null) {
    168             if (password.equals("")) {
    169                 buf.append("<password/>");
    170             }
    171             else {
    172                 buf.append("<password>").append(StringUtils.escapeForXML(password)).append("</password>");
    173             }
    174         }
    175         if (resource != null) {
    176             if (resource.equals("")) {
    177                 buf.append("<resource/>");
    178             }
    179             else {
    180                 buf.append("<resource>").append(resource).append("</resource>");
    181             }
    182         }
    183         buf.append("</query>");
    184         return buf.toString();
    185     }
    186 }
    187