From e623f644d1a410a95edf9c0d66085dd4503e7cb3 Mon Sep 17 00:00:00 2001 From: pujavs <43700552+pujavs@users.noreply.github.com> Date: Fri, 7 Oct 2022 22:02:07 +0530 Subject: [PATCH] fix(config-api): fix for returning associated-clients for scope (#2567) * fix(config-api): fix for returning associated-clients for scope * fix(config-api): fix for returning associated-clients for scope --- .../configapi/rest/model/CustomScope.java | 7 ++ .../docs/jans-config-api-swagger-auto.yaml | 22 ++--- .../configapi/service/auth/ScopeService.java | 94 +++++++++++++------ 3 files changed, 84 insertions(+), 39 deletions(-) diff --git a/jans-config-api/common/src/main/java/io/jans/configapi/rest/model/CustomScope.java b/jans-config-api/common/src/main/java/io/jans/configapi/rest/model/CustomScope.java index f07e3e57b5b..ce1c6b81b4d 100644 --- a/jans-config-api/common/src/main/java/io/jans/configapi/rest/model/CustomScope.java +++ b/jans-config-api/common/src/main/java/io/jans/configapi/rest/model/CustomScope.java @@ -18,4 +18,11 @@ public void setClients(List clients) { private List clients; + @Override + public String toString() { + return "CustomScope [clients=" + clients + "]"; + } + + + } diff --git a/jans-config-api/docs/jans-config-api-swagger-auto.yaml b/jans-config-api/docs/jans-config-api-swagger-auto.yaml index dbb4d39d2d8..e5f39debe13 100644 --- a/jans-config-api/docs/jans-config-api-swagger-auto.yaml +++ b/jans-config-api/docs/jans-config-api-swagger-auto.yaml @@ -2947,17 +2947,17 @@ components: type: string whitePagesCanView: type: boolean - adminCanAccess: - type: boolean - userCanEdit: - type: boolean adminCanEdit: type: boolean adminCanView: type: boolean + userCanAccess: + type: boolean userCanView: type: boolean - userCanAccess: + adminCanAccess: + type: boolean + userCanEdit: type: boolean baseDn: type: string @@ -3284,6 +3284,8 @@ components: format: int32 displayName: type: string + tokenBindingSupported: + type: boolean authenticationMethod: type: string enum: @@ -3295,8 +3297,6 @@ components: - tls_client_auth - self_signed_tls_client_auth - none - tokenBindingSupported: - type: boolean baseDn: type: string inum: @@ -4090,6 +4090,8 @@ components: $ref: '#/components/schemas/EngineConfig' ssaConfiguration: $ref: '#/components/schemas/SsaConfiguration' + fapi: + type: boolean enabledFeatureFlags: uniqueItems: true type: array @@ -4117,8 +4119,6 @@ components: - STAT - PAR - SSA - fapi: - type: boolean allResponseTypesSupported: uniqueItems: true type: array @@ -4384,13 +4384,13 @@ components: type: boolean internal: type: boolean + locationPath: + type: string locationType: type: string enum: - ldap - file - locationPath: - type: string baseDn: type: string ScriptError: diff --git a/jans-config-api/server/src/main/java/io/jans/configapi/service/auth/ScopeService.java b/jans-config-api/server/src/main/java/io/jans/configapi/service/auth/ScopeService.java index fa6381d94eb..52efb47f917 100644 --- a/jans-config-api/server/src/main/java/io/jans/configapi/service/auth/ScopeService.java +++ b/jans-config-api/server/src/main/java/io/jans/configapi/service/auth/ScopeService.java @@ -6,7 +6,6 @@ package io.jans.configapi.service.auth; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.api.client.util.Lists; import io.jans.as.common.model.registration.Client; import io.jans.as.common.service.OrganizationService; @@ -97,9 +96,8 @@ public CustomScope getScopeByInum(String inum, boolean withAssociatedClients) { try { CustomScope scope = persistenceEntryManager.find(CustomScope.class, getDnForScope(inum)); if (withAssociatedClients) { - List clients = clientService.getAllClients(); - List umaResources = umaResourceService.getAllResources(); - return setClients(scope, clients, umaResources); + + return setClients(scope); } return scope; } catch (Exception e) { @@ -206,43 +204,78 @@ public List searchScope(SearchRequest searchRequest) { return Collections.emptyList(); } - private CustomScope setClients(Scope scope, List clients, List umaResources) { - logger.debug("Search Scope with associated clients - scope:{}, clients:{}, umaResources:{}", scope, clients, - umaResources); + public List getAllScopesList() { + String scopesBaseDN = staticConfiguration.getBaseDn().getScopes(); + + return persistenceEntryManager.findEntries(scopesBaseDN, Scope.class, Filter.createPresenceFilter("inum")); + } + + public List getDefaultScopesDn() { + List defaultScopes = new ArrayList<>(); + + for (Scope scope : getAllScopesList()) { + if (Boolean.TRUE.equals(scope.isDefaultScope())) { + defaultScopes.add(scope.getDn()); + } + } + + return defaultScopes; + } + + public List getScopesDn(List scopeDnList) { + List scopes = new ArrayList<>(); + + for (String scopeDn : scopeDnList) { + Scope scope = getScopeByDn(scopeDn); + if (scope != null) { + scopes.add(scope.getDn()); + } + } + + return scopes; + } + + private CustomScope setClients(CustomScope customScope) { + logger.debug("Getting associated-clients for scope - customScope:{}", customScope); - ObjectMapper mapper = new ObjectMapper(); - CustomScope customScope = mapper.convertValue(scope, CustomScope.class); + List clients = clientService.getAllClients(); + List umaResources = umaResourceService.getAllResources(); + logger.debug("Verifying associated-clients using clients:{}, umaResources:{}", clients, umaResources); customScope.setClients(Lists.newArrayList()); for (Client client : clients) { logger.debug( - "Associated clients serach - scope.getScopeType():{}, scope.getInum():{}, scope.getCreatorId():{}, client.getClientId():{}, clientService.getDnForClient(client.getClientId()):{}, client.getScopes():{}, client.getClientId().equals(scope.getCreatorId()):{}", - scope.getScopeType(), scope.getInum(), scope.getCreatorId(), client.getClientId(), + "Associated clients search - customScope.getScopeType():{}, customScope.getInum():{}, customScope.getCreatorId():{}, client.getClientId():{}, clientService.getDnForClient(client.getClientId()):{}, client.getScopes():{}, client.getClientId().equals(customScope.getCreatorId()):{}", + customScope.getScopeType(), customScope.getInum(), customScope.getCreatorId(), client.getClientId(), clientService.getDnForClient(client.getClientId()), client.getScopes(), - client.getClientId().equals(scope.getCreatorId())); + client.getClientId().equals(customScope.getCreatorId())); - if (scope.getScopeType() == ScopeType.OPENID || scope.getScopeType() == ScopeType.OAUTH - || scope.getScopeType() == ScopeType.DYNAMIC) { + if (customScope.getScopeType() == ScopeType.OPENID || customScope.getScopeType() == ScopeType.OAUTH + || customScope.getScopeType() == ScopeType.DYNAMIC) { if (client.getScopes() != null - && Arrays.asList(client.getScopes()).contains(getDnForScope(scope.getInum()))) { + && Arrays.asList(client.getScopes()).contains(getDnForScope(customScope.getInum()))) { + logger.debug( + "Associated clients match for OOD - customScope.getScopeType():{}, customScope.getInum():{},client.getClientId():{}", + customScope.getScopeType(), customScope.getInum(), client.getClientId()); customScope.getClients().add(client); } - } else if (scope.getScopeType() == ScopeType.UMA) { + } else if (customScope.getScopeType() == ScopeType.UMA) { List umaRes = umaResources.stream() .filter(umaResource -> (umaResource.getScopes() != null - && umaResource.getScopes().contains(getDnForScope(scope.getInum())))) + && umaResource.getScopes().contains(getDnForScope(customScope.getInum())))) .collect(Collectors.toList()); - logger.trace("Associated clients serach - umaRes():{}", umaRes); - for (UmaResource res : umaRes) { - logger.trace( - " client.getDn():{}, res.getInum():{}, res.getClients():{}, res.getClients().contains(clientService.getDnForClient(client.getClientId()):{}", - client.getDn(), res.getInum(), res.getClients(), - res.getClients().contains(clientService.getDnForClient(client.getClientId()))); + logger.trace("Associated clients search - umaRes():{}", umaRes); + if (umaRes.stream().anyMatch( + ele -> ele.getClients().contains(clientService.getDnForClient(client.getClientId())))) { customScope.getClients().add(client); } - } else if ((scope.getScopeType() == ScopeType.SPONTANEOUS) - && (client.getClientId().equals(scope.getCreatorId()))) { + } else if ((customScope.getScopeType() == ScopeType.SPONTANEOUS) + && (client.getClientId().equals(customScope.getCreatorId()))) { + logger.debug( + "Associated clients match for SPONTANEOUS - customScope.getScopeType():{}, customScope.getInum():{},customScope.getCreatorId():{}, client.getClientId():{}", + customScope.getScopeType(), customScope.getInum(), customScope.getCreatorId(), + client.getClientId()); customScope.getClients().add(client); } } @@ -254,9 +287,14 @@ public List getAssociatedClients(List scopes) { if (scopes == null) { return scopes; } - List clients = clientService.getAllClients(); - List umaResources = umaResourceService.getAllResources(); - return (scopes.stream().map(scope -> setClients(scope, clients, umaResources)).collect(Collectors.toList())); + + List scopeList = Lists.newArrayList(); + for (CustomScope scope : scopes) { + scopeList.add(setClients(scope)); + } + + logger.debug("Getting associatedClients for scopeList:{}", scopeList); + return scopeList; }