final CredentialsProvider credsProvider) { if (!authState.isValid()) { return; String hostname = host.getHostName(); int port = host.getPort(); if (port < 0) { Scheme scheme = connManager.getSchemeRegistry().getScheme(host); AuthScheme authScheme = authState.getAuthScheme(); AuthScope authScope = new AuthScope( hostname, port, authScheme.getRealm(), authScheme.getSchemeName()); Credentials creds = authState.getCredentials(); if (creds == null) { creds = credsProvider.getCredentials(authScope); if (authScheme.isComplete()) { this.log.debug("Authentication failed"); creds = null;
if (request.containsHeader(AUTH.PROXY_AUTH_RESP)) { return; AuthState authState = (AuthState) context.getAttribute( ClientContext.PROXY_AUTH_STATE); if (authState == null) { AuthScheme authScheme = authState.getAuthScheme(); if (authScheme == null) { return; Credentials creds = authState.getCredentials(); if (creds == null) { this.log.debug("User credentials not available"); return; if (authState.getAuthScope() != null || !authScheme.isConnectionBased()) { try { request.addHeader(authScheme.authenticate(creds, request)); } catch (AuthenticationException ex) { if (this.log.isErrorEnabled()) { this.log.error("Proxy authentication error: " + ex.getMessage());
private void processChallenges( final Map<String, Header> challenges, final AuthState authState, final AuthenticationHandler authHandler, final HttpResponse response, final HttpContext context) throws MalformedChallengeException, AuthenticationException { AuthScheme authScheme = authState.getAuthScheme(); if (authScheme == null) { // Authentication not attempted before authScheme = authHandler.selectScheme(challenges, response, context); authState.setAuthScheme(authScheme); } String id = authScheme.getSchemeName(); Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH)); if (challenge == null) { throw new AuthenticationException(id + " authorization challenge expected, but not found"); } authScheme.processChallenge(challenge); this.log.debug("Authorization challenge processed"); }
private static Principal getAuthPrincipal(final AuthState authState) { AuthScheme scheme = authState.getAuthScheme(); if (scheme != null && scheme.isComplete() && scheme.isConnectionBased()) { Credentials creds = authState.getCredentials(); if (creds != null) { return creds.getUserPrincipal(); } } return null; }
HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (target == null) { target = route.getTargetHost(); if (target.getPort() < 0) { final Scheme scheme = connManager.getSchemeRegistry().getScheme(target); target = new HttpHost(target.getHostName(), scheme.getDefaultPort(), target.getSchemeName()); target, response, this.targetAuthStrategy, targetAuthState, context); HttpHost proxy = route.getProxyHost(); proxy = route.getTargetHost(); redirect.setHeaders(orig.getAllHeaders()); if (!route.getTargetHost().equals(newTarget)) { this.log.debug("Resetting target auth state"); targetAuthState.reset(); final AuthScheme authScheme = proxyAuthState.getAuthScheme(); if (authScheme != null && authScheme.isConnectionBased()) { this.log.debug("Resetting proxy auth state"); proxyAuthState.reset();
HttpHost newTarget = new HttpHost( uri.getHost(), uri.getPort(), targetAuthState.setAuthScope(null); proxyAuthState.setAuthScope(null); if (!route.getTargetHost().equals(newTarget)) { targetAuthState.invalidate(); AuthScheme authScheme = proxyAuthState.getAuthScheme(); if (authScheme != null && authScheme.isConnectionBased()) { proxyAuthState.invalidate(); redirect.setHeaders(orig.getAllHeaders()); context.getAttribute(ClientContext.CREDS_PROVIDER); context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (target == null) { target = route.getTargetHost(); } catch (AuthenticationException ex) { if (this.log.isWarnEnabled()) { this.log.warn("Authentication error: " + ex.getMessage()); return null; HttpHost proxy = route.getProxyHost();
final HttpResponse response, final HttpContext context) throws MalformedChallengeException { Args.notNull(challenges, "Map of auth challenges"); Args.notNull(authhost, "Host"); Args.notNull(response, "HTTP response"); Args.notNull(context, "HTTP context"); final CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute( ClientContext.CREDS_PROVIDER); if (credsProvider == null) { } catch (final AuthenticationException ex) { if (this.log.isWarnEnabled()) { this.log.warn(ex.getMessage(), ex); final String id = authScheme.getSchemeName(); final Header challenge = challenges.get(id.toLowerCase(Locale.ROOT)); authScheme.processChallenge(challenge); authhost.getHostName(), authhost.getPort(), authScheme.getRealm(), authScheme.getSchemeName());
final HttpResponse response, final HttpContext context) throws MalformedChallengeException { Args.notNull(challenges, "Map of auth challenges"); Args.notNull(authhost, "Host"); Args.notNull(response, "HTTP response"); Args.notNull(context, "HTTP context"); final HttpClientContext clientContext = HttpClientContext.adapt(context); final Lookup<AuthSchemeProvider> registry = clientContext.getAuthSchemeRegistry(); if (registry == null) { this.log.debug("Auth scheme registry not set in the context"); return options; final CredentialsProvider credsProvider = clientContext.getCredentialsProvider(); if (credsProvider == null) { this.log.debug("Credentials provider not set in the context"); authScheme.processChallenge(challenge); authhost.getHostName(), authhost.getPort(), authScheme.getRealm(), authScheme.getSchemeName());
throws HttpException, IOException { HttpHost proxy = route.getProxyHost(); HttpHost target = route.getTargetHost(); HttpResponse response = null; connect.addHeader(HTTP.USER_AGENT, agent); connect.addHeader(HTTP.TARGET_HOST, target.toHostString()); AuthScheme authScheme = this.proxyAuthState.getAuthScheme(); AuthScope authScope = this.proxyAuthState.getAuthScope(); Credentials creds = this.proxyAuthState.getCredentials(); if (creds != null) { if (authScope != null || !authScheme.isConnectionBased()) { try { connect.addHeader(authScheme.authenticate(creds, connect)); } catch (AuthenticationException ex) { if (this.log.isErrorEnabled()) { this.log.error("Proxy authentication error: " + ex.getMessage()); context.getAttribute(ClientContext.CREDS_PROVIDER); } catch (AuthenticationException ex) { if (this.log.isWarnEnabled()) { this.log.warn("Authentication error: " + ex.getMessage()); break;
final HttpClientContext context, final HttpExecutionAware execAware) throws IOException, HttpException { Args.notNull(route, "HTTP route"); Args.notNull(request, "HTTP request"); Args.notNull(context, "HTTP context"); final List<URI> redirectLocations = context.getRedirectLocations(); if (redirectLocations != null) { redirectLocations.clear(); final RequestConfig config = context.getRequestConfig(); final int maxRedirects = config.getMaxRedirects() > 0 ? config.getMaxRedirects() : 50; HttpRoute currentRoute = route; if (!redirect.headerIterator().hasNext()) { final HttpRequest original = request.getOriginal(); redirect.setHeaders(original.getAllHeaders()); final AuthState proxyAuthState = context.getProxyAuthState(); if (proxyAuthState != null) { final AuthScheme authScheme = proxyAuthState.getAuthScheme(); if (authScheme != null && authScheme.isConnectionBased()) { this.log.debug("Resetting proxy auth state"); proxyAuthState.reset();
/** * If no auth scheme has been selected for the given context, consider each * of the preferred auth schemes and select the first one for which an * AuthScheme and matching Credentials are available. */ public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState != null && authState.getAuthScheme() != null) { return; } HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); CredentialsProvider creds = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER); AuthSchemeRegistry schemes = (AuthSchemeRegistry) context.getAttribute(ClientContext.AUTHSCHEME_REGISTRY); for (Object schemeName : (Iterable) context.getAttribute(ClientContext.AUTH_SCHEME_PREF)) { AuthScheme scheme = schemes.getAuthScheme(schemeName.toString(), request.getParams()); if (scheme != null) { AuthScope targetScope = new AuthScope(target.getHostName(), target.getPort(), scheme.getRealm(), scheme .getSchemeName()); Credentials cred = creds.getCredentials(targetScope); if (cred != null) { authState.setAuthScheme(scheme); authState.setCredentials(cred); return; } } } } }
public void authSucceeded( final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) { AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); if (isCachable(authScheme)) { if (authCache == null) { authCache = new BasicAuthCache(); context.setAttribute(ClientContext.AUTH_CACHE, authCache); } if (this.log.isDebugEnabled()) { this.log.debug("Caching '" + authScheme.getSchemeName() + "' auth scheme for " + authhost); } authCache.put(authhost, authScheme); } }
public void authSucceeded( final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) { Args.notNull(authhost, "Host"); Args.notNull(authScheme, "Auth scheme"); Args.notNull(context, "HTTP context"); final HttpClientContext clientContext = HttpClientContext.adapt(context); if (isCachable(authScheme)) { AuthCache authCache = clientContext.getAuthCache(); if (authCache == null) { authCache = new BasicAuthCache(); clientContext.setAuthCache(authCache); } if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Caching '" + authScheme.getSchemeName() + "' auth scheme for " + authhost); } authCache.put(authhost, authScheme); } }
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { if (request == null) throw new IllegalArgumentException("HTTP request may not be null"); if (context == null) throw new IllegalArgumentException("HTTP context may not be null"); if (!request.getRequestLine().getMethod().equalsIgnoreCase("CONNECT")) { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState != null) { AuthScheme authScheme = authState.getAuthScheme(); if (authScheme != null && !authScheme.isConnectionBased()) { try { request.setHeader(authScheme.authenticate(null, request)); } catch (AuthenticationException ignored) { // ignored } } } } } }
public void authFailed( final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) { final AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); if (authCache == null) { return; } if (this.log.isDebugEnabled()) { this.log.debug("Removing from cache '" + authScheme.getSchemeName() + "' auth scheme for " + authhost); } authCache.remove(authhost); }
private static boolean isProxyType(HttpContext context, String scheme) { if (context == null) return false; AuthState authState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE); if (authState == null) return false; AuthScheme authScheme = authState.getAuthScheme(); if (authScheme == null) return false; String schemeName = authScheme.getSchemeName(); if (schemeName == null) return false; return schemeName.equalsIgnoreCase(scheme); }
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"); } } }
final AuthState authState, final HttpContext context) throws HttpException, IOException { AuthScheme authScheme = authState.getAuthScheme(); Credentials creds = authState.getCredentials(); switch (authState.getState()) { // TODO add UNCHALLENGED and HANDSHAKE cases case FAILURE: return; case SUCCESS: ensureAuthScheme(authScheme); if (authScheme.isConnectionBased()) { return; if (this.log.isDebugEnabled()) { this.log.debug("Generating response to an authentication challenge using " + authScheme.getSchemeName() + " scheme"); request.addHeader(header); break; } catch (final AuthenticationException ex) { if (this.log.isWarnEnabled()) { this.log.warn(authScheme + " authentication error: " + ex.getMessage()); try { final Header header = doAuth(authScheme, creds, request, context); request.addHeader(header); } catch (final AuthenticationException ex) { if (this.log.isErrorEnabled()) { this.log.error(authScheme + " authentication error: " + ex.getMessage());
private boolean isCachable(final AuthState authState) { final AuthScheme authScheme = authState.getAuthScheme(); if (authScheme == null || !authScheme.isComplete()) { return false; } final String schemeName = authScheme.getSchemeName(); return schemeName.equalsIgnoreCase(AuthPolicy.BASIC) || schemeName.equalsIgnoreCase(AuthPolicy.DIGEST); }
protected boolean isCachable(final AuthScheme authScheme) { if (authScheme == null || !authScheme.isComplete()) { return false; } final String schemeName = authScheme.getSchemeName(); return schemeName.equalsIgnoreCase(AuthSchemes.BASIC); }