@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); } } } }
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");
creds = credsProvider.getCredentials(authScope); if (this.log.isDebugEnabled()) { if (creds != null) {
creds = credsProvider.getCredentials(authScope); if (this.log.isDebugEnabled()) { if (creds != null) {
@Override public HttpResponse answer( InvocationOnMock invocation ) throws Throwable { HttpClientContext context = invocation.getArgumentAt( 1, HttpClientContext.class ); Credentials cred = context.getCredentialsProvider().getCredentials( new AuthScope( "hname", 1111 ) ); assertEquals( "uname", cred.getUserPrincipal().getName() ); return mockResponse( 200, responseContent ); } } );
@Test public void testAddCredentials() throws IOException, ClassNotFoundException { String testUser = "test_username"; slaveServer.setUsername( testUser ); String testPassword = "test_password"; slaveServer.setPassword( testPassword ); String host = "somehost"; slaveServer.setHostname( host ); int port = 1000; slaveServer.setPort( "" + port ); HttpClientContext auth = slaveServer.getAuthContext(); Credentials cred = auth.getCredentialsProvider().getCredentials( new AuthScope( host, port ) ); assertEquals( testUser, cred.getUserPrincipal().getName() ); assertEquals( testPassword, cred.getPassword() ); String user2 = "user2"; slaveServer.setUsername( user2 ); slaveServer.setPassword( "pass2" ); auth = slaveServer.getAuthContext(); cred = auth.getCredentialsProvider().getCredentials( new AuthScope( host, port ) ); assertEquals( user2, cred.getUserPrincipal().getName() ); }
/** {@inheritDoc} */ 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) { CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(HttpClientContext.HTTP_TARGET_HOST); Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (creds == null) { throw new HttpException("No credentials given for preemptive authentication"); } authState.update(authScheme, creds); } } }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { final AuthState authState = (AuthState) context .getAttribute(ClientContext.TARGET_AUTH_STATE); final CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); final HttpHost targetHost = (HttpHost) context .getAttribute(ExecutionContext.HTTP_TARGET_HOST); // If not auth scheme has been initialized yet if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); // Obtain credentials matching the target host Credentials creds = credsProvider.getCredentials(authScope); // If found, generate BasicScheme preemptively if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } } }
private void checkAuthFailure(final HttpResponse response) { if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { final Credentials credentials = httpContext.getCredentialsProvider().getCredentials(AuthScope.ANY); LOG.error("Cloudian admin API authentication failed, please check Cloudian configuration. Admin auth principal=" + credentials.getUserPrincipal() + ", password=" + credentials.getPassword() + ", API url=" + adminApiUrl); throw new ServerApiException(ApiErrorCode.UNAUTHORIZED, "Cloudian backend API call unauthorized, please ask your administrator to fix integration issues."); } }
httpClient.addRequestInterceptor(new PreemptiveAuthInterceptor(), 0); (...) static class PreemptiveAuthInterceptor implements HttpRequestInterceptor { 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 avaialble yet, try to initialize it // preemptively if (authState.getAuthScheme() == null) { CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (creds == null) throw new HttpException("No credentials for preemptive authentication"); authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } }
expected.setCredentialsProvider(expectedCredentialsProvider); assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(target)), actual.getCredentialsProvider().getCredentials(new AuthScope(target))); assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(proxy)), actual.getCredentialsProvider().getCredentials(new AuthScope(proxy))); expected.setCredentialsProvider(expectedCredentialsProvider); assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(target)), actual.getCredentialsProvider().getCredentials(new AuthScope(target))); assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(proxy)), actual.getCredentialsProvider().getCredentials(new AuthScope(proxy))); expected.setCredentialsProvider(expectedCredentialsProvider); assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(target)), actual.getCredentialsProvider().getCredentials(new AuthScope(target))); assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(proxy)), actual.getCredentialsProvider().getCredentials(new AuthScope(proxy)));
httpclient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, AuthPolicy.BASIC); Credentials creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, creds); httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, credsProvider.getCredentials(AuthScope.ANY));
@Override public Credentials getCredentials(final AuthScope authscope) { final String scheme = authscope.getScheme(); if (AuthSchemes.NTLM.equalsIgnoreCase(scheme) || AuthSchemes.SPNEGO.equalsIgnoreCase(scheme)) { return CurrentWindowsCredentials.INSTANCE; } else { return provider.getCredentials(authscope); } }
public Credentials getCredentials( AuthScope authScope ) { return getDelegate( authScope ).getCredentials( authScope ); }
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) { CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); authState.update(new BasicScheme(), creds); } } }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { 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); } } }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute( ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } }
private RestAuthCredential getCredentials(HttpContext context) { HttpHost targetHost = ((HttpClientContext) context).getTargetHost(); int port = targetHost.getPort(); if (port == -1) { port = targetHost.getSchemeName().equals("https") ? 443 : 80; } Credentials credentials = ((HttpClientContext) context).getCredentialsProvider().getCredentials(new AuthScope(targetHost.getHostName(), port)); if (credentials instanceof RestAuthCredential) { return (RestAuthCredential) credentials; } else { return null; } }
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { HttpClientContext clientContext = HttpClientContext.adapt(context); AuthState authState = clientContext.getTargetAuthState(); if (authState.getAuthScheme() == null) { CredentialsProvider credsProvider = clientContext.getCredentialsProvider(); HttpHost targetHost = clientContext.getTargetHost(); Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (creds != null) { authState.update(new BasicScheme(), creds); } } } }