Java Code Examples for org.apache.http.client.CredentialsProvider#getCredentials()
The following examples show how to use
org.apache.http.client.CredentialsProvider#getCredentials() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebServicesClientTest.java From attic-apex-core with Apache License 2.0 | 6 votes |
public static void checkUserCredentials(String username, String password, AuthScheme authScheme) throws NoSuchFieldException, IllegalAccessException { CredentialsProvider provider = getCredentialsProvider(); String httpScheme = AuthScope.ANY_SCHEME; if (authScheme == AuthScheme.BASIC) { httpScheme = AuthSchemes.BASIC; } else if (authScheme == AuthScheme.DIGEST) { httpScheme = AuthSchemes.DIGEST; } AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, httpScheme); Credentials credentials = provider.getCredentials(authScope); Assert.assertNotNull("Credentials", credentials); Assert.assertTrue("Credentials type is user", UsernamePasswordCredentials.class.isAssignableFrom(credentials.getClass())); UsernamePasswordCredentials pwdCredentials = (UsernamePasswordCredentials)credentials; Assert.assertEquals("Username", username, pwdCredentials.getUserName()); Assert.assertEquals("Password", password, pwdCredentials.getPassword()); }
Example 2
Source File: JolokiaClientFactory.java From hawkular-agent with Apache License 2.0 | 6 votes |
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); } }
Example 3
Source File: HttpClientConfigurator.java From bintray-client-java with Apache License 2.0 | 6 votes |
@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); } else { if (log.isDebugEnabled()) { log.debug("PreemptiveAuthInterceptor: No credentials for preemptive authentication"); } } } }
Example 4
Source File: HttpClientConfigurer.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 6 votes |
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() != null || authState.hasAuthOptions()) { return; } // If no authState has been established and this is a PUT or POST request, add preemptive authorisation String requestMethod = request.getRequestLine().getMethod(); if (requestMethod.equals(HttpPut.METHOD_NAME) || requestMethod.equals(HttpPost.METHOD_NAME)) { CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); Credentials credentials = credentialsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (credentials == null) { throw new HttpException("No credentials for preemptive authentication"); } authState.update(authScheme, credentials); } }
Example 5
Source File: TestUrlCreds.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testUrlCred() throws HTTPException { String url = "https://" + username + ":" + password + "@" + host + "/something/garbage.grb?query"; logger.info("Testing {}", url); try (HTTPMethod method = HTTPFactory.Get(url)) { HTTPSession session = method.getSession(); method.close(); session.setCredentialsProvider(new BasicCredentialsProvider()); try (HTTPMethod method2 = HTTPFactory.Get(session, url)) { HTTPSession session2 = method2.getSession(); CredentialsProvider credentialsProvider = session2.sessionprovider; assertThat(credentialsProvider).isNotNull(); AuthScope expectedAuthScope = new AuthScope(host, 80); Credentials credentials = credentialsProvider.getCredentials(expectedAuthScope); assertThat(credentials).isNotNull(); assertThat(credentials.getUserPrincipal().getName()).isEqualTo(username); assertThat(credentials.getPassword()).isEqualTo(password); } } }
Example 6
Source File: UriStrategy.java From activemq-artemis with Apache License 2.0 | 6 votes |
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); // If no auth scheme available yet, try to initialize it preemptively if (authState.getAuthScheme() == null) { AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth"); CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.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.update(authScheme, creds); } } }
Example 7
Source File: HttpClientConfigurer.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() != null || authState.hasAuthOptions()) { return; } // If no authState has been established and this is a PUT or POST request, add preemptive authorisation String requestMethod = request.getRequestLine().getMethod(); if (requestMethod.equals(HttpPut.METHOD_NAME) || requestMethod.equals(HttpPost.METHOD_NAME)) { CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); Credentials credentials = credentialsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (credentials == null) { throw new HttpException("No credentials for preemptive authentication"); } authState.update(authScheme, credentials); } }
Example 8
Source File: HttpSender.java From cloudhopper-commons with Apache License 2.0 | 6 votes |
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) { 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); } } }
Example 9
Source File: TestUrlCreds.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testUrlCredDefaultProvider() throws HTTPException { String url = "https://" + username + ":" + password + "@" + host + "/something/garbage.grb?query"; logger.info("Testing {}", url); try (HTTPMethod method = HTTPFactory.Get(url)) { // we should have a default BasicCredentialProvider available, even though // we didn't call the factory with a specific provider HTTPSession session = method.getSession(); CredentialsProvider credentialsProvider = session.sessionprovider; assertThat(credentialsProvider).isNotNull(); AuthScope expectedAuthScope = new AuthScope(host, 80); Credentials credentials = credentialsProvider.getCredentials(expectedAuthScope); assertThat(credentials).isNotNull(); assertThat(credentials.getUserPrincipal().getName()).isEqualTo(username); assertThat(credentials.getPassword()).isEqualTo(password); } }
Example 10
Source File: PreemptiveAuth.java From lucene-solr with Apache License 2.0 | 5 votes |
@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); } }
Example 11
Source File: PreemtiveAuthorizationHttpRequestInterceptor.java From Mobike with Apache License 2.0 | 5 votes |
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); } } }
Example 12
Source File: ProxyClientHttpRequestFactoryTest.java From sinavi-jfw with Apache License 2.0 | 5 votes |
@Test public void プロキシが設定されたHttpClientが生成される() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { HttpClient client = proxyFactory.getHttpClient(); assertThat(client, CoreMatchers.notNullValue()); CredentialsProvider provider = getCredentialsProvider(client); assertThat(provider, CoreMatchers.notNullValue()); Credentials credentials = provider.getCredentials(new AuthScope("proxy.example.com", 8080)); assertThat(credentials, CoreMatchers.notNullValue()); assertThat(credentials.getUserPrincipal().getName(), CoreMatchers.is("userid")); assertThat(credentials.getPassword(), CoreMatchers.is("P@ssword!")); }
Example 13
Source File: RemoteSystemClient.java From ghwatch with Apache License 2.0 | 5 votes |
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); } } }
Example 14
Source File: CredentialsProviderBuilderTest.java From cs-actions with Apache License 2.0 | 5 votes |
@Test public void createDefaultCredentialsProvider() { CredentialsProvider credentialsProvider = getCredentialsProvider(""); Credentials credentials = credentialsProvider.getCredentials(new AuthScope("host", 80)); assertThat(credentials, instanceOf(UsernamePasswordCredentials.class)); UsernamePasswordCredentials userCredentials = (UsernamePasswordCredentials) credentials; assertEquals("pass", userCredentials.getPassword()); }
Example 15
Source File: CredentialsProviderBuilderTest.java From cs-actions with Apache License 2.0 | 5 votes |
@Test public void createNtlmCredentialsProvider() { CredentialsProvider credentialsProvider = getCredentialsProvider(AuthSchemes.NTLM); Credentials credentials = credentialsProvider.getCredentials(new AuthScope("host", 80)); assertThat(credentials, instanceOf(NTCredentials.class)); NTCredentials ntCredentials = (NTCredentials) credentials; assertEquals("DOMAIN", ntCredentials.getDomain()); assertEquals("HOST", ntCredentials.getWorkstation()); assertEquals("pass", ntCredentials.getPassword()); Credentials proxyCredentials = credentialsProvider.getCredentials(new AuthScope("proxy", 8080)); assertThat(proxyCredentials, instanceOf(UsernamePasswordCredentials.class)); UsernamePasswordCredentials userCredentials = (UsernamePasswordCredentials) proxyCredentials; assertEquals("proxyUsername", userCredentials.getUserName()); }
Example 16
Source File: PreemtiveAuthorizationHttpRequestInterceptor.java From Android-Basics-Codes with Artistic License 2.0 | 5 votes |
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); } } }
Example 17
Source File: CredentialsProviderBuilderTest.java From cs-actions with Apache License 2.0 | 5 votes |
@Test public void createKerberosCredentialsProvider() { CredentialsProvider credentialsProvider = getCredentialsProvider(AuthSchemes.KERBEROS); Credentials credentials = credentialsProvider.getCredentials(new AuthScope("host", 80)); assertThat(credentials, instanceOf(Credentials.class)); }
Example 18
Source File: PreemtiveAuthorizationHttpRequestInterceptor.java From Android-Basics-Codes with Artistic License 2.0 | 5 votes |
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); } } }
Example 19
Source File: c.java From MiBandDecompiled with Apache License 2.0 | 5 votes |
public void process(HttpRequest httprequest, HttpContext httpcontext) { AuthState authstate = (AuthState)httpcontext.getAttribute("http.auth.target-scope"); CredentialsProvider credentialsprovider = (CredentialsProvider)httpcontext.getAttribute("http.auth.credentials-provider"); HttpHost httphost = (HttpHost)httpcontext.getAttribute("http.target_host"); if (authstate.getAuthScheme() == null) { org.apache.http.auth.Credentials credentials = credentialsprovider.getCredentials(new AuthScope(httphost.getHostName(), httphost.getPort())); if (credentials != null) { authstate.setAuthScheme(new BasicScheme()); authstate.setCredentials(credentials); } } }
Example 20
Source File: LibRequestDirector.java From YiBo with Apache License 2.0 | 4 votes |
private void updateAuthState( final AuthState authState, final HttpHost host, final CredentialsProvider credsProvider) { if (!authState.isValid()) { return; } String hostname = host.getHostName(); int port = host.getPort(); if (port < 0) { Scheme scheme = connManager.getSchemeRegistry().getScheme(host); port = scheme.getDefaultPort(); } AuthScheme authScheme = authState.getAuthScheme(); AuthScope authScope = new AuthScope( hostname, port, authScheme.getRealm(), authScheme.getSchemeName()); if (DEBUG) { Logger.debug("Authentication scope: {}", authScope); } Credentials creds = authState.getCredentials(); if (creds == null) { creds = credsProvider.getCredentials(authScope); if (DEBUG) { if (creds != null) { Logger.debug("Found credentials"); } else { Logger.debug("Credentials not found"); } } } else { if (authScheme.isComplete()) { if (DEBUG) { Logger.debug("Authentication failed"); } creds = null; } } authState.setAuthScope(authScope); authState.setCredentials(creds); }