Search in sources :

Example 6 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class IdentityProviderTest method failUpdateInvalidUrl.

@Test
public void failUpdateInvalidUrl() throws Exception {
    try (RealmAttributeUpdater rau = new RealmAttributeUpdater(realm).updateWith(r -> r.setSslRequired(SslRequired.ALL.name())).update()) {
        IdentityProviderRepresentation representation = createRep(UUID.randomUUID().toString(), "oidc");
        representation.getConfig().put("clientId", "clientId");
        representation.getConfig().put("clientSecret", "some secret value");
        try (Response response = realm.identityProviders().create(representation)) {
            assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
        }
        IdentityProviderResource resource = this.realm.identityProviders().get(representation.getAlias());
        representation = resource.toRepresentation();
        OIDCIdentityProviderConfigRep oidcConfig = new OIDCIdentityProviderConfigRep(representation);
        oidcConfig.setAuthorizationUrl("invalid://test");
        try {
            resource.update(representation);
            fail("Invalid URL");
        } catch (Exception e) {
            assertTrue(e instanceof ClientErrorException);
            Response response = ClientErrorException.class.cast(e).getResponse();
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
            ErrorRepresentation error = ((ClientErrorException) e).getResponse().readEntity(ErrorRepresentation.class);
            assertEquals("The url [authorization_url] is malformed", error.getErrorMessage());
        }
        oidcConfig.setAuthorizationUrl(null);
        oidcConfig.setTokenUrl("http://test");
        try {
            resource.update(representation);
            fail("Invalid URL");
        } catch (Exception e) {
            assertTrue(e instanceof ClientErrorException);
            Response response = ClientErrorException.class.cast(e).getResponse();
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
            ErrorRepresentation error = ((ClientErrorException) e).getResponse().readEntity(ErrorRepresentation.class);
            assertEquals("The url [token_url] requires secure connections", error.getErrorMessage());
        }
        oidcConfig.setAuthorizationUrl(null);
        oidcConfig.setTokenUrl(null);
        oidcConfig.setJwksUrl("http://test");
        try {
            resource.update(representation);
            fail("Invalid URL");
        } catch (Exception e) {
            assertTrue(e instanceof ClientErrorException);
            Response response = ClientErrorException.class.cast(e).getResponse();
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
            ErrorRepresentation error = ((ClientErrorException) e).getResponse().readEntity(ErrorRepresentation.class);
            assertEquals("The url [jwks_url] requires secure connections", error.getErrorMessage());
        }
        oidcConfig.setAuthorizationUrl(null);
        oidcConfig.setTokenUrl(null);
        oidcConfig.setJwksUrl(null);
        oidcConfig.setLogoutUrl("http://test");
        try {
            resource.update(representation);
            fail("Invalid URL");
        } catch (Exception e) {
            assertTrue(e instanceof ClientErrorException);
            Response response = ClientErrorException.class.cast(e).getResponse();
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
            ErrorRepresentation error = ((ClientErrorException) e).getResponse().readEntity(ErrorRepresentation.class);
            assertEquals("The url [logout_url] requires secure connections", error.getErrorMessage());
        }
        oidcConfig.setAuthorizationUrl(null);
        oidcConfig.setTokenUrl(null);
        oidcConfig.setJwksUrl(null);
        oidcConfig.setLogoutUrl(null);
        oidcConfig.setUserInfoUrl("http://localhost");
        try {
            resource.update(representation);
            fail("Invalid URL");
        } catch (Exception e) {
            assertTrue(e instanceof ClientErrorException);
            Response response = ClientErrorException.class.cast(e).getResponse();
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
            ErrorRepresentation error = ((ClientErrorException) e).getResponse().readEntity(ErrorRepresentation.class);
            assertEquals("The url [userinfo_url] requires secure connections", error.getErrorMessage());
        }
        rau.updateWith(r -> r.setSslRequired(SslRequired.EXTERNAL.name())).update();
        resource.update(representation);
    }
}
Also used : EndpointType(org.keycloak.dom.saml.v2.metadata.EndpointType) Arrays(java.util.Arrays) ResourceType(org.keycloak.events.admin.ResourceType) OIDCIdentityProviderConfigRep(org.keycloak.testsuite.broker.OIDCIdentityProviderConfigRep) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) Matchers.not(org.hamcrest.Matchers.not) ClientErrorException(javax.ws.rs.ClientErrorException) Assert.assertThat(org.junit.Assert.assertThat) SAMLIdentityProviderConfig(org.keycloak.broker.saml.SAMLIdentityProviderConfig) MediaType(javax.ws.rs.core.MediaType) MultipartFormDataOutput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) REMOTE(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer.REMOTE) Document(org.w3c.dom.Document) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) AdminEventPaths(org.keycloak.testsuite.util.AdminEventPaths) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Set(java.util.Set) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) UUID(java.util.UUID) RealmAttributeUpdater(org.keycloak.testsuite.updaters.RealmAttributeUpdater) NotFoundException(javax.ws.rs.NotFoundException) DocumentUtil(org.keycloak.saml.common.util.DocumentUtil) IdentityProviderResource(org.keycloak.admin.client.resource.IdentityProviderResource) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) List(java.util.List) SslRequired(org.keycloak.common.enums.SslRequired) Response(javax.ws.rs.core.Response) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.equalTo(org.hamcrest.Matchers.equalTo) OIDCLoginProtocol(org.keycloak.protocol.oidc.OIDCLoginProtocol) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) XMLSignature(javax.xml.crypto.dsig.XMLSignature) SAMLParser(org.keycloak.saml.processing.core.parsers.saml.SAMLParser) OperationType(org.keycloak.events.admin.OperationType) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) Assert(org.keycloak.testsuite.Assert) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) IdentityProviderMapperModel(org.keycloak.models.IdentityProviderMapperModel) HashMap(java.util.HashMap) IdentityProviderMapperTypeRepresentation(org.keycloak.representations.idm.IdentityProviderMapperTypeRepresentation) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) AttributeType(org.keycloak.dom.saml.v2.assertion.AttributeType) HashSet(java.util.HashSet) ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException) Charset(java.nio.charset.Charset) AUTH_SERVER_SSL_REQUIRED(org.keycloak.testsuite.util.ServerURLs.AUTH_SERVER_SSL_REQUIRED) IndexedEndpointType(org.keycloak.dom.saml.v2.metadata.IndexedEndpointType) AdminEventRepresentation(org.keycloak.representations.idm.AdminEventRepresentation) IdentityProviderMapperRepresentation(org.keycloak.representations.idm.IdentityProviderMapperRepresentation) StripSecretsUtils(org.keycloak.models.utils.StripSecretsUtils) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ConfigurationException(org.keycloak.saml.common.exceptions.ConfigurationException) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) SPSSODescriptorType(org.keycloak.dom.saml.v2.metadata.SPSSODescriptorType) Matchers.empty(org.hamcrest.Matchers.empty) NodeList(org.w3c.dom.NodeList) EntityDescriptorType(org.keycloak.dom.saml.v2.metadata.EntityDescriptorType) Files(java.nio.file.Files) Assert.assertNotNull(org.junit.Assert.assertNotNull) KeyTypes(org.keycloak.dom.saml.v2.metadata.KeyTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) XMLDSIG_NSURI(org.keycloak.saml.common.constants.JBossSAMLURIConstants.XMLDSIG_NSURI) Element(org.w3c.dom.Element) Assert.assertNull(org.junit.Assert.assertNull) Paths(java.nio.file.Paths) KeyDescriptorType(org.keycloak.dom.saml.v2.metadata.KeyDescriptorType) IdentityProviderMapperSyncMode(org.keycloak.models.IdentityProviderMapperSyncMode) Assert.assertEquals(org.junit.Assert.assertEquals) Response(javax.ws.rs.core.Response) IdentityProviderResource(org.keycloak.admin.client.resource.IdentityProviderResource) OIDCIdentityProviderConfigRep(org.keycloak.testsuite.broker.OIDCIdentityProviderConfigRep) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) ClientErrorException(javax.ws.rs.ClientErrorException) RealmAttributeUpdater(org.keycloak.testsuite.updaters.RealmAttributeUpdater) URISyntaxException(java.net.URISyntaxException) ClientErrorException(javax.ws.rs.ClientErrorException) NotFoundException(javax.ws.rs.NotFoundException) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException) ConfigurationException(org.keycloak.saml.common.exceptions.ConfigurationException) IOException(java.io.IOException) Test(org.junit.Test)

Example 7 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class UserTest method updateUserWithExistingEmail.

@Test
public void updateUserWithExistingEmail() {
    final String userId = createUser();
    assertNotNull(userId);
    assertNotNull(createUser("user2", "user2@localhost"));
    UserResource user = realm.users().get(userId);
    UserRepresentation userRep = user.toRepresentation();
    assertNotNull(userRep);
    userRep.setEmail("user2@localhost");
    try {
        updateUser(user, userRep);
        fail("Expected failure - Email conflict");
    } catch (ClientErrorException e) {
        assertNotNull(e.getResponse());
        assertThat(e.getResponse().getStatus(), is(409));
        ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
        Assert.assertEquals("User exists with same username or email", error.getErrorMessage());
        assertAdminEvents.assertEmpty();
    }
}
Also used : ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) UserResource(org.keycloak.admin.client.resource.UserResource) ClientErrorException(javax.ws.rs.ClientErrorException) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 8 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class UserTest method createDuplicatedUser2.

@Test
public void createDuplicatedUser2() {
    createUser();
    UserRepresentation user = new UserRepresentation();
    user.setUsername("user2");
    user.setEmail("user1@localhost");
    try (Response response = realm.users().create(user)) {
        assertEquals(409, response.getStatus());
        assertAdminEvents.assertEmpty();
        ErrorRepresentation error = response.readEntity(ErrorRepresentation.class);
        Assert.assertEquals("User exists with same email", error.getErrorMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 9 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class UserTest method createDuplicateEmailWithExistingDuplicates.

// KEYCLOAK-14611
@Test
public void createDuplicateEmailWithExistingDuplicates() {
    // Allow duplicate emails
    RealmRepresentation rep = realm.toRepresentation();
    rep.setDuplicateEmailsAllowed(true);
    realm.update(rep);
    // Create 2 users with the same email
    UserRepresentation user = new UserRepresentation();
    user.setEmail("user1@localhost");
    user.setUsername("user1");
    createUser(user, false);
    user.setUsername("user2");
    createUser(user, false);
    // Disallow duplicate emails
    rep.setDuplicateEmailsAllowed(false);
    realm.update(rep);
    // Create a third user with the same email
    user.setUsername("user3");
    assertAdminEvents.clear();
    try (Response response = realm.users().create(user)) {
        assertEquals(409, response.getStatus());
        ErrorRepresentation error = response.readEntity(ErrorRepresentation.class);
        Assert.assertEquals("User exists with same email", error.getErrorMessage());
        assertAdminEvents.assertEmpty();
    }
}
Also used : Response(javax.ws.rs.core.Response) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 10 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class ErrorResponse method errors.

public static Response errors(List<ErrorRepresentation> s, Response.Status status, boolean shrinkSingleError) {
    if (shrinkSingleError && s.size() == 1) {
        return Response.status(status).entity(s.get(0)).type(MediaType.APPLICATION_JSON).build();
    }
    ErrorRepresentation error = new ErrorRepresentation();
    error.setErrors(s);
    if (!shrinkSingleError && s.size() == 1) {
        error.setErrorMessage(s.get(0).getErrorMessage());
        error.setParams(s.get(0).getParams());
        error.setField(s.get(0).getField());
    }
    return Response.status(status).entity(error).type(MediaType.APPLICATION_JSON).build();
}
Also used : ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation)

Aggregations

ErrorRepresentation (org.keycloak.representations.idm.ErrorRepresentation)21 Test (org.junit.Test)14 Response (javax.ws.rs.core.Response)11 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)11 ClientErrorException (javax.ws.rs.ClientErrorException)7 UserResource (org.keycloak.admin.client.resource.UserResource)5 AuthServerContainerExclude (org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude)5 LinkedList (java.util.LinkedList)4 IOException (java.io.IOException)3 List (java.util.List)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 Charset (java.nio.charset.Charset)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2