Revision: 15905
Author: [email protected]
Date: Mon Dec 20 13:46:40 2010
Log: Update Issue 2173

Adding Impl
http://code.google.com/p/mobicents/source/detail?r=15905

Added:
/trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/core/session/SipSessionSecurity.java
Modified:
/trunk/servers/sip-servlets/sip-servlets-client/src/main/java/org/mobicents/javax/servlet/sip/SipServletRequestExt.java /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/core/session/MobicentsSipSession.java /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/core/session/SipSessionImpl.java /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/message/MobicentsSipSessionFacade.java /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/message/SipServletRequestImpl.java /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/security/AuthInfoEntry.java /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/security/authentication/DigestAuthenticator.java

=======================================
--- /dev/null
+++ /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/core/session/SipSessionSecurity.java Mon Dec 20 13:46:40 2010
@@ -0,0 +1,77 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.mobicents.servlet.sip.core.session;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.log4j.Logger;
+import org.mobicents.servlet.sip.security.AuthInfoEntry;
+
+/**
+ * Added to cache the credentials and store the nextnonce for Issue 2173 http://code.google.com/p/mobicents/issues/detail?id=2173 + * Handle Header [Authentication-Info: nextnonce="xyz"] in sip authorization responses
+ * @author [email protected]
+ *
+ */
+public class SipSessionSecurity {
+ private static final Logger logger = Logger.getLogger(SipSessionSecurity.class);
+
+ private ConcurrentHashMap<String, AuthInfoEntry> cachedAuthInfos = new ConcurrentHashMap<String, AuthInfoEntry>(2);
+       private String nextNonce;
+
+       public void removeCachedAuthInfo(String realm) {
+               if(logger.isDebugEnabled()) {
+                       logger.debug("Removing authInfo for realm " + realm);
+               }
+               getCachedAuthInfos().remove(realm);
+       }
+
+       public void addCachedAuthInfo(String realm, AuthInfoEntry 
authInfoEntry) {
+               if(logger.isDebugEnabled()) {
+ logger.debug("Caching authInfo " + authInfoEntry + " for realm " + realm);
+               }
+               getCachedAuthInfos().put(realm, authInfoEntry);
+       }
+
+       /**
+        * @return the cachedAuthInfos
+        */
+       public ConcurrentHashMap<String, AuthInfoEntry> getCachedAuthInfos() {
+               return cachedAuthInfos;
+       }
+
+       /**
+        * @param nextNonce the nextNonce to set
+        */
+       public void setNextNonce(String nextNonce) {
+               this.nextNonce = nextNonce;
+       }
+
+       /**
+        * @return the nextNonce
+        */
+       public String getNextNonce() {
+               return nextNonce;
+       }
+
+
+}
=======================================
--- /trunk/servers/sip-servlets/sip-servlets-client/src/main/java/org/mobicents/javax/servlet/sip/SipServletRequestExt.java Mon Dec 20 06:05:51 2010 +++ /trunk/servers/sip-servlets/sip-servlets-client/src/main/java/org/mobicents/javax/servlet/sip/SipServletRequestExt.java Mon Dec 20 13:46:40 2010
@@ -21,7 +21,9 @@
  */
 package org.mobicents.javax.servlet.sip;

+import javax.servlet.sip.AuthInfo;
 import javax.servlet.sip.SipServletRequest;
+import javax.servlet.sip.SipServletResponse;


 /**
@@ -38,5 +40,26 @@
  * @since 1.5
  */
 public interface SipServletRequestExt extends SipServletRequest {
-
-}
+       /**
+ * This method allows the addition of the appropriate authentication header(s) to the request that was challenged with a challenge response.<br/> + * It allows also to cache the credentials so that if a response containing an Authentication-Info header with a nextnonce, the credentials can be reused
+        * to generate the correct header
+ * @param challengeResponse The challenge response (401/407) receieved from a UAS/Proxy. + * @param authInfo The AuthInfo object that will add the Authentication headers to the request.
+        * @param cacheCredentials true if the user's credentials should be 
cached
+        */
+ void addAuthHeader(SipServletResponse challengeResponse, AuthInfo authInfo, boolean cacheCredentials);
+
+       /**
+ * This method allows the addition of the appropriate authentication header(s) to the request that was challenged with a challenge response without needing
+        * the creation and/or maintenance of the AuthInfo object.<br/>
+ * It allows also to cache the credentials so that if a response containing an Authentication-Info header with a nextnonce, the credentials can be reused
+        * to generate the correct header
+        *
+ * @param challengeResponse the challenge response (401/407) receieved from a UAS/Proxy.
+        * @param username username
+        * @param password password
+ * @param cacheCredentials true if the user's credentials should be cached for that realm
+        */
+ void addAuthHeader(SipServletResponse challengeResponse, String username, String password, boolean cacheCredentials);
+}
=======================================
--- /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/core/session/MobicentsSipSession.java Fri Nov 26 06:48:23 2010 +++ /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/core/session/MobicentsSipSession.java Mon Dec 20 13:46:40 2010
@@ -38,6 +38,7 @@
 import org.mobicents.servlet.sip.message.SipServletRequestImpl;
 import org.mobicents.servlet.sip.message.SipServletResponseImpl;
 import org.mobicents.servlet.sip.proxy.ProxyImpl;
+import org.mobicents.servlet.sip.security.AuthInfoEntry;

 /**
  *
@@ -151,20 +152,19 @@
        public boolean isValidInternal();

        public long getCseq();
-
        public void setCseq(long cseq);
-
        boolean validateCSeq(SipServletRequestImpl sipServletRequestImpl);

        String getTransport();
-
        void setTransport(String transport);

        int getRequestsPending();
-
        void setRequestsPending(int requests);

        void setAckReceived(long cSeq, boolean ackReceived);

        void notifySipSessionListeners(SipSessionEventType creation);
-}
+
+       void setSipSessionSecurity(SipSessionSecurity sipSessionSecurity);
+       SipSessionSecurity getSipSessionSecurity();
+}
=======================================
--- /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/core/session/SipSessionImpl.java Mon Dec 20 06:05:51 2010 +++ /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/core/session/SipSessionImpl.java Mon Dec 20 13:46:40 2010
@@ -72,6 +72,7 @@
 import javax.sip.SipProvider;
 import javax.sip.Transaction;
 import javax.sip.TransactionState;
+import javax.sip.header.AuthenticationInfoHeader;
 import javax.sip.header.CSeqHeader;
 import javax.sip.header.CallIdHeader;
 import javax.sip.header.ContactHeader;
@@ -264,6 +265,9 @@
        protected transient MobicentsSipSessionFacade facade = null;

protected transient ConcurrentHashMap<Long, Boolean> acksReceived = new ConcurrentHashMap<Long, Boolean>(2); + // Added for Issue 2173 http://code.google.com/p/mobicents/issues/detail?id=2173 + // Handle Header [Authentication-Info: nextnonce="xyz"] in sip authorization responses
+       protected transient SipSessionSecurity sipSessionSecurity;

protected SipSessionImpl (SipSessionKey key, SipFactoryImpl sipFactoryImpl, MobicentsSipApplicationSession mobicentsSipApplicationSession) {
                this.key = key;
@@ -498,6 +502,10 @@
throw new IllegalArgumentException("Problem setting param on the newly created susbequent request " + sipServletRequest,e);
                                        }
                                }
+
+ if(sipSessionSecurity != null && sipSessionSecurity.getNextNonce() != null) {
+                                       
sipServletRequest.updateAuthorizationHeadersWithNextNonce();
+                               }

                                return sipServletRequest;
                        } else {
@@ -530,6 +538,10 @@
                        }
                }

+ if(sipSessionSecurity != null && sipSessionSecurity.getNextNonce() != null) {
+                       
sipServletRequest.updateAuthorizationHeadersWithNextNonce();
+               }
+
                return sipServletRequest;
        }

@@ -822,6 +834,9 @@
                if(acksReceived != null) {
                        acksReceived.clear();
                }
+               if(sipSessionSecurity != null) {
+                       sipSessionSecurity.getCachedAuthInfos().clear();
+               }
 //             executorService.shutdown();
                parentSession = null;
                userPrincipal = null;
@@ -886,6 +901,7 @@
                subscriberURI = null;
                subscriptions = null;
                acksReceived = null;
+               sipSessionSecurity = null;
                // don't release or nullify the semaphore, it should be done 
externally
                // see Issue 
http://code.google.com/p/mobicents/issues/detail?id=1294
 //             if(semaphore != null) {
@@ -1305,6 +1321,20 @@
         */
public void updateStateOnResponse(SipServletResponseImpl response, boolean receive) {
                final String method = response.getMethod();
+
+ if(sipSessionSecurity != null && response.getStatus() >= 200 && response.getStatus() < 300) {
+                       // Issue 2173 
http://code.google.com/p/mobicents/issues/detail?id=2173
+ // it means some credentials were cached need to check if we need to store the nextnonce if the response have one + AuthenticationInfoHeader authenticationInfoHeader = (AuthenticationInfoHeader)response.getMessage().getHeader(AuthenticationInfoHeader.NAME);
+                       if(authenticationInfoHeader != null) {
+                               String nextNonce = 
authenticationInfoHeader.getNextNonce();
+                               if(logger.isDebugEnabled()) {
+ logger.debug("Storing nextNonce " + nextNonce + " for session " + key);
+                               }
+                               sipSessionSecurity.setNextNonce(nextNonce);
+                       }
+
+               }
// JSR 289 Section 6.2.1 Point 2 of rules governing the state of SipSession // In general, whenever a non-dialog creating request is sent or received,
                // the SipSession state remains unchanged. Similarly, a 
response received
@@ -2103,4 +2133,19 @@
        public boolean getCopyRecordRouteHeadersOnSubsequentResponses() {
                return copyRecordRouteHeadersOnSubsequentResponses;
        }
-}
+       /**
+        * @param sipSessionSecurity the sipSessionSecurity to set
+        */
+       public void setSipSessionSecurity(SipSessionSecurity 
sipSessionSecurity) {
+               this.sipSessionSecurity = sipSessionSecurity;
+       }
+       /**
+        * @return the sipSessionSecurity
+        */
+       public SipSessionSecurity getSipSessionSecurity() {
+               if(sipSessionSecurity == null) {
+                       sipSessionSecurity = new SipSessionSecurity();
+               }
+               return sipSessionSecurity;
+       }
+}
=======================================
--- /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/message/MobicentsSipSessionFacade.java Mon Dec 20 06:05:51 2010 +++ /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/message/MobicentsSipSessionFacade.java Mon Dec 20 13:46:40 2010
@@ -31,6 +31,7 @@
 import org.mobicents.servlet.sip.core.session.SipManager;
 import org.mobicents.servlet.sip.core.session.SipSessionEventType;
 import org.mobicents.servlet.sip.core.session.SipSessionKey;
+import org.mobicents.servlet.sip.core.session.SipSessionSecurity;
 import org.mobicents.servlet.sip.proxy.ProxyImpl;
 import org.mobicents.servlet.sip.startup.SipContext;
 import org.mobicents.servlet.sip.startup.StaticServiceHolder;
@@ -469,5 +470,13 @@
        public boolean getCopyRecordRouteHeadersOnSubsequentResponses() {
                return 
sipSession.getCopyRecordRouteHeadersOnSubsequentResponses();
        }
+
+       public SipSessionSecurity getSipSessionSecurity() {
+               return sipSession.getSipSessionSecurity();
+       }
+
+       public void setSipSessionSecurity(SipSessionSecurity 
sipSessionSecurity) {
+               sipSession.setSipSessionSecurity(sipSessionSecurity);
+       }

 }
=======================================
--- /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/message/SipServletRequestImpl.java Mon Dec 20 13:04:13 2010 +++ /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/message/SipServletRequestImpl.java Mon Dec 20 13:46:40 2010
@@ -29,10 +29,12 @@
 import java.io.ObjectOutput;
 import java.io.UnsupportedEncodingException;
 import java.text.ParseException;
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.ListIterator;
 import java.util.Locale;
 import java.util.Map;
@@ -366,7 +368,7 @@
 //                             response.addHeader(routeHeader);
 //                     }

- if(session.getCopyRecordRouteHeadersOnSubsequentResponses() && !isInitial() && requestMethod.equals(Request.INVITE)) { + if(session != null && session.getCopyRecordRouteHeadersOnSubsequentResponses() && !isInitial() && Request.INVITE.equals(requestMethod)) { // Miss Record-Route in Response for non compliant Server in reINVITE http://code.google.com/p/mobicents/issues/detail?id=2066 final ListIterator<RecordRouteHeader> recordRouteHeaders = request.getHeaders(RecordRouteHeader.NAME);
                                while (recordRouteHeaders.hasNext()) {
@@ -1506,6 +1508,24 @@
         */
        public void addAuthHeader(SipServletResponse challengeResponse,
                        AuthInfo authInfo) {
+               addAuthHeader(challengeResponse, authInfo, false);
+       }
+
+       /*
+        * (non-Javadoc)
+ * @see javax.servlet.sip.SipServletRequest#addAuthHeader(javax.servlet.sip.SipServletResponse, java.lang.String, java.lang.String)
+        */
+       public void addAuthHeader(SipServletResponse challengeResponse,
+                       String username, String password) {
+               addAuthHeader(challengeResponse, username, password, false);
+       }
+
+       /*
+        * (non-Javadoc)
+ * @see org.mobicents.javax.servlet.sip.SipServletRequestExt#addAuthHeader(javax.servlet.sip.SipServletResponse, javax.servlet.sip.AuthInfo, boolean)
+        */
+       public void addAuthHeader(SipServletResponse challengeResponse,
+                       AuthInfo authInfo, boolean cacheCredentials) {
                checkReadOnly();
                AuthInfoImpl authInfoImpl = (AuthInfoImpl) authInfo;

@@ -1533,6 +1553,10 @@
                                        authInfoEntry.getUserName(),
                                        authInfoEntry.getPassword(),
                                        this.getRequestURI().toString());
+
+                       if(cacheCredentials) {
+ getSipSession().getSipSessionSecurity().addCachedAuthInfo(wwwAuthHeader.getRealm(), authInfoEntry);
+                       }
                }

                // Now check for Proxy-Authentication
@@ -1555,16 +1579,19 @@
                                        authInfoEntry.getUserName(),
                                        authInfoEntry.getPassword(),
                                        this.getRequestURI().toString());
-               }
-
+
+                       if(cacheCredentials) {
+ getSipSession().getSipSessionSecurity().addCachedAuthInfo(proxyAuthHeader.getRealm(), authInfoEntry);
+                       }
+               }
        }

        /*
         * (non-Javadoc)
- * @see javax.servlet.sip.SipServletRequest#addAuthHeader(javax.servlet.sip.SipServletResponse, java.lang.String, java.lang.String) + * @see org.mobicents.javax.servlet.sip.SipServletRequestExt#addAuthHeader(javax.servlet.sip.SipServletResponse, java.lang.String, java.lang.String, boolean)
         */
        public void addAuthHeader(SipServletResponse challengeResponse,
-                       String username, String password) {
+                       String username, String password, boolean 
cacheCredentials) {
                checkReadOnly();
                SipServletResponseImpl challengeResponseImpl =
                        (SipServletResponseImpl) challengeResponse;
@@ -1583,6 +1610,10 @@
                        removeStaleAuthHeaders(wwwAuthHeader);
 //                     String uri = wwwAuthHeader.getParameter("uri");
addChallengeResponse(wwwAuthHeader, username, password, this.getRequestURI().toString());
+
+                       if(cacheCredentials) {
+ getSipSession().getSipSessionSecurity().addCachedAuthInfo(wwwAuthHeader.getRealm(), new AuthInfoEntry(response.getStatusCode(), username, password));
+                       }
                }


@@ -1599,6 +1630,10 @@
                        String uri = proxyAuthHeader.getParameter("uri");
                        if(uri == null) uri = this.getRequestURI().toString();
                        addChallengeResponse(proxyAuthHeader, username, 
password, uri);
+
+                       if(cacheCredentials) {
+ getSipSession().getSipSessionSecurity().addCachedAuthInfo(proxyAuthHeader.getRealm(), new AuthInfoEntry(response.getStatusCode(), username, password));
+                       }
                }
        }

@@ -1661,7 +1696,8 @@
                                "", // TODO: What is this entity-body?
                                wwwAuthHeader,
                                username,
-                               password);
+                               password,
+                               wwwAuthHeader.getNonce());

                message.addHeader(authorization);
        }
@@ -2039,4 +2075,73 @@
                out.writeBoolean(isFinalResponseGenerated);
                out.writeBoolean(is1xxResponseGenerated);
        }
-}
+
+       /**
+ * Added for Issue 2173 http://code.google.com/p/mobicents/issues/detail?id=2173 + * Handle Header [Authentication-Info: nextnonce="xyz"] in sip authorization responses
+        */
+       public void updateAuthorizationHeadersWithNextNonce() {
+               if(logger.isDebugEnabled()) {
+ logger.debug("Updating authorization headers with nextnonce " + getSipSession().getSipSessionSecurity().getNextNonce());
+               }
+               List<Header> authorizationHeaders = new ArrayList<Header>();
+
+               // First check for WWWAuthentication headers
+               ListIterator authHeaderIterator =
+                       message.getHeaders(AuthorizationHeader.NAME);
+               while(authHeaderIterator.hasNext()) {
+                       AuthorizationHeader wwwAuthHeader =
+                               (AuthorizationHeader) authHeaderIterator.next();
+ AuthInfoEntry authInfoEntry = getSipSession().getSipSessionSecurity().getCachedAuthInfos().get(wwwAuthHeader.getRealm());
+
+                       if(authInfoEntry != null) {
+
+ AuthorizationHeader authorization = DigestAuthenticator.getAuthorizationHeader(
+                                               getMethod(),
+                                               this.getRequestURI().toString(),
+                                               "", // TODO: What is this 
entity-body?
+                                               wwwAuthHeader,
+                                               authInfoEntry.getUserName(),
+                                               authInfoEntry.getPassword(),
+                                               
getSipSession().getSipSessionSecurity().getNextNonce());
+
+                               authorizationHeaders.add(authorization);
+                       } else {
+                               authorizationHeaders.add(wwwAuthHeader);
+                       }
+               }
+
+               // Now check for Proxy-Authentication
+               authHeaderIterator =
+                       message.getHeaders(ProxyAuthorizationHeader.NAME);
+               while(authHeaderIterator.hasNext()) {
+                       ProxyAuthorizationHeader proxyAuthHeader =
+                               (ProxyAuthorizationHeader) 
authHeaderIterator.next();
+//                     String uri = wwwAuthHeader.getParameter("uri");
+ AuthInfoEntry authInfoEntry = getSipSession().getSipSessionSecurity().getCachedAuthInfos().get(proxyAuthHeader.getRealm());
+
+                       if(authInfoEntry != null) {
+
+ AuthorizationHeader authorization = DigestAuthenticator.getAuthorizationHeader(
+                                               getMethod(),
+                                               this.getRequestURI().toString(),
+                                               "", // TODO: What is this 
entity-body?
+                                               proxyAuthHeader,
+                                               authInfoEntry.getUserName(),
+                                               authInfoEntry.getPassword(),
+                                               
getSipSession().getSipSessionSecurity().getNextNonce());
+
+                               authorizationHeaders.add(authorization);
+                       } else {
+                               authorizationHeaders.add(proxyAuthHeader);
+                       }
+               }
+
+               message.removeHeader(AuthorizationHeader.NAME);
+               message.removeHeader(ProxyAuthorizationHeader.NAME);
+
+               for(Header header : authorizationHeaders) {
+                       message.addHeader(header);
+               }
+       }
+}
=======================================
--- /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/security/AuthInfoEntry.java Tue Apr 15 16:44:58 2008 +++ /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/security/AuthInfoEntry.java Mon Dec 20 13:46:40 2010
@@ -49,4 +49,12 @@
        public void setStatusCode(int statusCode) {
                this.statusCode = statusCode;
        }
-}
+
+       @Override
+       public String toString() {
+               return "AuthInfoEntry [password=" + password + ", statusCode="
+                               + statusCode + ", userName=" + userName + "]";
+       }
+
+
+}
=======================================
--- /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/security/authentication/DigestAuthenticator.java Tue Jul 27 08:24:08 2010 +++ /trunk/servers/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/security/authentication/DigestAuthenticator.java Mon Dec 20 13:46:40 2010
@@ -24,8 +24,10 @@
 import java.util.StringTokenizer;

 import javax.sip.header.AuthorizationHeader;
+import javax.sip.header.Header;
 import javax.sip.header.HeaderFactory;
 import javax.sip.header.ProxyAuthenticateHeader;
+import javax.sip.header.ProxyAuthorizationHeader;
 import javax.sip.header.WWWAuthenticateHeader;

 import org.apache.catalina.Realm;
@@ -423,73 +425,102 @@
                 String                method,
                 String                uri,
                 String                requestBody,
-                WWWAuthenticateHeader authHeader,
+                Header                                   authHeader,
                 String                   username,
-                String                                   password)
+                String                                   password,
+                String                                   nonce)
     {
         String response = null;
         HeaderFactory headerFactory = SipFactories.headerFactory;
+        boolean isResponseHeader = true;
+        WWWAuthenticateHeader wwwAuthenticateHeader = null;
+        if(authHeader instanceof WWWAuthenticateHeader) {
+               wwwAuthenticateHeader = (WWWAuthenticateHeader) authHeader;
+        }
+
+        AuthorizationHeader authorizationHeader = null;
+        if(authHeader instanceof AuthorizationHeader) {
+               authorizationHeader =  (AuthorizationHeader) authHeader;
+               isResponseHeader = false;
+        }

         // JvB: authHeader.getQop() is a quoted _list_ of qop values
         // (e.g. "auth,auth-int") Client is supposed to pick one
-        String qopList = authHeader.getQop();
+        String qopList = null;
+        if(isResponseHeader) {
+               qopList = wwwAuthenticateHeader.getQop();
+        } else {
+               qopList = authorizationHeader.getQop();
+        }
+        String algorithm = null;
+        if(isResponseHeader) {
+               algorithm = wwwAuthenticateHeader.getAlgorithm();
+        } else {
+               algorithm = authorizationHeader.getAlgorithm();
+        }
+        String realm = null;
+        if(isResponseHeader) {
+               realm = wwwAuthenticateHeader.getRealm();
+        } else {
+               realm = authorizationHeader.getRealm();
+        }
+        String scheme = null;
+        if(isResponseHeader) {
+               scheme = wwwAuthenticateHeader.getScheme();
+        } else {
+               scheme = authorizationHeader.getScheme();
+        }
+        String opaque = null;
+        if(isResponseHeader) {
+               opaque = wwwAuthenticateHeader.getOpaque();
+        } else {
+               opaque = authorizationHeader.getOpaque();
+        }
         String qop = (qopList != null) ? "auth" : null;
         String nc_value = "00000001";
         String cnonce = "xyz";

-        try
-        {
+        try {
             response = MessageDigestResponseAlgorithm.calculateResponse(
-                authHeader.getAlgorithm(),
+                algorithm,
                 username,
-                authHeader.getRealm(),
+                realm,
                 password,
-                authHeader.getNonce(),
+                nonce,
                 nc_value, // JvB added
                 cnonce,   // JvB added
                 method,
                 uri,
                 requestBody,
                 qop);//jvb changed
-        }
-        catch (NullPointerException exc)
-        {
+        } catch (NullPointerException exc) {
             throw new IllegalStateException(
                 "The authenticate header was malformatted", exc);
         }

         AuthorizationHeader authorization = null;
-        try
-        {
-            if (authHeader instanceof ProxyAuthenticateHeader)
-            {
- authorization = headerFactory.createProxyAuthorizationHeader(
-                    authHeader.getScheme());
-            }
-            else
-            {
-                authorization = headerFactory.createAuthorizationHeader(
-                    authHeader.getScheme());
+        try {
+ if (authHeader instanceof ProxyAuthenticateHeader || authHeader instanceof ProxyAuthorizationHeader) { + authorization = headerFactory.createProxyAuthorizationHeader(scheme);
+            } else {
+ authorization = headerFactory.createAuthorizationHeader(scheme);
             }

             authorization.setUsername(username);
-            authorization.setRealm(authHeader.getRealm());
-            authorization.setNonce(authHeader.getNonce());
+            authorization.setRealm(realm);
+            authorization.setNonce(nonce);
             authorization.setParameter("uri", uri);
             authorization.setResponse(response);
-            if (authHeader.getAlgorithm() != null)
-            {
-                authorization.setAlgorithm(authHeader.getAlgorithm());
+            if (algorithm != null) {
+                authorization.setAlgorithm(algorithm);
             }

- if (authHeader.getOpaque() != null && authHeader.getOpaque().length() > 0)
-            {
-                authorization.setOpaque(authHeader.getOpaque());
+            if (opaque != null && opaque.length() > 0) {
+                authorization.setOpaque(opaque);
             }

             // jvb added
-            if (qop!=null)
-            {
+            if (qop!=null) {
                 authorization.setQop(qop);
                 authorization.setCNonce(cnonce);
                 authorization.setNonceCount( Integer.parseInt(nc_value) );
@@ -498,8 +529,7 @@
             authorization.setResponse(response);

         }
-        catch (ParseException ex)
-        {
+        catch (ParseException ex) {
             throw new SecurityException(
                 "Failed to create an authorization header!", ex);
         }

Reply via email to