Skip to content

Commit

Permalink
support web did resolution over http for local host (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
aparkersquare authored Jun 28, 2024
1 parent b14e1bc commit e287b8a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
14 changes: 6 additions & 8 deletions dids/src/main/kotlin/web5/sdk/dids/methods/web/DidWeb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,13 @@ public sealed class DidWebApi(
private fun decodeId(parsedDid: Did): String {
val domainNameWithPath = parsedDid.id.replace(":", "/")
val decodedDomain = URLDecoder.decode(domainNameWithPath, UTF_8)
val url = URL("https://$decodedDomain")

val targetUrl = StringBuilder("https://$decodedDomain")

val url = URL(targetUrl.toString())
if (url.path.isEmpty()) {
targetUrl.append(WELL_KNOWN_URL_PATH)
return buildString {
append(if (url.host == "localhost" || url.host == "127.0.0.1") "http://" else "https://")
append(decodedDomain)
if (url.path.isEmpty()) append(WELL_KNOWN_URL_PATH)
append(DID_DOC_FILE_NAME)
}
targetUrl.append(DID_DOC_FILE_NAME)
return targetUrl.toString()
}

}
45 changes: 41 additions & 4 deletions dids/src/test/kotlin/web5/sdk/dids/methods/web/DidWebTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import io.ktor.http.HttpStatusCode
import io.ktor.http.headersOf
import io.ktor.utils.io.ByteReadChannel
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import web5.sdk.crypto.InMemoryKeyManager
import web5.sdk.dids.DidResolutionResult
import web5.sdk.dids.methods.util.readKey
import web5.sdk.testing.TestVectors
import java.io.File
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class DidWebTest {

Expand Down Expand Up @@ -48,15 +46,20 @@ class DidWebTest {
fun resolve() {
val didsToTest = listOf(
"did:web:example.com",
"did:web:example.com%3A8080",
"did:web:w3c-ccg.github.io:user:alice",
"did:web:example.com%3A3000:user:alice",
"did:web:localhost",
"did:web:localhost%3A8080",
"did:web:127.0.0.1",
)
val api = DidWebApi {
engine = mockEngine()
}
for (did in didsToTest) {
val result = api.resolve(did)
assertEquals(did, result.didDocument!!.id)
val didDocument = assertNotNull(result.didDocument, "document should not be null for $did")
assertEquals(did, didDocument.id)
}
}

Expand All @@ -81,6 +84,14 @@ class DidWebTest {
)
}

"https://example.com:8080/.well-known/did.json" -> {
respond(
content = ByteReadChannel("""{"id": "did:web:example.com%3A8080"}"""),
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, "application/json")
)
}

"https://w3c-ccg.github.io/user/alice/did.json" -> {
respond(
content = ByteReadChannel("""{"id": "did:web:w3c-ccg.github.io:user:alice"}"""),
Expand All @@ -107,6 +118,32 @@ class DidWebTest {
)
}

// localhost (& loopback IP 127.0.0.1) use http

"http://localhost/.well-known/did.json" -> {
respond(
content = ByteReadChannel("""{"id": "did:web:localhost"}"""),
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, "application/json")
)
}

"http://localhost:8080/.well-known/did.json" -> {
respond(
content = ByteReadChannel("""{"id": "did:web:localhost%3A8080"}"""),
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, "application/json")
)
}

"http://127.0.0.1/.well-known/did.json" -> {
respond(
content = ByteReadChannel("""{"id": "did:web:127.0.0.1"}"""),
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, "application/json")
)
}

else -> throw Exception("")
}
}
Expand Down

0 comments on commit e287b8a

Please sign in to comment.