use of org.apache.http.auth.UsernamePasswordCredentials in project tutorials by eugenp.
the class HttpClientAuthLiveTest method provider.
// UTILS
private CredentialsProvider provider() {
final CredentialsProvider provider = new BasicCredentialsProvider();
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
provider.setCredentials(AuthScope.ANY, credentials);
return provider;
}
use of org.apache.http.auth.UsernamePasswordCredentials in project OpenOLAT by OpenOLAT.
the class RestConnection method login.
public boolean login(String username, String password) throws IOException, URISyntaxException {
URI uri = getContextURI().path("auth").path(username).queryParam("password", password).build();
// provider credentials
provider.setCredentials(new AuthScope(HOST, PORT), new UsernamePasswordCredentials(username, password));
provider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
int code = -1;
HttpGet httpget = new HttpGet(uri);
try (CloseableHttpResponse response = httpclient.execute(httpget)) {
Header header = response.getFirstHeader(RestSecurityHelper.SEC_TOKEN);
if (header != null) {
securityToken = header.getValue();
}
HttpEntity entity = response.getEntity();
code = response.getStatusLine().getStatusCode();
EntityUtils.consume(entity);
} catch (IOException e) {
log.error("", e);
}
return code == 200;
}
use of org.apache.http.auth.UsernamePasswordCredentials in project motech by motech.
the class MdsRestBundleIT method setUp.
@Before
public void setUp() throws IOException {
if (getDataService() == null || getDataServiceForFilteredEntity() == null) {
clearEntities();
prepareEntity();
prepareFilteredEntity();
SchemaHolder schemaHolder = entityService.getSchema();
jarGeneratorService.regenerateMdsDataBundle(schemaHolder);
}
getDataService().deleteAll();
getDataServiceForFilteredEntity().deleteAll();
if (!userService.hasActiveMotechAdmin()) {
userService.registerMotechAdmin("motech", "motech", "motech@motechsuite.org", Locale.ENGLISH);
}
getHttpClient().getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthPolicy.BASIC), new UsernamePasswordCredentials("motech", "motech"));
}
use of org.apache.http.auth.UsernamePasswordCredentials in project opencast by opencast.
the class StandAloneTrustedHttpClientImpl method manuallyHandleDigestAuthentication.
/**
* Handles the necessary handshake for digest authenticaion in the case where it isn't a GET operation.
*
* @param httpUriRequest
* The request location to get the digest authentication for.
* @param httpClient
* The client to send the request through.
* @throws org.opencastproject.security.api.TrustedHttpClientException
* Thrown if the client cannot be shutdown.
*/
private void manuallyHandleDigestAuthentication(HttpUriRequest httpUriRequest, HttpClient httpClient) throws TrustedHttpClientException {
HttpRequestBase digestRequest;
try {
digestRequest = (HttpRequestBase) httpUriRequest.getClass().newInstance();
} catch (Exception e) {
throw new IllegalStateException("Can not create a new " + httpUriRequest.getClass().getName());
}
digestRequest.setURI(httpUriRequest.getURI());
digestRequest.setHeader(REQUESTED_AUTH_HEADER, DIGEST_AUTH);
String[] realmAndNonce = getRealmAndNonce(digestRequest);
if (realmAndNonce != null) {
// Set the user/pass
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, pass);
// Set up the digest authentication with the required values
DigestScheme digestAuth = new DigestScheme();
digestAuth.overrideParamter("realm", realmAndNonce[0]);
digestAuth.overrideParamter("nonce", realmAndNonce[1]);
// Add the authentication header
try {
httpUriRequest.setHeader(digestAuth.authenticate(creds, httpUriRequest));
} catch (Exception e) {
// close the http connection(s)
httpClient.getConnectionManager().shutdown();
throw new TrustedHttpClientException(e);
}
}
}
use of org.apache.http.auth.UsernamePasswordCredentials in project connect-sdk-java by Ingenico-ePayments.
the class DefaultConnectionTest method assertProxy.
@SuppressWarnings("resource")
private static void assertProxy(DefaultConnection connection, ProxyConfiguration proxyConfiguration) {
CloseableHttpClient httpClient = ReflectionUtil.getField(connection, "httpClient", CloseableHttpClient.class);
DefaultProxyRoutePlanner routePlanner = ReflectionUtil.getField(httpClient, "routePlanner", DefaultProxyRoutePlanner.class);
HttpHost proxy = ReflectionUtil.getField(routePlanner, "proxy", HttpHost.class);
Assert.assertEquals(proxyConfiguration.getScheme(), proxy.getSchemeName());
Assert.assertEquals(proxyConfiguration.getPort(), proxy.getPort());
BasicCredentialsProvider credentialsProvider = ReflectionUtil.getField(httpClient, "credentialsProvider", BasicCredentialsProvider.class);
AuthScope authScope = new AuthScope(proxy);
Credentials credentials = credentialsProvider.getCredentials(authScope);
if (proxyConfiguration.getUsername() != null) {
Assert.assertTrue(credentials instanceof UsernamePasswordCredentials);
UsernamePasswordCredentials usernamePasswordCredentials = (UsernamePasswordCredentials) credentials;
Assert.assertEquals(proxyConfiguration.getUsername(), usernamePasswordCredentials.getUserName());
Assert.assertEquals(proxyConfiguration.getPassword(), usernamePasswordCredentials.getPassword());
} else {
Assert.assertNull(credentials);
}
}
Aggregations