@Override public void process(HttpRequest request, HttpContext context) { AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); Credentials credentials = credsProvider.getCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (credentials != null) { authState.update(new BasicScheme(), credentials); } } } }
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth"); CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authScheme != null) { Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (creds == null) { throw new HttpException("No credentials for preemptive authentication"); authState.setAuthScheme(authScheme); authState.setCredentials(creds);
private HttpClientContext convertHttpClientContext(Request request, Site site, Proxy proxy) { HttpClientContext httpContext = new HttpClientContext(); if (proxy != null && proxy.getUsername() != null) { AuthState authState = new AuthState(); authState.update(new BasicScheme(ChallengeState.PROXY), new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword())); httpContext.setAttribute(HttpClientContext.PROXY_AUTH_STATE, authState); } if (request.getCookies() != null && !request.getCookies().isEmpty()) { CookieStore cookieStore = new BasicCookieStore(); for (Map.Entry<String, String> cookieEntry : request.getCookies().entrySet()) { BasicClientCookie cookie1 = new BasicClientCookie(cookieEntry.getKey(), cookieEntry.getValue()); cookie1.setDomain(UrlUtils.removePort(UrlUtils.getDomain(request.getUrl()))); cookieStore.addCookie(cookie1); } httpContext.setCookieStore(cookieStore); } return httpContext; }
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; }
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"); }
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();
final CredentialsProvider credsProvider) { if (!authState.isValid()) { return; 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 (this.log.isDebugEnabled()) { if (creds != null) { if (authScheme.isComplete()) { this.log.debug("Authentication failed"); creds = null; authState.setAuthScope(authScope); authState.setCredentials(creds);
CredentialsProvider credentialsProvider = localContext.getCredentialsProvider(); if (key.hasProxy && !StringUtils.isEmpty(key.proxyUser)) { authScope = new AuthScope(key.proxyHost, key.proxyPort); credentials = credentialsProvider.getCredentials(authScope); credentialsProvider.clear(); if (credentials != null) { credentialsProvider.setCredentials(authScope, credentials); } else { try { requestURI = new URI(request.getRequestLine().getUri()); } catch (final URISyntaxException ignore) { // NOSONAR HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); URL url; if(requestURI.isAbsolute()) { url = requestURI.toURL(); } else { url = new URL(targetHost.getSchemeName(), targetHost.getHostName(), targetHost.getPort(), requestURI.getPath()); AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort(), authorization.getRealm(), targetHost.getSchemeName()); Credentials creds = credentialsProvider.getCredentials(authScope);
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()) {
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); if (this.targetAuthState.getCredentials() != null) { this.targetAuthState.setAuthScope(null); HttpHost proxy = route.getProxyHost(); if (this.proxyAuthState.getCredentials() != null) { this.proxyAuthState.setAuthScope(null);
/** * 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; } } } } }
HttpHost proxy = route.getProxyHost(); RequestWrapper request = roureq.getRequest(); HttpHost newTarget = new HttpHost( uri.getHost(), uri.getPort(), redirect.setHeaders(orig.getAllHeaders()); context.getAttribute(ClientContext.CREDS_PROVIDER); context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (target == null) { target = route.getTargetHost(); if (this.targetAuthState.getCredentials() != null) { this.targetAuthState.setAuthScope(null); if (this.proxyAuthState.getCredentials() != null) { this.proxyAuthState.setAuthScope(null);
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()) { context.getAttribute(ClientContext.CREDS_PROVIDER); if (this.proxyAuthState.getCredentials() != null) { done = false; this.proxyAuthState.setAuthScope(null);
public void process(HttpRequest request, HttpContext context) { AuthState authState = (AuthState) context.getAttribute( ClientContext.TARGET_AUTH_STATE); // If not auth scheme has been initialized yet if (authState.getAuthScheme() == null) { Credentials creds = new UsernamePasswordCredentials(username, password); authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } }
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; final Queue<AuthOption> authOptions = authState.getAuthOptions(); if (authOptions != null) { while (!authOptions.isEmpty()) { authScheme = authOption.getAuthScheme(); creds = authOption.getCredentials(); authState.update(authScheme, creds); 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) {
throws HttpException, IOException { HttpHost proxy = route.getProxyHost(); HttpHost target = route.getTargetHost(); HttpResponse response = null; connect.setParams(this.params); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, proxy); context.setAttribute(ExecutionContext.HTTP_CONNECTION, managedConn); context.setAttribute(ClientContext.TARGET_AUTH_STATE, if (this.proxyAuthState.getCredentials() != null) { done = false; this.proxyAuthState.setAuthScope(null);
@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 } } } } } }
if (!redirect.headerIterator().hasNext()) { final HttpRequest original = request.getOriginal(); redirect.setHeaders(original.getAllHeaders()); if (!currentRoute.getTargetHost().equals(newTarget)) { final AuthState targetAuthState = context.getTargetAuthState(); if (targetAuthState != null) { 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();
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { Credentials creds = new UsernamePasswordCredentials(myUsername, myPassword); authState.update(new BasicScheme(), creds); } }
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); // If no auth scheme available yet, try to initialize it preemptively if (authState.getAuthScheme() == null) { CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); Credentials creds = credsProvider.getCredentials(AuthScope.ANY); authState.update(authScheme, creds); } } }