Home | History | Annotate | Download | only in header
      1 /*
      2 * Conditions Of Use
      3 *
      4 * This software was developed by employees of the National Institute of
      5 * Standards and Technology (NIST), an agency of the Federal Government.
      6 * Pursuant to title 15 Untied States Code Section 105, works of NIST
      7 * employees are not subject to copyright protection in the United States
      8 * and are considered to be in the public domain.  As a result, a formal
      9 * license is not needed to use the software.
     10 *
     11 * This software is provided by NIST as a service and is expressly
     12 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
     13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
     14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
     15 * AND DATA ACCURACY.  NIST does not warrant or make any representations
     16 * regarding the use of the software or the results thereof, including but
     17 * not limited to the correctness, accuracy, reliability or usefulness of
     18 * the software.
     19 *
     20 * Permission to use this software is contingent upon your acceptance
     21 * of the terms of this agreement.
     22 *
     23 */
     24 package gov.nist.javax.sip.header;
     25 
     26 import gov.nist.core.*;
     27 import java.text.ParseException;
     28 
     29 /**
     30  * Authentication info SIP Header.
     31  *
     32  * @author M. Ranganathan   NIST/ITL/ANTD
     33  * @since 1.1
     34  * @version 1.2 $Revision: 1.9 $ $Date: 2009/07/17 18:57:27 $
     35  *
     36  *
     37  */
     38 public final class AuthenticationInfo
     39     extends ParametersHeader
     40     implements javax.sip.header.AuthenticationInfoHeader {
     41 
     42     /**
     43      * Comment for <code>serialVersionUID</code>
     44      */
     45     private static final long serialVersionUID = -4371927900917127057L;
     46 
     47     /** Default contstructor.
     48      */
     49     public AuthenticationInfo() {
     50         super(NAME);
     51         parameters.setSeparator(COMMA); // Odd ball.
     52     }
     53 
     54     public void add(NameValue nv) {
     55         parameters.set(nv);
     56     }
     57 
     58     /** Value of header encoded in canonical form.
     59      */
     60 
     61     protected String encodeBody() {
     62         return parameters.encode();
     63 
     64     }
     65 
     66     /** Get the name value pair for a given authentication info parameter.
     67      *
     68      *@param name is the name for which we want to retrieve the name value
     69      *  list.
     70      */
     71 
     72     public NameValue getAuthInfo(String name) {
     73         return parameters.getNameValue(name);
     74     }
     75 
     76     /**
     77      * Returns the AuthenticationInfo value of this AuthenticationInfoHeader.
     78      *
     79      *
     80      *
     81      * @return the String representing the AuthenticationInfo
     82      *
     83      *
     84      *
     85      */
     86     public String getAuthenticationInfo() {
     87         return this.encodeBody();
     88     }
     89 
     90     /** Returns the CNonce value of this AuthenticationInfoHeader.
     91      *
     92      * @return the String representing the cNonce information, null if value is
     93      * not set.
     94      * @since v1.1
     95      */
     96     public String getCNonce() {
     97         return this.getParameter(ParameterNames.CNONCE);
     98     }
     99 
    100     /** Returns the nextNonce value of this AuthenticationInfoHeader.
    101      *
    102      * @return the String representing the nextNonce
    103      * information, null if value is not set.
    104      * @since v1.1
    105      */
    106     public String getNextNonce() {
    107         return this.getParameter(ParameterNames.NEXT_NONCE);
    108     }
    109 
    110     /** Returns the Nonce Count value of this AuthenticationInfoHeader.
    111      *
    112      * @return the integer representing the nonceCount information, -1 if value is
    113      * not set.
    114      * @since v1.1
    115      */
    116     public int getNonceCount() {
    117         return this.getParameterAsInt(ParameterNames.NONCE_COUNT);
    118     }
    119 
    120     /** Returns the messageQop value of this AuthenticationInfoHeader.
    121      *
    122      * @return the string representing the messageQop information, null if the
    123      * value is not set.
    124      * @since v1.1
    125      */
    126     public String getQop() {
    127         return this.getParameter(ParameterNames.QOP);
    128     }
    129 
    130     /** Returns the Response value of this AuthenticationInfoHeader.
    131      *
    132      * @return the String representing the Response information.
    133      * @since v1.1
    134      */
    135     public String getResponse() {
    136         return this.getParameter(ParameterNames.RESPONSE_AUTH);
    137     }
    138 
    139     /** Sets the CNonce of the AuthenticationInfoHeader to the <var>cNonce</var>
    140      * parameter value.
    141      *
    142      * @param cNonce - the new cNonce String of this AuthenticationInfoHeader.
    143      * @throws ParseException which signals that an error has been reached
    144      * unexpectedly while parsing the cNonce value.
    145      * @since v1.1
    146      */
    147     public void setCNonce(String cNonce) throws ParseException {
    148         this.setParameter(ParameterNames.CNONCE, cNonce);
    149     }
    150 
    151     /** Sets the NextNonce of the AuthenticationInfoHeader to the <var>nextNonce</var>
    152      * parameter value.
    153      *
    154      * @param nextNonce - the new nextNonce String of this AuthenticationInfoHeader.
    155      * @throws ParseException which signals that an error has been reached
    156      * unexpectedly while parsing the nextNonce value.
    157      * @since v1.1
    158      */
    159     public void setNextNonce(String nextNonce) throws ParseException {
    160         this.setParameter(ParameterNames.NEXT_NONCE, nextNonce);
    161     }
    162 
    163     /** Sets the Nonce Count of the AuthenticationInfoHeader to the <var>nonceCount</var>
    164      * parameter value.
    165      *
    166      * @param nonceCount - the new nonceCount integer of this AuthenticationInfoHeader.
    167      * @throws ParseException which signals that an error has been reached
    168      * unexpectedly while parsing the nonceCount value.
    169      * @since v1.1
    170      */
    171     public void setNonceCount(int nonceCount) throws ParseException {
    172         if (nonceCount < 0)
    173             throw new ParseException("bad value", 0);
    174         String nc = Integer.toHexString(nonceCount);
    175 
    176         String base = "00000000";
    177         nc = base.substring(0, 8 - nc.length()) + nc;
    178         this.setParameter(ParameterNames.NC, nc);
    179     }
    180 
    181     /** Sets the Qop value of the AuthenticationInfoHeader to the new
    182      * <var>qop</var> parameter value.
    183      *
    184      * @param qop - the new Qop string of this AuthenticationInfoHeader.
    185      * @throws ParseException which signals that an error has been reached
    186      * unexpectedly while parsing the Qop value.
    187      * @since v1.1
    188      */
    189     public void setQop(String qop) throws ParseException {
    190         this.setParameter(ParameterNames.QOP, qop);
    191     }
    192 
    193     /** Sets the Response of the
    194      * AuthenticationInfoHeader to the new <var>response</var>
    195      * parameter value.
    196      *
    197      * @param response - the new response String of this
    198      * AuthenticationInfoHeader.
    199      * @throws ParseException which signals that an error has been reached
    200      * unexpectedly while parsing the Response.
    201      * @since v1.1
    202      */
    203     public void setResponse(String response) throws ParseException {
    204         this.setParameter(ParameterNames.RESPONSE_AUTH, response);
    205     }
    206 
    207     public void setParameter(String name, String value) throws ParseException {
    208         if (name == null)
    209             throw new NullPointerException("null name");
    210         NameValue nv = super.parameters.getNameValue(name.toLowerCase());
    211         if (nv == null) {
    212             nv = new NameValue(name, value);
    213             if (name.equalsIgnoreCase(ParameterNames.QOP)
    214                 || name.equalsIgnoreCase(ParameterNames.NEXT_NONCE)
    215                 || name.equalsIgnoreCase(ParameterNames.REALM)
    216                 || name.equalsIgnoreCase(ParameterNames.CNONCE)
    217                 || name.equalsIgnoreCase(ParameterNames.NONCE)
    218                 || name.equalsIgnoreCase(ParameterNames.OPAQUE)
    219                 || name.equalsIgnoreCase(ParameterNames.USERNAME)
    220                 || name.equalsIgnoreCase(ParameterNames.DOMAIN)
    221                 || name.equalsIgnoreCase(ParameterNames.NEXT_NONCE)
    222                 || name.equalsIgnoreCase(ParameterNames.RESPONSE_AUTH)) {
    223                 if (value == null)
    224                     throw new NullPointerException("null value");
    225                 if (value.startsWith(Separators.DOUBLE_QUOTE))
    226                     throw new ParseException(
    227                         value + " : Unexpected DOUBLE_QUOTE",
    228                         0);
    229                 nv.setQuotedValue();
    230             }
    231             super.setParameter(nv);
    232         } else
    233             nv.setValueAsObject(value);
    234     }
    235 }
    236