Search in sources :

Example 1 with HttpResponseException

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());
}
Also used : HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 2 with HttpResponseException

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);
    }
}
Also used : AuthenticationException(org.alfresco.repo.security.authentication.AuthenticationException) HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException)

Example 3 with HttpResponseException

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);
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) RequestBuilder(org.apache.http.client.methods.RequestBuilder) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) IOException(java.io.IOException)

Example 4 with HttpResponseException

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();
    }
}
Also used : Response(javax.ws.rs.core.Response) HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) ResourcesResource(org.keycloak.admin.client.resource.ResourcesResource) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation)

Aggregations

HttpResponseException (org.keycloak.authorization.client.util.HttpResponseException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Response (javax.ws.rs.core.Response)1 AuthenticationException (org.alfresco.repo.security.authentication.AuthenticationException)1 BaseSpringTest (org.alfresco.util.BaseSpringTest)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 RequestBuilder (org.apache.http.client.methods.RequestBuilder)1 Test (org.junit.Test)1 ResourcesResource (org.keycloak.admin.client.resource.ResourcesResource)1 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)1