Skip to content

Commit

Permalink
The export of ClientAuthorizations does no longer include IDs so we n…
Browse files Browse the repository at this point in the history
…ow search for the name.

Signed-off-by: Jonas Voelcker <barmer@jonas-voelcker.de>
  • Loading branch information
jonasvoelcker committed Jun 18, 2024
1 parent e3a7d68 commit 0469ae0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,24 @@ public void createAuthorizationResource(String realmName, String id, ResourceRep

public void updateAuthorizationResource(String realmName, String id, ResourceRepresentation resource) {
ClientResource clientResource = getResourceById(realmName, id);
clientResource.authorization().resources().resource(resource.getId()).update(resource);
String resourceId = getResourceId(clientResource, resource.getName());
clientResource.authorization().resources().resource(resourceId).update(resource);
}

public void removeAuthorizationResource(String realmName, String id, String resourceId) {
public void removeAuthorizationResource(String realmName, String id, String resourceName) {
ClientResource clientResource = getResourceById(realmName, id);
clientResource.authorization().resources().resource(resourceId).remove();
String resourceId = getResourceId(clientResource, resourceName);
if (resourceId != null) {
clientResource.authorization().resources().resource(resourceId).remove();
}
}

private String getResourceId(ClientResource clientResource, String resourceName) {
return clientResource.authorization().resources().resources().stream()
.filter(resource -> resourceName.equals(resource.getName()))
.findFirst()
.map(ResourceRepresentation::getId)
.orElse(null);
}

public void addAuthorizationScope(String realmName, String id, String name) {
Expand All @@ -200,12 +212,24 @@ public void addAuthorizationScope(String realmName, String id, String name) {

public void updateAuthorizationScope(String realmName, String id, ScopeRepresentation scope) {
ClientResource clientResource = getResourceById(realmName, id);
clientResource.authorization().scopes().scope(scope.getId()).update(scope);
String scopeId = getScopeId(clientResource, scope.getName());
clientResource.authorization().scopes().scope(scopeId).update(scope);
}

public void removeAuthorizationScope(String realmName, String id, String scopeId) {
public void removeAuthorizationScope(String realmName, String id, String scopeName) {
ClientResource clientResource = getResourceById(realmName, id);
clientResource.authorization().scopes().scope(scopeId).remove();
String scopeId = getScopeId(clientResource, scopeName);
if (scopeId != null) {
clientResource.authorization().scopes().scope(scopeId).remove();
}
}

private String getScopeId(ClientResource clientResource, String scopeName) {
return clientResource.authorization().scopes().scopes().stream()
.filter(scope -> scopeName.equals(scope.getName()))
.findFirst()
.map(ScopeRepresentation::getId)
.orElse(null);
}

public void createAuthorizationPolicy(String realmName, String id, PolicyRepresentation policy) {
Expand All @@ -218,12 +242,24 @@ public void createAuthorizationPolicy(String realmName, String id, PolicyReprese

public void updateAuthorizationPolicy(String realmName, String id, PolicyRepresentation policy) {
ClientResource clientResource = getResourceById(realmName, id);
clientResource.authorization().policies().policy(policy.getId()).update(policy);
String policyId = getPolicyId(clientResource, policy.getName());
clientResource.authorization().policies().policy(policyId).update(policy);
}

public void removeAuthorizationPolicy(String realmName, String id, String policyId) {
public void removeAuthorizationPolicy(String realmName, String id, String policyName) {
ClientResource clientResource = getResourceById(realmName, id);
clientResource.authorization().policies().policy(policyId).remove();
String policyId = getPolicyId(clientResource, policyName);
if (policyId != null) {
clientResource.authorization().policies().policy(policyId).remove();
}
}

private String getPolicyId(ClientResource clientResource, String policyName) {
return clientResource.authorization().policies().policies().stream()
.filter(policy -> policyName.equals(policy.getName()))
.findFirst()
.map(PolicyRepresentation::getId)
.orElse(null);
}

public void addScopeMapping(String realmName, String clientId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ private void removeAuthorizationResource(
existingClientAuthorizationResource.getName(), getClientIdentifier(client), realmName
);
clientRepository.removeAuthorizationResource(
realmName, client.getId(), existingClientAuthorizationResource.getId()
realmName, client.getId(), existingClientAuthorizationResource.getName()
);
}

Expand Down Expand Up @@ -430,7 +430,7 @@ private void removeAuthorizationScope(
logger.debug("Remove authorization scope '{}' for client '{}' in realm '{}'",
existingClientAuthorizationScope.getName(), getClientIdentifier(client), realmName);

clientRepository.removeAuthorizationScope(realmName, client.getId(), existingClientAuthorizationScope.getId());
clientRepository.removeAuthorizationScope(realmName, client.getId(), existingClientAuthorizationScope.getName());
}

private void createOrUpdateAuthorizationPolicies(
Expand Down Expand Up @@ -519,7 +519,7 @@ private void removeAuthorizationPolicy(

try {
clientRepository.removeAuthorizationPolicy(
realmName, client.getId(), existingClientAuthorizationPolicy.getId()
realmName, client.getId(), existingClientAuthorizationPolicy.getName()
);
} catch (NotFoundException ignored) {
// policies got deleted if linked resources are deleted, too.
Expand Down

0 comments on commit 0469ae0

Please sign in to comment.