private void doDemo() throws IOException { HttpClient client = new HttpClient(); client.getParams().setParameter( CredentialsProvider.PROVIDER, new ConsoleAuthPrompter()); GetMethod httpget = new GetMethod("http://target-host/requires-auth.html"); httpget.setDoAuthentication(true); try { // execute the GET int status = client.executeMethod(httpget); // print the status and response System.out.println(httpget.getStatusLine().toString()); System.out.println(httpget.getResponseBodyAsString()); } finally { // release any connection resources used by the method httpget.releaseConnection(); } }
/** * Tests GET via non-authenticating proxy + interactive host auth + connection keep-alive */ public void testGetInteractiveHostAuthConnKeepAlive() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); this.client.getParams().setParameter(CredentialsProvider.PROVIDER, new GetItWrongThenGetItRight()); HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true)); handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); this.server.setRequestHandler(handlerchain); GetMethod get = new GetMethod("/"); try { this.client.executeMethod(get); assertEquals(HttpStatus.SC_OK, get.getStatusCode()); } finally { get.releaseConnection(); } }
/** * Tests GET via non-authenticating proxy + interactive host auth + connection close */ public void testGetInteractiveHostAuthConnClose() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); this.client.getParams().setParameter(CredentialsProvider.PROVIDER, new GetItWrongThenGetItRight()); HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false)); handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); this.server.setRequestHandler(handlerchain); GetMethod get = new GetMethod("/"); try { this.client.executeMethod(get); assertEquals(HttpStatus.SC_OK, get.getStatusCode()); } finally { get.releaseConnection(); } }
/** * Tests GET via authenticating proxy + interactive host and proxy auth + connection keep-alive */ public void testGetInteractiveProxyAuthHostAuthConnKeepAlive() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); this.client.getParams().setParameter(CredentialsProvider.PROVIDER, new GetItWrongThenGetItRight()); HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true)); handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); this.server.setRequestHandler(handlerchain); this.proxy.requireAuthentication(creds, "test", true); GetMethod get = new GetMethod("/"); try { this.client.executeMethod(get); assertEquals(HttpStatus.SC_OK, get.getStatusCode()); } finally { get.releaseConnection(); } }
/** * Tests GET via authenticating proxy + interactive host and proxy auth + connection close */ public void testGetInteractiveProxyAuthHostAuthConnClose() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); this.client.getParams().setParameter(CredentialsProvider.PROVIDER, new GetItWrongThenGetItRight()); HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false)); handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); this.server.setRequestHandler(handlerchain); this.proxy.requireAuthentication(creds, "test", true); GetMethod get = new GetMethod("/"); try { this.client.executeMethod(get); assertEquals(HttpStatus.SC_OK, get.getStatusCode()); } finally { get.releaseConnection(); } }
/** * Tests POST via non-authenticating proxy + interactive host auth + connection keep-alive */ public void testPostInteractiveHostAuthConnKeepAlive() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); this.client.getParams().setParameter(CredentialsProvider.PROVIDER, new GetItWrongThenGetItRight()); HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true)); handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); this.server.setRequestHandler(handlerchain); PostMethod post = new PostMethod("/"); post.setRequestEntity(new StringRequestEntity("Like tons of stuff", null, null)); try { this.client.executeMethod(post); assertEquals(HttpStatus.SC_OK, post.getStatusCode()); assertNotNull(post.getResponseBodyAsString()); } finally { post.releaseConnection(); } }
/** * Tests POST via non-authenticating proxy + interactive host auth + connection close */ public void testPostInteractiveHostAuthConnClose() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); this.client.getParams().setParameter(CredentialsProvider.PROVIDER, new GetItWrongThenGetItRight()); HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false)); handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); this.server.setRequestHandler(handlerchain); PostMethod post = new PostMethod("/"); post.setRequestEntity(new StringRequestEntity("Like tons of stuff", null, null)); try { this.client.executeMethod(post); assertEquals(HttpStatus.SC_OK, post.getStatusCode()); assertNotNull(post.getResponseBodyAsString()); } finally { post.releaseConnection(); } }
/** * Tests POST via non-authenticating proxy + interactive host auth + connection keep-alive */ public void testPostInteractiveProxyAuthHostAuthConnKeepAlive() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); this.client.getParams().setParameter(CredentialsProvider.PROVIDER, new GetItWrongThenGetItRight()); HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true)); handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); this.server.setRequestHandler(handlerchain); this.proxy.requireAuthentication(creds, "test", true); PostMethod post = new PostMethod("/"); post.setRequestEntity(new StringRequestEntity("Like tons of stuff", null, null)); try { this.client.executeMethod(post); assertEquals(HttpStatus.SC_OK, post.getStatusCode()); assertNotNull(post.getResponseBodyAsString()); } finally { post.releaseConnection(); } }
/** * Tests POST via non-authenticating proxy + interactive host auth + connection close */ public void testPostInteractiveProxyAuthHostAuthConnClose() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); this.client.getParams().setParameter(CredentialsProvider.PROVIDER, new GetItWrongThenGetItRight()); HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false)); handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); this.server.setRequestHandler(handlerchain); this.proxy.requireAuthentication(creds, "test", true); PostMethod post = new PostMethod("/"); post.setRequestEntity(new StringRequestEntity("Like tons of stuff", null, null)); try { this.client.executeMethod(post); assertEquals(HttpStatus.SC_OK, post.getStatusCode()); assertNotNull(post.getResponseBodyAsString()); } finally { post.releaseConnection(); } }
/** * Tests POST via non-authenticating proxy + interactive host auth + connection keep-alive */ public void testPostInteractiveHostAuthConnKeepAlive() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); this.client.getParams().setParameter(CredentialsProvider.PROVIDER, new GetItWrongThenGetItRight()); HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true)); handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); this.server.setRequestHandler(handlerchain); PostMethod post = new PostMethod("/"); post.setRequestEntity(new StringRequestEntity("Like tons of stuff")); try { this.client.executeMethod(post); assertEquals(HttpStatus.SC_OK, post.getStatusCode()); assertNotNull(post.getResponseBodyAsString()); } finally { post.releaseConnection(); } }
/** * Tests POST via non-authenticating proxy + interactive host auth + connection close */ public void testPostInteractiveHostAuthConnClose() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); this.client.getParams().setParameter(CredentialsProvider.PROVIDER, new GetItWrongThenGetItRight()); HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false)); handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); this.server.setRequestHandler(handlerchain); PostMethod post = new PostMethod("/"); post.setRequestEntity(new StringRequestEntity("Like tons of stuff")); try { this.client.executeMethod(post); assertEquals(HttpStatus.SC_OK, post.getStatusCode()); assertNotNull(post.getResponseBodyAsString()); } finally { post.releaseConnection(); } }
/** * Tests POST via non-authenticating proxy + interactive host auth + connection keep-alive */ public void testPostInteractiveProxyAuthHostAuthConnKeepAlive() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); this.client.getParams().setParameter(CredentialsProvider.PROVIDER, new GetItWrongThenGetItRight()); HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true)); handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); this.server.setRequestHandler(handlerchain); this.proxy.requireAuthentication(creds, "test", true); PostMethod post = new PostMethod("/"); post.setRequestEntity(new StringRequestEntity("Like tons of stuff")); try { this.client.executeMethod(post); assertEquals(HttpStatus.SC_OK, post.getStatusCode()); assertNotNull(post.getResponseBodyAsString()); } finally { post.releaseConnection(); } }
/** * Tests POST via non-authenticating proxy + interactive host auth + connection close */ public void testPostInteractiveProxyAuthHostAuthConnClose() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); this.client.getParams().setParameter(CredentialsProvider.PROVIDER, new GetItWrongThenGetItRight()); HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false)); handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); this.server.setRequestHandler(handlerchain); this.proxy.requireAuthentication(creds, "test", true); PostMethod post = new PostMethod("/"); post.setRequestEntity(new StringRequestEntity("Like tons of stuff")); try { this.client.executeMethod(post); assertEquals(HttpStatus.SC_OK, post.getStatusCode()); assertNotNull(post.getResponseBodyAsString()); } finally { post.releaseConnection(); } }
public static void enableAuth(HttpClient client, Keychain keychain, KeyId keyId) { Signer signer = new Signer(keychain, keyId); CredentialsProvider credProvider = (CredentialsProvider) client.getParams() .getParameter(CredentialsProvider.PROVIDER); CredentialsProvider newProvider; if (credProvider instanceof SignerCredentialsProvider) { newProvider = new SignerCredentialsProvider(signer, ((SignerCredentialsProvider) credProvider).getDelegatee()); } else { newProvider = new SignerCredentialsProvider(signer, credProvider); } client.getParams().setParameter(CredentialsProvider.PROVIDER, newProvider); AuthPolicy.registerAuthScheme(Constants.SCHEME, Http3SignatureAuthScheme.class); List<String> schemes = new ArrayList<String>(); schemes.add(Constants.SCHEME); Collection authSchemePriority = (Collection) DefaultHttpParams.getDefaultParams().getParameter(AuthPolicy.AUTH_SCHEME_PRIORITY); if (authSchemePriority != null) { schemes.addAll(authSchemePriority); } client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, schemes); }
private Credentials promptForCredentials( final AuthScheme authScheme, final HttpParams params, final AuthScope authscope) { LOG.debug("Credentials required"); Credentials creds = null; CredentialsProvider credProvider = (CredentialsProvider)params.getParameter(CredentialsProvider.PROVIDER); if (credProvider != null) { try { creds = credProvider.getCredentials( authScheme, authscope.getHost(), authscope.getPort(), false); } catch (CredentialsNotAvailableException e) { LOG.warn(e.getMessage()); } if (creds != null) { this.state.setCredentials(authscope, creds); if (LOG.isDebugEnabled()) { LOG.debug(authscope + " new credentials given"); } } } else { LOG.debug("Credentials provider not available"); } return creds; }
private Credentials promptForProxyCredentials( final AuthScheme authScheme, final HttpParams params, final AuthScope authscope) { LOG.debug("Proxy credentials required"); Credentials creds = null; CredentialsProvider credProvider = (CredentialsProvider)params.getParameter(CredentialsProvider.PROVIDER); if (credProvider != null) { try { creds = credProvider.getCredentials( authScheme, authscope.getHost(), authscope.getPort(), true); } catch (CredentialsNotAvailableException e) { LOG.warn(e.getMessage()); } if (creds != null) { this.state.setProxyCredentials(authscope, creds); if (LOG.isDebugEnabled()) { LOG.debug(authscope + " new credentials given"); } } } else { LOG.debug("Proxy credentials provider not available"); } return creds; }
public SignerCredentialsProvider(Signer signer, CredentialsProvider delegatee) { this.signer = signer; this.delegatee = delegatee; }
public CredentialsProvider getDelegatee() { return delegatee; }
/** * Constructs the service and initialises the properties. * * @param awsCredentials * the S3 user credentials to use when communicating with S3, may be null in which case the * communication is done as an anonymous user. * @param invokingApplicationDescription * a short description of the application using the service, suitable for inclusion in a * user agent string for REST/HTTP requests. Ideally this would include the application's * version number, for example: <code>Cockpit/0.6.1</code> or <code>My App Name/1.0</code> * @param credentialsProvider * an implementation of the HttpClient CredentialsProvider interface, to provide a means for * prompting for credentials when necessary. * * @throws S3ServiceException */ public RestS3Service(AWSCredentials awsCredentials, String invokingApplicationDescription, CredentialsProvider credentialsProvider) throws S3ServiceException { this(awsCredentials, invokingApplicationDescription, credentialsProvider, Jets3tProperties.getInstance(Constants.JETS3T_PROPERTIES_FILENAME)); }
/** * Constructs the service and initialises the properties. * * @param awsCredentials * the S3 user credentials to use when communicating with S3, may be null in which case the * communication is done as an anonymous user. * @param invokingApplicationDescription * a short description of the application using the service, suitable for inclusion in a * user agent string for REST/HTTP requests. Ideally this would include the application's * version number, for example: <code>Cockpit/0.6.1</code> or <code>My App Name/1.0</code> * @param credentialsProvider * an implementation of the HttpClient CredentialsProvider interface, to provide a means for * prompting for credentials when necessary. * @param jets3tProperties * JetS3t properties that will be applied within this service. * * @throws S3ServiceException */ public RestS3Service(AWSCredentials awsCredentials, String invokingApplicationDescription, CredentialsProvider credentialsProvider, Jets3tProperties jets3tProperties) throws S3ServiceException { this(awsCredentials, invokingApplicationDescription, credentialsProvider, jets3tProperties, new HostConfiguration()); }