drools-eclipse/org.guvnor.eclipse.webdav/src/client/org/eclipse/webdav/internal/authentication/AuthorizationAuthority.java
public boolean authorize(Request request, IResponse response, IContext context, URL proxyServerUrl, boolean isProxyAuthorization) {
        Assert.isNotNull(request);
        Assert.isNotNull(context);

        URL serverUrl = null;
        URL protectionSpaceUrl = null;

        if (isProxyAuthorization) {
            if (proxyServerUrl == null) {
                return false;
            }
            serverUrl = proxyServerUrl;
            protectionSpaceUrl = proxyServerUrl;
        } else {
            URL resourceUrl = request.getResourceUrl();
            try {
                serverUrl = new URL(resourceUrl.getProtocol(), resourceUrl.getHost(), resourceUrl.getPort(), "/"); //$NON-NLS-1$
            } catch (MalformedURLException e) {
                return false;
            }
            protectionSpaceUrl = resourceUrl;
        }

        if (response != null) {
            String challengeString = null;
            if (isProxyAuthorization) {
                challengeString = response.getContext().getProxyAuthenticate();
            } else {
                challengeString = response.getContext().getWWWAuthenticate();
            }

            if (challengeString == null) {
                return false;
            }

            AuthenticateChallenge challenge = null;
            try {
                challenge = new AuthenticateChallenge(challengeString);
            } catch (ParserException e) {
                return false;
            }

            String authScheme = challenge.getAuthScheme();
            String realm = challenge.getRealm();

            AuthorizationAuthority authority = getAuthorizationAuthority(authScheme);
            if (authority == null) {
                return false;
            }

            Map oldInfo = authenticatorStore.getAuthenticationInfo(serverUrl, realm, authScheme);
            Map info = authority.getAuthenticationInfo(challenge, oldInfo, serverUrl, protectionSpaceUrl);
            if (info == null) {
                return false;
            }

            authenticatorStore.addAuthenticationInfo(serverUrl, realm, authScheme, info);
            authenticatorStore.addProtectionSpace(protectionSpaceUrl, realm);
        }

        String realm = authenticatorStore.getProtectionSpace(protectionSpaceUrl);
        if (realm == null) {
            return false;
        }

        Map info = null;
        String authScheme = null;

        for (int i = 0; i < authenticationSchemes.length; ++i) {
            authScheme = authenticationSchemes[i];
            info = authenticatorStore.getAuthenticationInfo(serverUrl, realm, authScheme);
            if (info != null) {
                break;
            }
        }

        if (info == null) {
            return false;
        }

        AuthorizationAuthority authority = getAuthorizationAuthority(authScheme);
        if (authority == null) {
            return false;
        }

        String authorization = authority.getAuthorization(request, info, serverUrl, protectionSpaceUrl, proxyServerUrl);
        if (authorization == null) {
            return false;
        }

        if (isProxyAuthorization) {
            if (authorization.equals(context.getProxyAuthorization()))
                return false; // we already had that auth so it must've failed
            context.setProxyAuthorization(authorization);
        } else {
            if (authorization.equals(context.getAuthorization()))
                return false; // we already had that auth so it must've failed
            context.setAuthorization(authorization);
        }

        return true;
    }
public boolean authorize(Request request, IResponse response, IContext context, URL proxyServerUrl, boolean isProxyAuthorization) {
        Assert.isNotNull(request);
        Assert.isNotNull(context);

        URL serverUrl = null;
        URL protectionSpaceUrl = null;

        if (isProxyAuthorization) {
            if (proxyServerUrl == null) {
                return false;
            }
            serverUrl = proxyServerUrl;
            protectionSpaceUrl = proxyServerUrl;
        } else {
            URL resourceUrl = request.getResourceUrl();
            try {
                serverUrl = new URL(resourceUrl.getProtocol(), resourceUrl.getHost(), resourceUrl.getPort(), "/"); //$NON-NLS-1$
            } catch (MalformedURLException e) {
                return false;
            }
            protectionSpaceUrl = resourceUrl;
        }

        if (response != null) {
            String challengeString = null;
            if (isProxyAuthorization) {
                challengeString = response.getContext().getProxyAuthenticate();
            } else {
                challengeString = response.getContext().getWWWAuthenticate();
            }

            if (challengeString == null) {
                return false;
            }

            AuthenticateChallenge challenge = null;
            try {
                challenge = new AuthenticateChallenge(challengeString);
            } catch (ParserException e) {
                return false;
            }

            String authScheme = challenge.getAuthScheme();
            String realm = challenge.getRealm();

            AuthorizationAuthority authority = getAuthorizationAuthority(authScheme);
            if (authority == null) {
                return false;
            }

            Map oldInfo = authenticatorStore.getAuthenticationInfo(serverUrl, realm, authScheme);
            Map info = authority.getAuthenticationInfo(challenge, oldInfo, serverUrl, protectionSpaceUrl);
            if (info == null) {
                return false;
            }

            authenticatorStore.addAuthenticationInfo(serverUrl, realm, authScheme, info);
            authenticatorStore.addProtectionSpace(protectionSpaceUrl, realm);
        }

        String realm = authenticatorStore.getProtectionSpace(protectionSpaceUrl);
        if (realm == null) {
            return false;
        }

        Map info = null;
        String authScheme = null;

        for (int i = 0; i < authenticationSchemes.length; ++i) {
            authScheme = authenticationSchemes[i];
            info = authenticatorStore.getAuthenticationInfo(serverUrl, realm, authScheme);
            if (info != null) {
                break;
            }
        }

        if (info == null) {
            return false;
        }

        AuthorizationAuthority authority = getAuthorizationAuthority(authScheme);
        if (authority == null) {
            return false;
        }

        String authorization = authority.getAuthorization(request, info, serverUrl, protectionSpaceUrl, proxyServerUrl);
        if (authorization == null) {
            return false;
        }

        if (isProxyAuthorization) {
            if (authorization.equals(context.getProxyAuthorization()))
                return false; // we already had that auth so it must've failed
            context.setProxyAuthorization(authorization);
        } else {
            if (authorization.equals(context.getAuthorization()))
                return false; // we already had that auth so it must've failed
            context.setAuthorization(authorization);
        }

        return true;
    }
public boolean authorize ( Request request , IResponse response , IContext context , URL proxyServerUrl , boolean isProxyAuthorization ) { Assert . isNotNull ( request ) ; Assert . isNotNull ( context ) ; URL serverUrl = null ; URL protectionSpaceUrl = null ; if ( isProxyAuthorization ) { if ( proxyServerUrl == null ) { return false ; } serverUrl = proxyServerUrl ; protectionSpaceUrl = proxyServerUrl ; } else { URL resourceUrl = request . getResourceUrl ( ) ; try { serverUrl = new URL ( resourceUrl . getProtocol ( ) , resourceUrl . getHost ( ) , resourceUrl . getPort ( ) , "/" ) ; //$NON-NLS-1$ } catch ( MalformedURLException e ) { return false ; } protectionSpaceUrl = resourceUrl ; } if ( response != null ) { String challengeString = null ; if ( isProxyAuthorization ) { challengeString = response . getContext ( ) . getProxyAuthenticate ( ) ; } else { challengeString = response . getContext ( ) . getWWWAuthenticate ( ) ; } if ( challengeString == null ) { return false ; } AuthenticateChallenge challenge = null ; try { challenge = new AuthenticateChallenge ( challengeString ) ; } catch ( ParserException e ) { return false ; } String authScheme = challenge . getAuthScheme ( ) ; String realm = challenge . getRealm ( ) ; AuthorizationAuthority authority = getAuthorizationAuthority ( authScheme ) ; if ( authority == null ) { return false ; } Map oldInfo = authenticatorStore . getAuthenticationInfo ( serverUrl , realm , authScheme ) ; Map info = authority . getAuthenticationInfo ( challenge , oldInfo , serverUrl , protectionSpaceUrl ) ; if ( info == null ) { return false ; } authenticatorStore . addAuthenticationInfo ( serverUrl , realm , authScheme , info ) ; authenticatorStore . addProtectionSpace ( protectionSpaceUrl , realm ) ; } String realm = authenticatorStore . getProtectionSpace ( protectionSpaceUrl ) ; if ( realm == null ) { return false ; } Map info = null ; String authScheme = null ; for ( int i = 0 ; i < authenticationSchemes . length ; ++ i ) { authScheme = authenticationSchemes [ i ] ; info = authenticatorStore . getAuthenticationInfo ( serverUrl , realm , authScheme ) ; if ( info != null ) { break ; } } if ( info == null ) { return false ; } AuthorizationAuthority authority = getAuthorizationAuthority ( authScheme ) ; if ( authority == null ) { return false ; } String authorization = authority . getAuthorization ( request , info , serverUrl , protectionSpaceUrl , proxyServerUrl ) ; if ( authorization == null ) { return false ; } if ( isProxyAuthorization ) { if ( authorization . equals ( context . getProxyAuthorization ( ) ) ) return false ; // we already had that auth so it must've failed context . setProxyAuthorization ( authorization ) ; } else { if ( authorization . equals ( context . getAuthorization ( ) ) ) return false ; // we already had that auth so it must've failed context . setAuthorization ( authorization ) ; } return true ; }
Authorizes the given request by setting its authorization credentials
in the given context. If the given response is not <code>null</code>,
it is assumed to contain an authenticate challenge that is used to
derive the authorization credentials. Returns true if the authorization
succeeds, and false otherwise.

@param request the request to authorize
@param response the response containing the authenticate challenge
@param context the context where the authorization credentials are set
@param proxyServerUrl the URL of the proxy server, or <code>null</code>
if there is no proxy server
@param isProxyAuthorization a boolean indicating whether the
authorization credentials should be computed for the proxy server or
the origin server
@return a boolean indicating whether the request was successfully
authorized
Authorizes the given request by setting its authorization credentials in the given context . If the given response is not <code > null< / code > it is assumed to contain an authenticate challenge that is used to derive the authorization credentials . Returns true if the authorization succeeds and false otherwise .