protected static SipPrincipal findPrincipal(MobicentsSipServletRequest request,
                                             String authorization,
                                             Realm realm,
                                             String securityDomain) {

        //System.out.println("Authorization token : " + authorization);
        // Validate the authorization credentials format    	
        if (authorization == null)
            return (null);
        if (!authorization.startsWith("Digest "))
            return (null);
        String tmpAuthorization = authorization.substring(7).trim();

        // Bugzilla 37132: http://issues.apache.org/bugzilla/show_bug.cgi?id=37132
        // The solution of 37132 doesn't work with : response="2d05f1206becab904c1f311f205b405b",cnonce="5644k1k670",username="admin",nc=00000001,qop=auth,nonce="b6c73ab509830b8c0897984f6b0526e8",realm="sip-servlets-realm",opaque="9ed6d115d11f505f9ee20f6a68654cc2",uri="sip:192.168.1.142",algorithm=MD5
        // That's why I am going back to simple comma (Vladimir). TODO: Review this.
        String[] tokens = tmpAuthorization.split(",");//(?=(?:[^\"]*\"[^\"]*\")+$)");

        String userName = null;
        String realmName = null;
        String nOnce = null;
        String nc = null;
        String cnonce = null;
        String qop = null;
        String uri = null;
        String response = null;
        String method = request.getMethod();

        for (int i = 0; i < tokens.length; i++) {
            String currentToken = tokens[i];
            if (currentToken.length() == 0)
                continue;

            int equalSign = currentToken.indexOf('=');
            if (equalSign < 0)
                return null;
            String currentTokenName =
                currentToken.substring(0, equalSign).trim();
            String currentTokenValue =
                currentToken.substring(equalSign + 1).trim();
            if ("username".equals(currentTokenName))
                userName = removeQuotes(currentTokenValue);
            if ("realm".equals(currentTokenName))
                realmName = removeQuotes(currentTokenValue, true);
            if ("nonce".equals(currentTokenName))
                nOnce = removeQuotes(currentTokenValue);
            if ("nc".equals(currentTokenName))
                nc = removeQuotes(currentTokenValue);
            if ("cnonce".equals(currentTokenName))
                cnonce = removeQuotes(currentTokenValue);
            if ("qop".equals(currentTokenName))
                qop = removeQuotes(currentTokenValue);
            if ("uri".equals(currentTokenName))
                uri = removeQuotes(currentTokenValue);
            if ("response".equals(currentTokenName))
                response = removeQuotes(currentTokenValue);
        }

        if ( (userName == null) || (realmName == null) || (nOnce == null)
             || (uri == null) || (response == null) )
            return null;

        // Second MD5 digest used to calculate the digest :
        // MD5(Method + ":" + uri)
        String a2 = method + ":" + uri;
        //System.out.println("A2:" + a2);

        byte[] buffer = null;
        synchronized (md5Helper) {
            buffer = md5Helper.digest(a2.getBytes());
        }
        String md5a2 = MD5_ENCODER.encode(buffer);

        //taken from https://github.com/jbossas/jboss-as/blob/7.1.2.Final/web/src/main/java/org/jboss/as/web/security/SecurityContextAssociationValve.java#L86
        SecurityContext sc = SecurityActions.getSecurityContext();
        if (sc == null) {
        	if (log.isDebugEnabled()) {
                log.debug("Security Domain " + securityDomain + " for Realm " + realmName);
        	}
        	if (securityDomain == null) {
        		if (log.isDebugEnabled()) {
                    log.debug("Security Domain is null using default security domain " + SIPSecurityConstants.DEFAULT_SIP_APPLICATION_POLICY + " for Realm " + realmName);
            	}
                securityDomain = SIPSecurityConstants.DEFAULT_SIP_APPLICATION_POLICY;
        	}
            sc = SecurityActions.createSecurityContext(securityDomain);
            SecurityActions.setSecurityContextOnAssociation(sc);
        }
        
        try {
        	return (new CatalinaSipPrincipal(realm.authenticate(userName, response, nOnce, nc, cnonce, qop, realmName, md5a2)));
        } finally {        
            SecurityActions.clearSecurityContext();
            SecurityRolesAssociation.setSecurityRoles(null);
        }

    }
protected static SipPrincipal findPrincipal ( MobicentsSipServletRequest request , String authorization , Realm realm , String securityDomain ) { if ( authorization == null ) return ( null ) ; if ( ! authorization . startsWith ( "Digest " ) ) return ( null ) ; String tmpAuthorization = authorization . substring ( 7 ) . trim ( ) ; String [ ] tokens = tmpAuthorization . split ( "," ) ; String userName = null ; String realmName = null ; String nOnce = null ; String nc = null ; String cnonce = null ; String qop = null ; String uri = null ; String response = null ; String method = request . getMethod ( ) ; for ( int i = 0 ; i < tokens . length ; i ++ ) { String currentToken = tokens [ i ] ; if ( currentToken . length ( ) == 0 ) continue ; int equalSign = currentToken . indexOf ( '=' ) ; if ( equalSign < 0 ) return null ; String currentTokenName = currentToken . substring ( 0 , equalSign ) . trim ( ) ; String currentTokenValue = currentToken . substring ( equalSign + 1 ) . trim ( ) ; if ( "username" . equals ( currentTokenName ) ) userName = removeQuotes ( currentTokenValue ) ; if ( "realm" . equals ( currentTokenName ) ) realmName = removeQuotes ( currentTokenValue , true ) ; if ( "nonce" . equals ( currentTokenName ) ) nOnce = removeQuotes ( currentTokenValue ) ; if ( "nc" . equals ( currentTokenName ) ) nc = removeQuotes ( currentTokenValue ) ; if ( "cnonce" . equals ( currentTokenName ) ) cnonce = removeQuotes ( currentTokenValue ) ; if ( "qop" . equals ( currentTokenName ) ) qop = removeQuotes ( currentTokenValue ) ; if ( "uri" . equals ( currentTokenName ) ) uri = removeQuotes ( currentTokenValue ) ; if ( "response" . equals ( currentTokenName ) ) response = removeQuotes ( currentTokenValue ) ; } if ( ( userName == null ) || ( realmName == null ) || ( nOnce == null ) || ( uri == null ) || ( response == null ) ) return null ; String a2 = method + ":" + uri ; byte [ ] buffer = null ; synchronized ( md5Helper ) { buffer = md5Helper . digest ( a2 . getBytes ( ) ) ; } String md5a2 = MD5_ENCODER . encode ( buffer ) ; SecurityContext sc = SecurityActions . getSecurityContext ( ) ; if ( sc == null ) { if ( log . isDebugEnabled ( ) ) { log . debug ( "Security Domain " + securityDomain + " for Realm " + realmName ) ; } if ( securityDomain == null ) { if ( log . isDebugEnabled ( ) ) { log . debug ( "Security Domain is null using default security domain " + SIPSecurityConstants . DEFAULT_SIP_APPLICATION_POLICY + " for Realm " + realmName ) ; } securityDomain = SIPSecurityConstants . DEFAULT_SIP_APPLICATION_POLICY ; } sc = SecurityActions . createSecurityContext ( securityDomain ) ; SecurityActions . setSecurityContextOnAssociation ( sc ) ; } try { return ( new CatalinaSipPrincipal ( realm . authenticate ( userName , response , nOnce , nc , cnonce , qop , realmName , md5a2 ) ) ) ; } finally { SecurityActions . clearSecurityContext ( ) ; SecurityRolesAssociation . setSecurityRoles ( null ) ; } }
Parse the specified authorization credentials, and return the
associated Principal that these credentials authenticate (if any)
from the specified Realm.  If there is no such Principal, return
<code>null</code>.

@param request HTTP servlet request
@param authorization Authorization credentials from this request
@param realm Realm used to authenticate Principals