use of org.apache.http.client.CredentialsProvider in project vcell by virtualcell.
the class VCellApiClient method authenticate.
public AccessTokenRepresentation authenticate(String userid, String password, boolean alreadyDigested) throws ClientProtocolException, IOException {
// hash the password
String digestedPassword = (alreadyDigested) ? (password) : createdDigestPassword(password);
HttpGet httpget = new HttpGet("https://" + httpHost.getHostName() + ":" + httpHost.getPort() + "/access_token?user_id=" + userid + "&user_password=" + digestedPassword + "&client_id=" + clientID);
if (lg.isLoggable(Level.INFO)) {
lg.info("Executing request to retrieve access_token " + httpget.getRequestLine());
}
String responseBody = httpclient.execute(httpget, responseHandler);
String accessTokenJson = responseBody;
if (lg.isLoggable(Level.INFO)) {
lg.info("returned: " + accessTokenJson);
}
Gson gson = new Gson();
AccessTokenRepresentation accessTokenRep = gson.fromJson(accessTokenJson, AccessTokenRepresentation.class);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(httpHost.getHostName(), httpHost.getPort()), new UsernamePasswordCredentials("access_token", accessTokenRep.getToken()));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(httpHost, basicAuth);
// Add AuthCache to the execution context
httpClientContext = HttpClientContext.create();
httpClientContext.setCredentialsProvider(credsProvider);
httpClientContext.setAuthCache(authCache);
return accessTokenRep;
}
use of org.apache.http.client.CredentialsProvider in project Lucee by lucee.
the class HTTPEngine4Impl method setProxy.
public static void setProxy(HttpClientBuilder builder, HttpUriRequest request, ProxyData proxy) {
// set Proxy
if (ProxyDataImpl.isValid(proxy)) {
HttpHost host = new HttpHost(proxy.getServer(), proxy.getPort() == -1 ? 80 : proxy.getPort());
builder.setProxy(host);
// username/password
if (!StringUtil.isEmpty(proxy.getUsername())) {
CredentialsProvider cp = new BasicCredentialsProvider();
builder.setDefaultCredentialsProvider(cp);
cp.setCredentials(new AuthScope(proxy.getServer(), proxy.getPort()), new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));
}
}
}
use of org.apache.http.client.CredentialsProvider in project CzechIdMng by bcvsolutions.
the class TestAppAuthenticationFilter method getSigningKey.
private Optional<String> getSigningKey() {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("idm", "");
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(1 * 1000).setSocketTimeout(1 * 1000).build()).build();
try {
HttpResponse response = client.execute(new HttpGet("http://localhost:9080/oauth/token_key"));
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Status: " + statusCode);
if (statusCode < 400) {
ObjectMapper om = new ObjectMapper();
KeyAlg key = om.readValue(response.getEntity().getContent(), KeyAlg.class);
return Optional.of(key.getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
return Optional.empty();
}
use of org.apache.http.client.CredentialsProvider in project janusgraph by JanusGraph.
the class RestClientSetupTest method testHttpBasicAuthConfiguration.
@Test
public void testHttpBasicAuthConfiguration() throws Exception {
// testing that the appropriate values are passed to the client builder via credentials provider
final String testRealm = "testRealm";
final String testUser = "testUser";
final String testPassword = "testPassword";
final CredentialsProvider cp = basicAuthTestBase(ImmutableMap.<String, String>builder().build(), testRealm, testUser, testPassword);
final Credentials credentials = cp.getCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, testRealm));
assertNotNull(credentials);
assertEquals(testUser, credentials.getUserPrincipal().getName());
assertEquals(testPassword, credentials.getPassword());
}
use of org.apache.http.client.CredentialsProvider in project janusgraph by JanusGraph.
the class RestClientSetupTest method basicAuthTestBase.
private CredentialsProvider basicAuthTestBase(final Map<String, String> extraConfigValues, final String realm, final String username, final String password) throws Exception {
final HttpClientConfigCallback hccc = authTestBase(ImmutableMap.<String, String>builder().put("index." + INDEX_NAME + ".elasticsearch.interface", "REST_CLIENT").put("index." + INDEX_NAME + ".elasticsearch.http.auth.type", HttpAuthTypes.BASIC.toString()).put("index." + INDEX_NAME + ".elasticsearch.http.auth.basic.username", username).put("index." + INDEX_NAME + ".elasticsearch.http.auth.basic.password", password).put("index." + INDEX_NAME + ".elasticsearch.http.auth.basic.realm", realm).putAll(extraConfigValues).build());
final HttpAsyncClientBuilder hacb = PowerMockito.mock(HttpAsyncClientBuilder.class);
doReturn(hacb).when(hacb).setDefaultCredentialsProvider(anyObject());
hccc.customizeHttpClient(hacb);
final ArgumentCaptor<BasicCredentialsProvider> cpCaptor = ArgumentCaptor.forClass(BasicCredentialsProvider.class);
verify(hacb).setDefaultCredentialsProvider(cpCaptor.capture());
final CredentialsProvider cp = cpCaptor.getValue();
assertNotNull(cp);
return cp;
}
Aggregations