diff --git a/pom.xml b/pom.xml index 4b7e9b8f721..f7ff525918e 100644 --- a/pom.xml +++ b/pom.xml @@ -243,6 +243,11 @@ com.fasterxml.jackson.core jackson-databind + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr353 + com.io7m.xom diff --git a/src/main/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderConfigurationParser.java b/src/main/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderConfigurationParser.java index e0cd9d6a6da..608ea83fdd9 100644 --- a/src/main/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderConfigurationParser.java +++ b/src/main/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderConfigurationParser.java @@ -1,6 +1,8 @@ package edu.harvard.iq.dataverse.authorization.providers; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.datatype.jsr353.JSR353Module; import edu.harvard.iq.dataverse.authorization.exceptions.AuthenticationProviderConfigurationException; import org.everit.json.schema.Schema; import org.everit.json.schema.ValidationException; @@ -9,6 +11,8 @@ import org.json.JSONObject; import org.json.JSONTokener; +import javax.json.Json; +import javax.json.JsonObject; import java.io.IOException; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; @@ -44,6 +48,11 @@ public static boolean validate(String rawJson) throws AuthenticationProviderConf } } + public static JsonObject serialize(AuthenticationProviderConfiguration config) { + mapper.registerModule(new JSR353Module()); + return mapper.convertValue(config, JsonObject.class); + } + /** * Fetch the schema and process into usable validator based on https://github.com/everit-org/json-schema * @return The schema object, to be used as validator diff --git a/src/main/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderOptions.java b/src/main/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderOptions.java index 792843f032e..449121f1151 100644 --- a/src/main/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderOptions.java +++ b/src/main/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderOptions.java @@ -66,6 +66,15 @@ public boolean equals(Object o) { return false; } + public String toString() { + return + "client_id: "+this.oauthClientId+", "+ + "client_secret: "+this.oauthClientSecret+", "+ + "user_endpoint: "+this.oauthUserEndpoint+", "+ + "issuer: "+this.oidcIssuerUrl+", "+ + "passive_login: "+this.shibPassiveLogin; + } + public static class AuthenticationProviderOptionsConverter implements AttributeConverter { @Override public String convertToDatabaseColumn(AuthenticationProviderOptions authenticationProviderOptions) { diff --git a/src/test/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderConfigurationParserTest.java b/src/test/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderConfigurationParserTest.java index 5315fb7bad6..125a65551bc 100644 --- a/src/test/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderConfigurationParserTest.java +++ b/src/test/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderConfigurationParserTest.java @@ -1,33 +1,27 @@ package edu.harvard.iq.dataverse.authorization.providers; +import com.fasterxml.jackson.databind.ObjectMapper; import edu.harvard.iq.dataverse.authorization.exceptions.AuthenticationProviderConfigurationException; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import java.io.IOException; -import java.net.URISyntaxException; -import java.net.URL; +import javax.json.JsonObject; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; import java.util.List; -import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; class AuthenticationProviderConfigurationParserTest { - @BeforeAll - static void loadAuthSchema() throws URISyntaxException, IOException { - /* Non working code. NPE. Can't we access resources from main in a test? - Path file = Paths.get(ClassLoader.getSystemResource("schemas/auth-provider-schema.json").toURI()); - String content = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); - System.out.println(content); - */ - } + ObjectMapper mapper = new ObjectMapper(); @ParameterizedTest @ValueSource(strings = {"minimal-valid.json", "title-i18n-valid.json"}) @@ -40,6 +34,32 @@ void parseValid(String filename) throws Exception { assertNotNull(config); } + @Test + void testUnmarshallingDeepForOptions() throws Exception { + Path file = Paths.get(ClassLoader.getSystemResource("auth-providers/options-unmarshal-test.json").toURI()); + String content = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); + + AuthenticationProviderConfiguration config = AuthenticationProviderConfigurationParser.parse(content); + + List expect = Arrays.asList("client_id","test","client_secret","test"); + assertThat(config.options().toString(), stringContainsInOrder(expect)); + } + + @Test + void testSerialization() throws Exception { + // given + Path file = Paths.get(ClassLoader.getSystemResource("auth-providers/options-unmarshal-test.json").toURI()); + String content = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); + AuthenticationProviderConfiguration config = AuthenticationProviderConfigurationParser.parse(content); + + // when + JsonObject test = AuthenticationProviderConfigurationParser.serialize(config); + + //then + assertNotNull(test); + assertEquals(mapper.writeValueAsString(config), test.toString()); + } + @Test void testValidatorLoading() throws Exception { assertDoesNotThrow(() -> AuthenticationProviderConfigurationParser.getValidator()); diff --git a/src/test/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderOptionsTest.java b/src/test/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderOptionsTest.java index a19f6fb2f9b..be70a2cb460 100644 --- a/src/test/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderOptionsTest.java +++ b/src/test/java/edu/harvard/iq/dataverse/authorization/providers/AuthenticationProviderOptionsTest.java @@ -26,6 +26,20 @@ void testUnmarshaling() throws Exception { assertEquals(expect, opts); } + @Test + void testUnmarshalingPartial() throws Exception { + // given + String json = "{\"client_id\": \"test\", \"client_secret\": \"test\" }"; + AuthenticationProviderOptions expect = new AuthenticationProviderOptions(); + expect.oauthClientId = "test"; + expect.oauthClientSecret = "test"; + + // when + AuthenticationProviderOptions opts = mapper.readValue(json, AuthenticationProviderOptions.class); + // then + assertEquals(expect, opts); + } + @Test void testMarshalingComplete() throws Exception { // given diff --git a/src/test/resources/auth-providers/options-unmarshal-test.json b/src/test/resources/auth-providers/options-unmarshal-test.json new file mode 100644 index 00000000000..3190361a497 --- /dev/null +++ b/src/test/resources/auth-providers/options-unmarshal-test.json @@ -0,0 +1,12 @@ +{ + "id": "test", + "type": "test", + "enabled": true, + "title": { + "en": "test" + }, + "options": { + "client_id": "test", + "client_secret": "test" + } +} \ No newline at end of file