From 6202230fceec5b72f1541e70cf0833f58bc9ab76 Mon Sep 17 00:00:00 2001 From: YuriyZ Date: Tue, 10 Jan 2023 22:11:23 +0200 Subject: [PATCH] feat(jans-auth-server): added dynamicRegistrationDefaultCustomAttributes to provide default custom attributes during dcr #3595 (#3596) --- .../model/configuration/AppConfiguration.java | 14 ++- .../register/ws/rs/RegisterService.java | 29 +++++++ .../register/ws/rs/RegisterServiceTest.java | 86 +++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 jans-auth-server/server/src/test/java/io/jans/as/server/register/ws/rs/RegisterServiceTest.java diff --git a/jans-auth-server/model/src/main/java/io/jans/as/model/configuration/AppConfiguration.java b/jans-auth-server/model/src/main/java/io/jans/as/model/configuration/AppConfiguration.java index cdaea347375..394cf3b7f42 100644 --- a/jans-auth-server/model/src/main/java/io/jans/as/model/configuration/AppConfiguration.java +++ b/jans-auth-server/model/src/main/java/io/jans/as/model/configuration/AppConfiguration.java @@ -7,6 +7,7 @@ package io.jans.as.model.configuration; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.databind.JsonNode; import com.google.common.collect.Lists; import io.jans.agama.model.EngineConfig; import io.jans.as.model.common.*; @@ -256,9 +257,12 @@ public class AppConfiguration implements Configuration { @DocProperty(description = "A list of the JWS signing algorithms (alg values) supported by the Token Endpoint for the signature on the JWT used to authenticate the Client at the Token Endpoint for the private_key_jwt and client_secret_jwt authentication methods") private List tokenEndpointAuthSigningAlgValuesSupported; - @DocProperty(description = "This list details the custom attributes for dynamic registration") + @DocProperty(description = "This list details the custom attributes allowed for dynamic registration") private List dynamicRegistrationCustomAttributes; + @DocProperty(description = "This map provides default custom attributes with values for dynamic registration") + private JsonNode dynamicRegistrationDefaultCustomAttributes; + @DocProperty(description = "A list of the display parameter values that the OpenID Provider supports") private List displayValuesSupported; @@ -1844,6 +1848,14 @@ public void setTokenEndpointAuthSigningAlgValuesSupported(List tokenEndp this.tokenEndpointAuthSigningAlgValuesSupported = tokenEndpointAuthSigningAlgValuesSupported; } + public JsonNode getDynamicRegistrationDefaultCustomAttributes() { + return dynamicRegistrationDefaultCustomAttributes; + } + + public void setDynamicRegistrationDefaultCustomAttributes(JsonNode dynamicRegistrationDefaultCustomAttributes) { + this.dynamicRegistrationDefaultCustomAttributes = dynamicRegistrationDefaultCustomAttributes; + } + public List getDynamicRegistrationCustomAttributes() { return dynamicRegistrationCustomAttributes; } diff --git a/jans-auth-server/server/src/main/java/io/jans/as/server/register/ws/rs/RegisterService.java b/jans-auth-server/server/src/main/java/io/jans/as/server/register/ws/rs/RegisterService.java index 782cf3aaccf..efc197b4521 100644 --- a/jans-auth-server/server/src/main/java/io/jans/as/server/register/ws/rs/RegisterService.java +++ b/jans-auth-server/server/src/main/java/io/jans/as/server/register/ws/rs/RegisterService.java @@ -5,6 +5,7 @@ */ package io.jans.as.server.register.ws.rs; +import com.fasterxml.jackson.databind.JsonNode; import com.google.common.collect.Lists; import io.jans.as.client.RegisterRequest; import io.jans.as.common.model.registration.Client; @@ -400,6 +401,8 @@ private void putCustomStuffIntoObject(Client client, JSONObject requestObject) t return; } + addDefaultCustomAttributes(requestObject); + for (String attr : attrList) { if (requestObject.has(attr)) { addCustomAttribute(client, requestObject, attr); @@ -407,6 +410,32 @@ private void putCustomStuffIntoObject(Client client, JSONObject requestObject) t } } + public void addDefaultCustomAttributes(JSONObject requestObject) { + final JsonNode node = appConfiguration.getDynamicRegistrationDefaultCustomAttributes(); + final List allowed = appConfiguration.getDynamicRegistrationCustomAttributes(); + if (allowed == null || allowed.isEmpty() || node == null || node.isEmpty()) { + return; + } + + final Iterator fieldNames = node.fieldNames(); + while (fieldNames.hasNext()) { + String key = fieldNames.next(); + if (!allowed.contains(key)) { + continue; + } + final JsonNode value = node.get(key); + if (value.isBoolean()) { + requestObject.put(key, value.booleanValue()); + } else if (value.isTextual()) { + requestObject.put(key, value.textValue()); + } else if (value.isNumber()) { + requestObject.put(key, value.numberValue()); + } else if (value.isDouble()) { + requestObject.put(key, value.asDouble()); + } + } + } + private void addCustomAttribute(Client client, JSONObject requestObject, String attr) { final JSONArray parameterValuesJsonArray = requestObject.optJSONArray(attr); final List parameterValues = parameterValuesJsonArray != null ? diff --git a/jans-auth-server/server/src/test/java/io/jans/as/server/register/ws/rs/RegisterServiceTest.java b/jans-auth-server/server/src/test/java/io/jans/as/server/register/ws/rs/RegisterServiceTest.java new file mode 100644 index 00000000000..d44fdeb0f10 --- /dev/null +++ b/jans-auth-server/server/src/test/java/io/jans/as/server/register/ws/rs/RegisterServiceTest.java @@ -0,0 +1,86 @@ +package io.jans.as.server.register.ws.rs; + +import com.beust.jcommander.internal.Lists; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.jans.as.common.service.AttributeService; +import io.jans.as.model.configuration.AppConfiguration; +import io.jans.as.model.error.ErrorResponseFactory; +import io.jans.as.server.ciba.CIBARegisterClientMetadataService; +import io.jans.as.server.service.ScopeService; +import org.json.JSONObject; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.testng.MockitoTestNGListener; +import org.slf4j.Logger; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertTrue; + +/** + * @author Yuriy Z + */ +@Listeners(MockitoTestNGListener.class) +public class RegisterServiceTest { + + @InjectMocks + @Spy + private RegisterService registerService; + + @Mock + private AppConfiguration appConfiguration; + + @Mock + private Logger log; + + @Mock + private ScopeService scopeService; + + @Mock + private ErrorResponseFactory errorResponseFactory; + + @Mock + private AttributeService attributeService; + + @Mock + private CIBARegisterClientMetadataService cibaRegisterClientMetadataService; + + @Test + public void addDefaultCustomAttributes_whenCalledWithExistingConfigurationData_shouldPopulateRequestObject() throws JsonProcessingException { + final ObjectMapper mapper = new ObjectMapper(); + final JsonNode node = mapper.readTree("{\"jansInclClaimsInIdTkn\": true, \"jansTrustedClnt\": true}"); + when(appConfiguration.getDynamicRegistrationCustomAttributes()).thenReturn(Lists.newArrayList("jansInclClaimsInIdTkn", "jansTrustedClnt")); + when(appConfiguration.getDynamicRegistrationDefaultCustomAttributes()).thenReturn(node); + + JSONObject requestObject = new JSONObject(); + registerService.addDefaultCustomAttributes(requestObject); + + assertTrue(requestObject.getBoolean("jansInclClaimsInIdTkn")); + assertTrue(requestObject.getBoolean("jansTrustedClnt")); + } + + @Test + public void addDefaultCustomAttributes_whenCustomAttributePropertyIsEmpty_shouldNotTakeEffect() throws JsonProcessingException { + final ObjectMapper mapper = new ObjectMapper(); + final JsonNode node = mapper.readTree("{\"jansInclClaimsInIdTkn\": true, \"jansTrustedClnt\": true}"); + when(appConfiguration.getDynamicRegistrationCustomAttributes()).thenReturn(Lists.newArrayList()); + when(appConfiguration.getDynamicRegistrationDefaultCustomAttributes()).thenReturn(node); + + JSONObject requestObject = new JSONObject(); + registerService.addDefaultCustomAttributes(requestObject); + + assertTrue(requestObject.isEmpty()); + } + + @Test + public void addDefaultCustomAttributes_whenCalledWithNoData_shouldNotTakeEffect() { + JSONObject requestObject = new JSONObject(); + registerService.addDefaultCustomAttributes(requestObject); + + assertTrue(requestObject.isEmpty()); + } +}