diff --git a/waltid-did/build.gradle.kts b/waltid-did/build.gradle.kts index 5ac17490c..d5ce3b5da 100644 --- a/waltid-did/build.gradle.kts +++ b/waltid-did/build.gradle.kts @@ -18,18 +18,10 @@ java { } -/*tasks.test { - useJUnitPlatform() -}*/ - kotlin { jvmToolchain(11) } -/*application { - mainClass.set("id.walt.did.DidMainKt") -}*/ - kotlin { jvm { compilations.all { diff --git a/waltid-did/src/commonMain/kotlin/id/walt/did/dids/resolver/LocalResolver.kt b/waltid-did/src/commonMain/kotlin/id/walt/did/dids/resolver/LocalResolver.kt index 4886e3165..ad2ad2e6a 100644 --- a/waltid-did/src/commonMain/kotlin/id/walt/did/dids/resolver/LocalResolver.kt +++ b/waltid-did/src/commonMain/kotlin/id/walt/did/dids/resolver/LocalResolver.kt @@ -6,14 +6,23 @@ import id.walt.did.dids.resolver.local.DidJwkResolver import id.walt.did.dids.resolver.local.DidKeyResolver import id.walt.did.dids.resolver.local.DidWebResolver import id.walt.did.dids.resolver.local.LocalResolverMethod +import io.ktor.client.* +import io.ktor.client.engine.cio.* +import io.ktor.client.plugins.contentnegotiation.* +import io.ktor.serialization.kotlinx.json.* import kotlinx.serialization.json.JsonObject class LocalResolver : DidResolver { override val name = "core-crypto local resolver" + private val http = HttpClient(CIO) { + install(ContentNegotiation) { + json(DidWebResolver.json) + } + } private val resolvers = listOf( DidJwkResolver(), - DidWebResolver(), + DidWebResolver(http), DidKeyResolver() ).associateBy { it.method }.toMutableMap() diff --git a/waltid-did/src/commonMain/kotlin/id/walt/did/dids/resolver/local/DidWebResolver.kt b/waltid-did/src/commonMain/kotlin/id/walt/did/dids/resolver/local/DidWebResolver.kt index 79284cd10..b6ef05de7 100644 --- a/waltid-did/src/commonMain/kotlin/id/walt/did/dids/resolver/local/DidWebResolver.kt +++ b/waltid-did/src/commonMain/kotlin/id/walt/did/dids/resolver/local/DidWebResolver.kt @@ -2,33 +2,26 @@ package id.walt.did.dids.resolver.local import id.walt.core.crypto.keys.Key import id.walt.core.crypto.keys.LocalKey +import id.walt.did.dids.DidUtils import id.walt.did.dids.document.DidDocument import io.ktor.client.* import io.ktor.client.call.* -import io.ktor.client.plugins.contentnegotiation.* import io.ktor.client.request.* -import io.ktor.serialization.kotlinx.json.* import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonObject -class DidWebResolver : LocalResolverMethod("web") { +class DidWebResolver(private val client: HttpClient) : LocalResolverMethod("web") { companion object { - var URL_PROTOCOL = "https"//TODO: fix (exposed for test purpose) + const val URL_PROTOCOL = "https" val json = Json { ignoreUnknownKeys = true } } - private val http = HttpClient { - install(ContentNegotiation) { - json(json) - } - } - - fun resolveDidToUrl(did: String): String { - val didParts = did.removePrefix("did:web:").split(":") + private fun resolveDidToUrl(did: String): String = DidUtils.identifierFromDid(did)?.let { + val didParts = it.split(":") val domain = didParts[0].replace("%3A", ":") val selectedPath = didParts.drop(1) @@ -38,15 +31,15 @@ class DidWebResolver : LocalResolverMethod("web") { else -> "/${selectedPath.joinToString("/")}/did.json" } - return "$URL_PROTOCOL://$domain$path" - } + "$URL_PROTOCOL://$domain$path" + } ?: throw IllegalArgumentException("Unexpected did format (missing identifier): $did") override suspend fun resolve(did: String): Result { val url = resolveDidToUrl(did) val response = runCatching { DidDocument( - jsonObject = http.get(url).body() + jsonObject = client.get(url).body() ) } diff --git a/waltid-did/src/jvmTest/kotlin/TestClient.kt b/waltid-did/src/jvmTest/kotlin/TestClient.kt new file mode 100644 index 000000000..58a7d6ee2 --- /dev/null +++ b/waltid-did/src/jvmTest/kotlin/TestClient.kt @@ -0,0 +1,29 @@ +import id.walt.did.dids.resolver.local.DidWebResolver +import io.ktor.client.* +import io.ktor.client.engine.cio.* +import io.ktor.client.plugins.contentnegotiation.* +import io.ktor.network.tls.* +import io.ktor.serialization.kotlinx.json.* +import java.security.cert.X509Certificate +import javax.net.ssl.X509TrustManager + +object TestClient { + + val http = HttpClient(CIO) { + install(ContentNegotiation) { + json(DidWebResolver.json) + } + engine { + https { + trustManager = TrustAllManager(this) + } + } + } + + private class TrustAllManager(config: TLSConfigBuilder) : X509TrustManager { + private val delegate = config.build().trustManager + override fun checkClientTrusted(certificates: Array?, authType: String?) {} + override fun checkServerTrusted(certificates: Array?, authType: String?) {} + override fun getAcceptedIssuers(): Array = delegate.acceptedIssuers + } +} \ No newline at end of file diff --git a/waltid-did/src/jvmTest/kotlin/TestServer.kt b/waltid-did/src/jvmTest/kotlin/TestServer.kt index f562b48b1..f89c2bc73 100644 --- a/waltid-did/src/jvmTest/kotlin/TestServer.kt +++ b/waltid-did/src/jvmTest/kotlin/TestServer.kt @@ -9,29 +9,29 @@ import io.ktor.server.routing.* import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import java.io.File +import javax.security.auth.x500.X500Principal object TestServer { - private val keyStoreFile = File("src/jvmTest/resources/keystore.jks") + private val keyStoreFile = File(this.javaClass.classLoader.getResource("")!!.path.plus("keystore.jks")) + private val ed25519DocumentResponse = + this.javaClass.classLoader.getResource("did-doc/ed25519.json")!!.path.let { File(it).readText() } + private val secp256k1DocumentResponse = + this.javaClass.classLoader.getResource("did-doc/secp256k1.json")!!.path.let { File(it).readText() } + private val secp256r1DocumentResponse = + this.javaClass.classLoader.getResource("did-doc/secp256r1.json")!!.path.let { File(it).readText() } + private val rsaDocumentResponse = + this.javaClass.classLoader.getResource("did-doc/rsa.json")!!.path.let { File(it).readText() } private val keyStore = buildKeyStore { certificate("test") { password = "test123" domains = listOf("localhost", "127.0.0.1", "0.0.0.0") + subject = X500Principal("CN=localhost, OU=walt.id, O=walt.id, C=AT") } - } - + }.also { it.saveToFile(keyStoreFile, "test123") } private val environment = applicationEngineEnvironment { envConfig() } - private val ed25519DocumentResponse = File("src/jvmTest/resources/did-doc/ed25519.json").readText() - private val secp256k1DocumentResponse = File("src/jvmTest/resources/did-doc/secp256k1.json").readText() - private val secp256r1DocumentResponse = File("src/jvmTest/resources/did-doc/secp256r1.json").readText() - private val rsaDocumentResponse = File("src/jvmTest/resources/did-doc/rsa.json").readText() - - val server = embeddedServer(Netty, environment) {} - - init { - keyStore.saveToFile(keyStoreFile, "test123") - } + val server: ApplicationEngine by lazy { embeddedServer(Netty, environment) } private fun Application.module() { install(ContentNegotiation) { @@ -58,15 +58,16 @@ object TestServer { module() } connector { + port = 8000 + } + sslConnector( + keyStore = keyStore, + keyAlias = "test", + keyStorePassword = { "test123".toCharArray() }, + privateKeyPassword = { "test123".toCharArray() }) { port = 8080 + keyStorePath = keyStoreFile } -// sslConnector( -// keyStore = keyStore, -// keyAlias = "test", -// keyStorePassword = { "test123".toCharArray() }, -// privateKeyPassword = { "test123".toCharArray() }) { -// port = 8443 -// keyStorePath = keyStoreFile -// } } + } \ No newline at end of file diff --git a/waltid-did/src/jvmTest/kotlin/resolvers/DidWebResolverTest.kt b/waltid-did/src/jvmTest/kotlin/resolvers/DidWebResolverTest.kt index c56e7bc33..aa763276a 100644 --- a/waltid-did/src/jvmTest/kotlin/resolvers/DidWebResolverTest.kt +++ b/waltid-did/src/jvmTest/kotlin/resolvers/DidWebResolverTest.kt @@ -1,5 +1,6 @@ package resolvers +import TestClient import TestServer import id.walt.core.crypto.keys.Key import id.walt.did.dids.document.DidDocument @@ -16,7 +17,7 @@ import org.junit.jupiter.params.provider.MethodSource import java.util.stream.Stream class DidWebResolverTest : DidResolverTestBase() { - override val sut: LocalResolverMethod = DidWebResolver() + override val sut: LocalResolverMethod = DidWebResolver(TestClient.http) @ParameterizedTest @MethodSource @@ -42,7 +43,6 @@ class DidWebResolverTest : DidResolverTestBase() { @JvmStatic @BeforeAll fun startServer() { - DidWebResolver.URL_PROTOCOL = "http" TestServer.server.start() } @@ -50,7 +50,6 @@ class DidWebResolverTest : DidResolverTestBase() { @AfterAll fun stopServer() { TestServer.server.stop() - DidWebResolver.URL_PROTOCOL = "https" } @JvmStatic diff --git a/waltid-did/src/jvmTest/resources/keystore.jks b/waltid-did/src/jvmTest/resources/keystore.jks deleted file mode 100644 index b7faf3628..000000000 Binary files a/waltid-did/src/jvmTest/resources/keystore.jks and /dev/null differ