protected void populateHttpCredential(HttpHost host, AuthScheme authScheme, String user, String password) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, password); AuthCache authCache = httpClientContext.getAuthCache(); if (authCache == null) { authCache = new BasicAuthCache(); httpClientContext.setAuthCache(authCache); } authCache.put(host, authScheme); if (httpClientContext.getCredentialsProvider() == null) { httpClientContext.setCredentialsProvider(new BasicCredentialsProvider()); } httpClientContext.getCredentialsProvider().setCredentials(new AuthScope(host), credentials); }
protected HttpClientContext createContextInstance() { HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(httpClientContextTemplate.getCredentialsProvider()); context.setAuthCache(httpClientContextTemplate.getAuthCache()); return context; }
@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() ); }
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."); } }
HttpClientContext actual = restGet.getHttpClientContext(restConfig, target, Optional.empty()); assertNull(actual.getCredentialsProvider()); 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)));
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; } }
protected void populateHttpCredential(HttpHost host, AuthScheme authScheme, String user, String password) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, password); AuthCache authCache = httpClientContext.getAuthCache(); if (authCache == null) { authCache = new BasicAuthCache(); httpClientContext.setAuthCache(authCache); } authCache.put(host, authScheme); if (httpClientContext.getCredentialsProvider() == null) { httpClientContext.setCredentialsProvider(new BasicCredentialsProvider()); } httpClientContext.getCredentialsProvider().setCredentials(new AuthScope(host), credentials); }
protected HttpClientContext createContextInstance() { HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(httpClientContextTemplate.getCredentialsProvider()); context.setAuthCache(httpClientContextTemplate.getAuthCache()); return context; }
@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); } } } }
@Override public void process(final HttpRequest request, final HttpContext context) throws IOException { if (context instanceof HttpClientContext) { final HttpClientContext clientContext = (HttpClientContext)context; final AuthState authState = clientContext.getTargetAuthState(); final CredentialsProvider credsProvider = clientContext.getCredentialsProvider(); final HttpHost targetHost = clientContext.getTargetHost(); // If not authentication scheme has been initialized yet if (authState.getAuthScheme() == null) { final AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); // Obtain credentials matching the target host final Credentials creds = credsProvider.getCredentials(authScope); // If found, generate BasicScheme preemptively if (creds != null) { authState.update(new BasicScheme(), creds); } } } else { throw new IllegalArgumentException("Not sure how to handle a non-HttpClientContext context."); } }
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { final URI uri = URI.create(request.getRequestLine().getUri()); if (uri.getPath().endsWith(loginPath)) { LOG.debug("Request ends with {} so I'm not intercepting the request", loginPath); return; } Cookie loginCookie = getLoginCookie(context, loginTokenName); if (loginCookie != null) { LOG.debug("Request has cookie {}={} so I'm not intercepting the request", loginCookie.getName(), loginCookie.getValue()); return; } // get host final HttpHost host = HttpClientContext.adapt(context).getTargetHost(); // get the username and password from the credentials provider final CredentialsProvider credsProvider = HttpClientContext.adapt(context).getCredentialsProvider(); final AuthScope scope = new AuthScope(host.getHostName(), host.getPort()); final String username = credsProvider.getCredentials(scope).getUserPrincipal().getName(); final String password = credsProvider.getCredentials(scope).getPassword(); List<NameValuePair> parameters = new LinkedList<>(); parameters.add(new BasicNameValuePair("j_username", username)); parameters.add(new BasicNameValuePair("j_password", password)); HttpEntity httpEntity = new UrlEncodedFormEntity(parameters, "utf-8"); HttpPost loginPost = new HttpPost(URI.create(request.getRequestLine().getUri()).resolve(loginPath)); loginPost.setEntity(httpEntity); final CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build(); client.execute(host, loginPost, context); }
@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); } } } }
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { HttpClientContext clientContext = HttpClientContext.adapt(context); AuthState authState = clientContext.getTargetAuthState(); // If there's no auth scheme available yet, try to initialize it preemptively 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); } } } }
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { HttpClientContext clientContext = HttpClientContext.adapt(context); AuthState authState = clientContext.getTargetAuthState(); // If there's no auth scheme available yet, try to initialize it preemptively 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); } } } }
/** * Add authentication information for the given host * @param host the host * @param credentials the credentials * @param authScheme the scheme for preemptive authentication (should be * <code>null</code> if adding authentication for a proxy server) * @param context the context in which the authentication information * should be saved */ private void addAuthentication(HttpHost host, Credentials credentials, AuthScheme authScheme, HttpClientContext context) { AuthCache authCache = context.getAuthCache(); if (authCache == null) { authCache = new BasicAuthCache(); context.setAuthCache(authCache); } CredentialsProvider credsProvider = context.getCredentialsProvider(); if (credsProvider == null) { credsProvider = new BasicCredentialsProvider(); context.setCredentialsProvider(credsProvider); } credsProvider.setCredentials(new AuthScope(host), credentials); if (authScheme != null) { authCache.put(host, authScheme); } }
@Override public HttpClientContext decoratePrototypeContext( final AuthScope scope, SiteConfig location, PasswordType type, HttpClientContext ctx ) { CredentialsProvider creds = ctx.getCredentialsProvider(); if ( creds == null || !(creds instanceof BasicCredentialsProvider)) { creds = new BasicCredentialsProvider(); ctx.setCredentialsProvider( creds ); } if ( PasswordType.USER == type ) { final String password = passwords.lookup( new PasswordKey( location, PasswordType.USER ) ); creds.setCredentials( scope, new UsernamePasswordCredentials( location.getUser(), password ) ); } else if ( PasswordType.PROXY == type ) { final String password = passwords.lookup( new PasswordKey( location, PasswordType.PROXY ) ); creds.setCredentials( scope, new UsernamePasswordCredentials( location.getProxyUser(), password ) ); } return ctx; }
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { HttpClientContext clientContext = HttpClientContext.adapt(context); AuthState proxyAuthState = clientContext.getProxyAuthState(); // If there's no auth scheme available yet, try to initialize it preemptively if (proxyAuthState.getAuthScheme() == null) { CredentialsProvider credsProvider = clientContext.getCredentialsProvider(); RouteInfo route = clientContext.getHttpRoute(); if (route == null) { return; } HttpHost proxyHost = route.getProxyHost(); if (proxyHost == null) { return; } Credentials creds = credsProvider.getCredentials( new AuthScope(proxyHost.getHostName(), proxyHost.getPort())); if (creds == null) { return; } proxyAuthState.update(new BasicScheme(ChallengeState.PROXY), creds); } } }
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { HttpClientContext clientContext = HttpClientContext.adapt(context); AuthState proxyAuthState = clientContext.getProxyAuthState(); // If there's no auth scheme available yet, try to initialize it preemptively if (proxyAuthState.getAuthScheme() == null) { CredentialsProvider credsProvider = clientContext.getCredentialsProvider(); RouteInfo route = clientContext.getHttpRoute(); if (route == null) { return; } HttpHost proxyHost = route.getProxyHost(); if (proxyHost == null) { return; } Credentials creds = credsProvider.getCredentials( new AuthScope(proxyHost.getHostName(), proxyHost.getPort())); if (creds == null) { return; } proxyAuthState.update(new BasicScheme(ChallengeState.PROXY), creds); } } }
private void copyContextPrototype( final HttpClientContext prototype, final HttpClientContext target ) { target.setRequestConfig( prototype.getRequestConfig() ); target.setAuthCache( prototype.getAuthCache() ); target.setAuthSchemeRegistry( prototype.getAuthSchemeRegistry() ); target.setCookieSpecRegistry( prototype.getCookieSpecRegistry() ); target.setCookieStore( prototype.getCookieStore() ); target.setCredentialsProvider( prototype.getCredentialsProvider() ); target.setUserToken( prototype.getUserToken() ); }