Skip to content

Commit

Permalink
fix: test server tls, local-did-web-resolver inject http client (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeplotean authored Oct 10, 2023
1 parent 80a6feb commit 5c24861
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 48 deletions.
8 changes: 0 additions & 8 deletions waltid-did/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@ java {
}


/*tasks.test {
useJUnitPlatform()
}*/

kotlin {
jvmToolchain(11)
}

/*application {
mainClass.set("id.walt.did.DidMainKt")
}*/

kotlin {
jvm {
compilations.all {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<DidDocument> {
val url = resolveDidToUrl(did)

val response = runCatching {
DidDocument(
jsonObject = http.get(url).body<JsonObject>()
jsonObject = client.get(url).body<JsonObject>()
)
}

Expand Down
29 changes: 29 additions & 0 deletions waltid-did/src/jvmTest/kotlin/TestClient.kt
Original file line number Diff line number Diff line change
@@ -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<out X509Certificate>?, authType: String?) {}
override fun checkServerTrusted(certificates: Array<out X509Certificate>?, authType: String?) {}
override fun getAcceptedIssuers(): Array<X509Certificate> = delegate.acceptedIssuers
}
}
43 changes: 22 additions & 21 deletions waltid-did/src/jvmTest/kotlin/TestServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
// }
}

}
5 changes: 2 additions & 3 deletions waltid-did/src/jvmTest/kotlin/resolvers/DidWebResolverTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package resolvers

import TestClient
import TestServer
import id.walt.core.crypto.keys.Key
import id.walt.did.dids.document.DidDocument
Expand All @@ -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
Expand All @@ -42,15 +43,13 @@ class DidWebResolverTest : DidResolverTestBase() {
@JvmStatic
@BeforeAll
fun startServer() {
DidWebResolver.URL_PROTOCOL = "http"
TestServer.server.start()
}

@JvmStatic
@AfterAll
fun stopServer() {
TestServer.server.stop()
DidWebResolver.URL_PROTOCOL = "https"
}

@JvmStatic
Expand Down
Binary file removed waltid-did/src/jvmTest/resources/keystore.jks
Binary file not shown.

0 comments on commit 5c24861

Please sign in to comment.