use of org.keycloak.authorization.client.util.HttpResponseException in project alfresco-repository by Alfresco.
the class IdentityServiceAuthenticationComponentTest method testAuthenticationFail.
@Test(expected = AuthenticationException.class)
public void testAuthenticationFail() {
when(mockAuthzClient.obtainAccessToken("username", "password")).thenThrow(new HttpResponseException("Unauthorized", 401, "Unauthorized", null));
authComponent.authenticateImpl("username", "password".toCharArray());
}
use of org.keycloak.authorization.client.util.HttpResponseException in project alfresco-repository by Alfresco.
the class IdentityServiceAuthenticationComponent method authenticateImpl.
public void authenticateImpl(String userName, char[] password) throws AuthenticationException {
if (authzClient == null) {
if (logger.isDebugEnabled()) {
logger.debug("AuthzClient was not set, possibly due to the 'identity-service.authentication.enable-username-password-authentication=false' property. ");
}
throw new AuthenticationException("User not authenticated because AuthzClient was not set.");
}
try {
// Attempt to get an access token using the user credentials
authzClient.obtainAccessToken(userName, new String(password));
// Successfully obtained access token so treat as authenticated user
setCurrentUser(userName);
} catch (HttpResponseException e) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to authenticate user against Keycloak. Status: " + e.getStatusCode() + " Reason: " + e.getReasonPhrase());
}
throw new AuthenticationException("Failed to authenticate user against Keycloak.", e);
}
}
use of org.keycloak.authorization.client.util.HttpResponseException in project keycloak by keycloak.
the class HttpClaimInformationPointProvider method executeRequest.
private InputStream executeRequest(HttpFacade httpFacade) {
String method = config.get("method").toString();
if (method == null) {
method = "GET";
}
RequestBuilder builder = null;
if ("GET".equalsIgnoreCase(method)) {
builder = RequestBuilder.get();
} else {
builder = RequestBuilder.post();
}
builder.setUri(config.get("url").toString());
byte[] bytes = new byte[0];
try {
setParameters(builder, httpFacade);
if (config.containsKey("headers")) {
setHeaders(builder, httpFacade);
}
HttpResponse response = httpClient.execute(builder.build());
HttpEntity entity = response.getEntity();
if (entity != null) {
bytes = EntityUtils.toByteArray(entity);
}
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode < 200 || statusCode >= 300) {
throw new HttpResponseException("Unexpected response from server: " + statusCode + " / " + statusLine.getReasonPhrase(), statusCode, statusLine.getReasonPhrase(), bytes);
}
return new ByteArrayInputStream(bytes);
} catch (Exception cause) {
try {
throw new RuntimeException("Error executing http method [" + builder + "]. Response : " + StreamUtil.readString(new ByteArrayInputStream(bytes), Charset.forName("UTF-8")), cause);
} catch (Exception e) {
throw new RuntimeException("Error executing http method [" + builder + "]", cause);
}
}
}
use of org.keycloak.authorization.client.util.HttpResponseException in project keycloak by keycloak.
the class ResourceManagementTest method doCreateResource.
protected ResourceRepresentation doCreateResource(ResourceRepresentation newResource) {
ResourcesResource resources = getClientResource().authorization().resources();
try (Response response = resources.create(newResource)) {
int status = response.getStatus();
if (status != Response.Status.CREATED.getStatusCode()) {
throw new RuntimeException(new HttpResponseException("Error", status, "", null));
}
ResourceRepresentation stored = response.readEntity(ResourceRepresentation.class);
return resources.resource(stored.getId()).toRepresentation();
}
}
Aggregations