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);
}
}
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();
}
}
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());
}
}
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();
}
}
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();
}
Aggregations