Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jans-auth-server): added dynamicRegistrationDefaultCustomAttributes to provide default custom attributes during dcr #3595 #3596

Merged
merged 1 commit into from
Jan 10, 2023
Merged
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 @@ -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.*;
Expand Down Expand Up @@ -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<String> 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<String> 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<String> displayValuesSupported;

Expand Down Expand Up @@ -1844,6 +1848,14 @@ public void setTokenEndpointAuthSigningAlgValuesSupported(List<String> tokenEndp
this.tokenEndpointAuthSigningAlgValuesSupported = tokenEndpointAuthSigningAlgValuesSupported;
}

public JsonNode getDynamicRegistrationDefaultCustomAttributes() {
return dynamicRegistrationDefaultCustomAttributes;
}

public void setDynamicRegistrationDefaultCustomAttributes(JsonNode dynamicRegistrationDefaultCustomAttributes) {
this.dynamicRegistrationDefaultCustomAttributes = dynamicRegistrationDefaultCustomAttributes;
}

public List<String> getDynamicRegistrationCustomAttributes() {
return dynamicRegistrationCustomAttributes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -400,13 +401,41 @@ private void putCustomStuffIntoObject(Client client, JSONObject requestObject) t
return;
}

addDefaultCustomAttributes(requestObject);

for (String attr : attrList) {
if (requestObject.has(attr)) {
addCustomAttribute(client, requestObject, attr);
}
}
}

public void addDefaultCustomAttributes(JSONObject requestObject) {
final JsonNode node = appConfiguration.getDynamicRegistrationDefaultCustomAttributes();
final List<String> allowed = appConfiguration.getDynamicRegistrationCustomAttributes();
if (allowed == null || allowed.isEmpty() || node == null || node.isEmpty()) {
return;
}

final Iterator<String> 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<String> parameterValues = parameterValuesJsonArray != null ?
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}