Skip to content

Commit

Permalink
fix: did-web ensure path speratator prefix (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeplotean authored Nov 23, 2023
1 parent f2a0aec commit e584dc3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import id.walt.did.dids.registrar.DidResult
import id.walt.did.dids.registrar.dids.DidCreateOptions
import id.walt.did.dids.registrar.local.LocalRegistrarMethod
import id.walt.did.utils.EncodingUtils.urlEncode
import id.walt.did.utils.ExtensionMethods.ensurePrefix

class DidWebRegistrar : LocalRegistrarMethod("web") {
override suspend fun register(options: DidCreateOptions): DidResult = options.get<KeyType>("keyType")?.let {
Expand All @@ -21,7 +22,7 @@ class DidWebRegistrar : LocalRegistrarMethod("web") {
}?.let {
val domain = urlEncode(it)
val path = options.get<String>("path")?.takeIf { it.isNotEmpty() }?.let {
it.split("/").joinToString(":") { part -> urlEncode(part) }
it.ensurePrefix("/").split("/").joinToString(":") { part -> urlEncode(part) }
} ?: ""
DidResult(
"did:web:$domain$path", DidDocument(
Expand Down
17 changes: 10 additions & 7 deletions waltid-did/src/jvmTest/kotlin/registrars/DidWebRegistrarTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import id.walt.did.dids.registrar.dids.DidCreateOptions
import id.walt.did.dids.registrar.dids.DidWebCreateOptions
import id.walt.did.dids.registrar.local.web.DidWebRegistrar
import id.walt.did.utils.EncodingUtils
import id.walt.did.utils.ExtensionMethods.ensurePrefix
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
Expand Down Expand Up @@ -75,22 +76,24 @@ class DidWebRegistrarTest : DidRegistrarTestBase(DidWebRegistrar()) {
val domain = options.get<String>("domain")!!
val path = options.get<String>("path")
defaultDidAssertions(result, options)
// assert [did identifier] and [domain + path] are identical
assert(
EncodingUtils.urlDecode(DidUtils.identifierFromDid(did)!!) ==
domain.plus(path?.replace("/", ":"))
)
identifierAssertions(did, domain, path)
}

private val webKeyAssertions: registrarKeyAssertion = { result, options, key ->
val did = result.did
val domain = options.get<String>("domain")!!
val path = options.get<String>("path")
defaultKeyAssertions(result, options, key)
identifierAssertions(did, domain, path)
}

private fun identifierAssertions(did: String, domain: String, path: String?) {
// assert [did identifier] and [domain + path] are identical
assert(
EncodingUtils.urlDecode(DidUtils.identifierFromDid(did)!!) ==
domain.plus(path?.replace("/", ":"))
//TODO: avoid computations in result comparison
EncodingUtils.urlDecode(DidUtils.identifierFromDid(did)!!) == domain.plus(
path.takeIf { !it.isNullOrEmpty() }?.ensurePrefix("/")?.replace("/", ":") ?: ""
)
)
}
}
Expand Down

0 comments on commit e584dc3

Please sign in to comment.