private void doPreemptiveAuth( final HttpHost host, final AuthScheme authScheme, final AuthStateHC4 authState, final CredentialsProvider credsProvider) { final String schemeName = authScheme.getSchemeName(); if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Re-using cached '" + schemeName + "' auth scheme for " + host); } final AuthScope authScope = new AuthScope(host.getHostName(), host.getPort(), AuthScope.ANY_REALM, schemeName); final Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { if ("BASIC".equalsIgnoreCase(authScheme.getSchemeName())) { authState.setState(AuthProtocolState.CHALLENGED); } else { authState.setState(AuthProtocolState.SUCCESS); } authState.update(authScheme, creds); } else { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "No credentials for preemptive authentication"); } } }
/** * @since 4.3 */ public ProxyClient( final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory, final ConnectionConfig connectionConfig, final RequestConfig requestConfig) { super(); this.connFactory = connFactory != null ? connFactory : ManagedHttpClientConnectionFactory.INSTANCE; this.connectionConfig = connectionConfig != null ? connectionConfig : ConnectionConfig.DEFAULT; this.requestConfig = requestConfig != null ? requestConfig : RequestConfig.DEFAULT; this.httpProcessor = new ImmutableHttpProcessor( new RequestTargetHostHC4(), new RequestClientConnControl(), new RequestUserAgentHC4()); this.requestExec = new HttpRequestExecutor(); this.proxyAuthStrategy = new ProxyAuthenticationStrategy(); this.authenticator = new HttpAuthenticator(); this.proxyAuthState = new AuthStateHC4(); this.authSchemeRegistry = new AuthSchemeRegistry(); this.authSchemeRegistry.register(AuthSchemes.BASIC, new BasicSchemeFactoryHC4()); this.authSchemeRegistry.register(AuthSchemes.DIGEST, new DigestSchemeFactoryHC4()); this.authSchemeRegistry.register(AuthSchemes.NTLM, new NTLMSchemeFactory()); this.reuseStrategy = new DefaultConnectionReuseStrategyHC4(); }
private void setupContext(final HttpClientContext context) { if (context.getAttribute(HttpClientContext.TARGET_AUTH_STATE) == null) { context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, new AuthStateHC4()); } if (context.getAttribute(HttpClientContext.PROXY_AUTH_STATE) == null) { context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, new AuthStateHC4()); } if (context.getAttribute(HttpClientContext.AUTHSCHEME_REGISTRY) == null) { context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry); } if (context.getAttribute(HttpClientContext.COOKIESPEC_REGISTRY) == null) { context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); } if (context.getAttribute(HttpClientContext.COOKIE_STORE) == null) { context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); } if (context.getAttribute(HttpClientContext.CREDS_PROVIDER) == null) { context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider); } if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) { context.setAttribute(HttpClientContext.REQUEST_CONFIG, this.defaultConfig); } }
public Object getUserToken(final HttpContext context) { final HttpClientContext clientContext = HttpClientContext.adapt(context); Principal userPrincipal = null; final AuthStateHC4 targetAuthState = clientContext.getTargetAuthState(); if (targetAuthState != null) { userPrincipal = getAuthPrincipal(targetAuthState); if (userPrincipal == null) { final AuthStateHC4 proxyAuthState = clientContext.getProxyAuthState(); userPrincipal = getAuthPrincipal(proxyAuthState); } } if (userPrincipal == null) { final HttpConnection conn = clientContext.getConnection(); if (conn.isOpen() && conn instanceof ManagedHttpClientConnection) { final SSLSession sslsession = ((ManagedHttpClientConnection) conn).getSSLSession(); if (sslsession != null) { userPrincipal = sslsession.getLocalPrincipal(); } } } return userPrincipal; }
public boolean isAuthenticationRequested( final HttpHost host, final HttpResponse response, final AuthenticationStrategy authStrategy, final AuthStateHC4 authState, final HttpContext context) { if (authStrategy.isAuthenticationRequested(host, response, context)) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Authentication required"); } if (authState.getState() == AuthProtocolState.SUCCESS) { authStrategy.authFailed(host, authState.getAuthScheme(), context); } return true; } else { switch (authState.getState()) { case CHALLENGED: case HANDSHAKE: if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Authentication succeeded"); } authState.setState(AuthProtocolState.SUCCESS); authStrategy.authSucceeded(host, authState.getAuthScheme(), context); break; case SUCCESS: break; default: authState.setState(AuthProtocolState.UNCHALLENGED); } return false; } }
private boolean needAuthentication( final AuthStateHC4 targetAuthState, final AuthStateHC4 proxyAuthState, final HttpRoute route, final HttpResponse response, final HttpClientContext context) { final RequestConfig config = context.getRequestConfig(); if (config.isAuthenticationEnabled()) { HttpHost target = context.getTargetHost(); if (target == null) { target = route.getTargetHost(); } if (target.getPort() < 0) { target = new HttpHost( target.getHostName(), route.getTargetHost().getPort(), target.getSchemeName()); } final boolean targetAuthRequested = this.authenticator.isAuthenticationRequested( target, response, this.targetAuthStrategy, targetAuthState, context); HttpHost proxy = route.getProxyHost(); // if proxy is not set use target host instead if (proxy == null) { proxy = route.getTargetHost(); } final boolean proxyAuthRequested = this.authenticator.isAuthenticationRequested( proxy, response, this.proxyAuthStrategy, proxyAuthState, context); if (targetAuthRequested) { return this.authenticator.handleAuthChallenge(target, response, this.targetAuthStrategy, targetAuthState, context); } if (proxyAuthRequested) { return this.authenticator.handleAuthChallenge(proxy, response, this.proxyAuthStrategy, proxyAuthState, context); } } return false; }
private static Principal getAuthPrincipal(final AuthStateHC4 authState) { final AuthScheme scheme = authState.getAuthScheme(); if (scheme != null && scheme.isComplete() && scheme.isConnectionBased()) { final Credentials creds = authState.getCredentials(); if (creds != null) { return creds.getUserPrincipal(); } } return null; }
public AuthStateHC4 getTargetAuthState() { return getAttribute(TARGET_AUTH_STATE, AuthStateHC4.class); }
public AuthStateHC4 getProxyAuthState() { return getAttribute(PROXY_AUTH_STATE, AuthStateHC4.class); }