Skip to content

Commit

Permalink
feat(jans-config-api): fetch the associated clients_id in GET scopes …
Browse files Browse the repository at this point in the history
…api response (#1946)

* feat: need to fetch the associated clients_id in GET scopes api response #1923

* feat: need to fetch the associated clients_id in GET scopes api response #1923

* feat: need to fetch the associated clients_id in GET scopes api response #1923
  • Loading branch information
duttarnab committed Jul 29, 2022
1 parent 9074482 commit ffe743c
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.jans.configapi.rest.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.jans.as.common.model.registration.Client;
import io.jans.as.persistence.model.Scope;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class CustomScope extends Scope {
public List<Client> getClients() {
return clients;
}

public void setClients(List<Client> clients) {
this.clients = clients;
}

private List<Client> clients;

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ private ApiConstants() {}
public static final String SERVER_STAT = "/server-stat";
public static final String USERNAME_PATH = "/{username}";
public static final String CLIENTID_PATH = "/{clientId}";
public static final String AGAMA = "/agama";
public static final String AGAMA = "/agama";
public static final String QNAME_PATH = "{qname}";
public static final String ENABLED = "enabled";
public static final String QNAME = "qname";

public static final String LIMIT = "limit";
public static final String START_INDEX = "startIndex";
public static final String PATTERN = "pattern";
public static final String WITH_ASSOCIATED_CLIENTS = "withAssociatedClients";
public static final String STATUS = "status";
public static final String INUM = "inum";
public static final String ID = "id";
Expand Down
12 changes: 12 additions & 0 deletions jans-config-api/docs/jans-config-api-swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,12 @@ paths:
in: query
name: pattern
description: Search pattern.
- schema:
type: boolean
default: false
in: query
name: withAssociatedClients
description: Also fetch associated clients with scopes.
post:
tags:
- OAuth - Scopes
Expand Down Expand Up @@ -2353,6 +2359,12 @@ paths:
name: inum
in: path
required: true
- schema:
type: boolean
default: false
in: query
name: withAssociatedClients
description: Also fetch associated clients with scopes.
get:
tags:
- OAuth - Scopes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.jans.as.model.common.ScopeType;
import io.jans.as.persistence.model.Scope;
import io.jans.configapi.core.rest.ProtectedApi;
import io.jans.configapi.rest.model.CustomScope;
import io.jans.configapi.service.auth.ScopeService;
import io.jans.configapi.util.ApiAccessConstants;
import io.jans.configapi.util.ApiConstants;
Expand Down Expand Up @@ -58,23 +59,25 @@ public class ScopesResource extends ConfigBaseResource {
@ProtectedApi(scopes = { ApiAccessConstants.SCOPES_READ_ACCESS })
public Response getScopes(@DefaultValue("") @QueryParam(ApiConstants.TYPE) String type,
@DefaultValue(DEFAULT_LIST_SIZE) @QueryParam(value = ApiConstants.LIMIT) int limit,
@DefaultValue("") @QueryParam(value = ApiConstants.PATTERN) String pattern) {
@DefaultValue("") @QueryParam(value = ApiConstants.PATTERN) String pattern,
@DefaultValue("false") @QueryParam(value = ApiConstants.WITH_ASSOCIATED_CLIENTS) boolean withAssociatedClients) {
log.debug("SCOPES to be fetched type = " + type + " , limit = " + limit + " , pattern = " + pattern);
final List<Scope> scopes;
final List<CustomScope> scopes;
if (StringHelper.isNotEmpty(pattern)) {
scopes = scopeService.searchScopes(pattern, limit, type);
scopes = scopeService.searchScopes(pattern, limit, type, withAssociatedClients);
} else {
scopes = scopeService.getAllScopesList(limit, type);
scopes = scopeService.getAllScopesList(limit, type, withAssociatedClients);
}
return Response.ok(scopes).build();
}

@GET
@ProtectedApi(scopes = { ApiAccessConstants.SCOPES_READ_ACCESS })
@Path(ApiConstants.INUM_PATH)
public Response getScopeById(@NotNull @PathParam(ApiConstants.INUM) String inum) {
public Response getScopeById(@NotNull @PathParam(ApiConstants.INUM) String inum,
@DefaultValue("false") @QueryParam(value = ApiConstants.WITH_ASSOCIATED_CLIENTS) boolean withAssociatedClients) {
log.debug("SCOPES to be fetched - inum = " + inum);
Scope scope = scopeService.getScopeByInum(inum);
CustomScope scope = scopeService.getScopeByInum(inum, withAssociatedClients);
checkResourceNotNull(scope, SCOPE);
return Response.ok(scope).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@

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;
import io.jans.as.common.util.AttributeConstants;
import io.jans.as.model.common.ScopeType;
import io.jans.as.model.config.StaticConfiguration;
import io.jans.as.model.uma.persistence.UmaResource;
import io.jans.as.persistence.model.Scope;
import io.jans.configapi.rest.model.CustomScope;
import io.jans.orm.PersistenceEntryManager;
import io.jans.orm.search.filter.Filter;
import io.jans.util.StringHelper;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Responsible for OpenID Connect, OAuth2 and UMA scopes. (Type is defined by
Expand All @@ -42,6 +50,12 @@ public class ScopeService {
@Inject
OrganizationService organizationService;

@Inject
ClientService clientService;

@Inject
UmaResourceService umaResourceService;

public String baseDn() {
return staticConfiguration.getBaseDn().getScopes();
}
Expand Down Expand Up @@ -70,9 +84,19 @@ public void updateScope(Scope scope) {
persistenceEntryManager.merge(scope);
}

public Scope getScopeByInum(String inum) {
public CustomScope getScopeByInum(String inum){
return getScopeByInum(inum, false);
}

public CustomScope getScopeByInum(String inum, boolean withAssociatedClients) {
try {
return persistenceEntryManager.find(Scope.class, getDnForScope(inum));
CustomScope scope = persistenceEntryManager.find(CustomScope.class, getDnForScope(inum));
if (withAssociatedClients) {
List<Client> clients = clientService.getAllClients();
List<UmaResource> umaResources = umaResourceService.getAllResources();
return setClients(scope, clients, umaResources);
}
return scope;
} catch (Exception e) {
return null;
}
Expand All @@ -86,7 +110,7 @@ public String getDnForScope(String inum) {
return String.format("inum=%s,ou=scopes,%s", inum, orgDn);
}

public List<Scope> searchScopes(String pattern, int sizeLimit) {
public List<CustomScope> searchScopes(String pattern, int sizeLimit) {
return searchScopes(pattern, sizeLimit, null);
}

Expand All @@ -99,13 +123,17 @@ public List<Scope> searchScopesById(String jsId) {
return new ArrayList<>();
}
}

public Scope getScopeByDn(String dn) {
return persistenceEntryManager.find(Scope.class, dn);
}

public List<Scope> searchScopes(String pattern, int sizeLimit, String scopeType) {
String[] targetArray = new String[] { pattern };
public List<CustomScope> searchScopes(String pattern, int sizeLimit, String scopeType) {
return searchScopes(pattern, sizeLimit, scopeType, false);
}

public List<CustomScope> searchScopes(String pattern, int sizeLimit, String scopeType, boolean withAssociatedClients) {
String[] targetArray = new String[]{pattern};
Filter displayNameFilter = Filter.createSubstringFilter(AttributeConstants.DISPLAY_NAME, null, targetArray,
null);
Filter descriptionFilter = Filter.createSubstringFilter(AttributeConstants.DESCRIPTION, null, targetArray,
Expand All @@ -115,22 +143,70 @@ public List<Scope> searchScopes(String pattern, int sizeLimit, String scopeType)
searchFilter = Filter.createANDFilter(Filter.createEqualityFilter("jansScopeTyp", scopeType), searchFilter);
}
try {
return persistenceEntryManager.findEntries(getDnForScope(null), Scope.class, searchFilter, sizeLimit);
List<CustomScope> scopes = persistenceEntryManager.findEntries(getDnForScope(null), CustomScope.class, searchFilter, sizeLimit);

if (withAssociatedClients) {
List<Client> clients = clientService.getAllClients();
List<UmaResource> umaResources = umaResourceService.getAllResources();
List<CustomScope> custScopes = scopes.stream().map(scope -> setClients(scope, clients, umaResources)).collect(Collectors.toList());
return custScopes;
}

return scopes;
} catch (Exception e) {
logger.error("No scopes found by pattern: " + pattern, e);
return new ArrayList<>();
}
}

public List<Scope> getAllScopesList(int size) {
public List<CustomScope> getAllScopesList(int size) {
return getAllScopesList(size, null);
}

public List<Scope> getAllScopesList(int size, String scopeType) {
public List<CustomScope> getAllScopesList(int size, String scopeType) {
return getAllScopesList(size, scopeType, false);
}

public List<CustomScope> getAllScopesList(int size, String scopeType, boolean withAssociatedClients) {
Filter searchFilter = null;
if (StringHelper.isNotEmpty(scopeType)) {
searchFilter = Filter.createEqualityFilter("jansScopeTyp", scopeType);
}
return persistenceEntryManager.findEntries(getDnForScope(null), Scope.class, searchFilter, size);
List<CustomScope> scopes = persistenceEntryManager.findEntries(getDnForScope(null), CustomScope.class, searchFilter, size);

if (withAssociatedClients) {
List<Client> clients = clientService.getAllClients();
List<UmaResource> umaResources = umaResourceService.getAllResources();
List<CustomScope> custScopes = scopes.stream().map(scope -> setClients(scope, clients, umaResources)).collect(Collectors.toList());
return custScopes;
}
return scopes;
}

private CustomScope setClients(Scope scope, List<Client> clients, List<UmaResource> umaResources) {
ObjectMapper mapper = new ObjectMapper();
CustomScope customScope = mapper.convertValue(scope, CustomScope.class);
customScope.setClients(Lists.newArrayList());

for (Client client : clients) {
if (client.getScopes() == null) {
continue;
}
if (scope.getScopeType() == ScopeType.OPENID || scope.getScopeType() == ScopeType.OAUTH || scope.getScopeType() == ScopeType.DYNAMIC) {
if (Arrays.asList(client.getScopes()).contains(getDnForScope(scope.getInum()))) {
customScope.getClients().add(client);
}
} else if (scope.getScopeType() == ScopeType.UMA) {
List<UmaResource> umaRes = umaResources.stream().filter(umaResource -> (umaResource.getScopes() != null && umaResource.getScopes().contains(getDnForScope(scope.getInum())))).collect(Collectors.toList());
if (umaRes.stream().anyMatch(ele -> ele.getClients().contains(clientService.getDnForClient(client.getClientId())))) {
customScope.getClients().add(client);
}
} else if (scope.getScopeType() == ScopeType.SPONTANEOUS) {
if (client.getClientId().equals(customScope.getAttributes().getSpontaneousClientId())) {
customScope.getClients().add(client);
}
}
}
return customScope;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public List<UmaResource> getAllResources(int sizeLimit) {
return persistenceEntryManager.findEntries(getDnForResource(null), UmaResource.class, null, sizeLimit);
}

public List<UmaResource> getAllResources() {
return persistenceEntryManager.findEntries(getDnForResource(null), UmaResource.class, null);
}

public void addResource(UmaResource resource) {
persistenceEntryManager.persist(resource);
}
Expand Down

0 comments on commit ffe743c

Please sign in to comment.