8000 Prevent admins from defining a dynamic client scope with type Default by reda-alaoui · Pull Request #26976 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ private void addDefaultClientScope(String clientScopeId, boolean defaultScope) {
if (clientScope == null) {
throw new NotFoundException("Client scope not found");
}
if (defaultScope && clientScope.isDynamicScope()) {
throw new org.keycloak.services.ErrorResponseException("invalid_request", "Can't assign a Dynamic Scope to a Client as a Default Scope", Response.Status.BAD_REQUEST);
}
realm.addDefaultClientScope(clientScope, defaultScope);

adminEvent.operation(OperationType.CREATE).resource(ResourceType.CLIENT_SCOPE).resourcePath(session.getContext().getUri()).success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,36 @@ public void dynamicClientScopeCannotBeAssignedAsDefaultClientScope() {

}

@Test
@EnableFeature(value = Profile.Feature.DYNAMIC_SCOPES, skipRestart = true)
public void dynamicClientScopeCannotBeAssignedAsDefaultDefaultClientScope() {
ClientScopeRepresentation dynamicClientScope = new ClientScopeRepresentation();
dynamicClientScope.setName("dynamic-client-scope");
dynamicClientScope.setProtocol("openid-connect");
dynamicClientScope.setAttributes(new HashMap<>() {{
put(ClientScopeModel.IS_DYNAMIC_SCOPE, "true");
put(ClientScopeModel.DYNAMIC_SCOPE_REGEXP, "dynamic-scope-def:*");
}});
String clientScopeId = createClientScope(dynamicClientScope);
getCleanup().addClientScopeId(clientScopeId);

try {
testRealmResource().addDefaultDefaultClientScope(clientScopeId);
Assert.fail("A Dynamic Scope shouldn't not be used as a default default client scope");
} catch (ClientErrorException ex) {
Response response = ex.getResponse();
assertThat(response, Matchers.statusCodeIs(Status.BAD_REQUEST));

String respBody = response.readEntity(String.class);
try {
Map<String, String> responseJson = JsonSerialization.readValue(respBody, Map.class);
Assert.assertEquals("Can't assign a Dynamic Scope to a Client as a Default Scope", responseJson.get("error_description"));
} catch (IOException e) {
fail("Failed to extract the errorMessage from a CreateScope Response");
}
}
}

private void handleExpectedCreateFailure(ClientScopeRepresentation scopeRep, int expectedErrorCode, String expectedErrorMessage) {
try(Response resp = clientScopes().create(scopeRep)) {
Assert.assertEquals(expectedErrorCode, resp.getStatus());
Expand Down
0