-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorporate did_web test vectors #172
Changes from all commits
dfc5507
744dad6
5e1c646
7067c25
ee106e0
2d6eedc
74cd5e1
66e9e6f
6734551
c8380f7
61279a2
e095bc9
c573e36
d8ecb4a
5bc317a
ff56c35
3b74af4
40b69f9
f3f0ac5
c8a30c0
77d0118
ee432c4
7c5ee35
c9f39d0
29b1226
7abe689
c4fe9d7
c18d108
399ef24
2783f68
32ba68a
7e699a3
1b510a7
cb911e9
e2adca5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ dependencies { | |
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jackson_version") | ||
implementation("com.nimbusds:nimbus-jose-jwt:9.34") | ||
implementation("com.github.multiformats:java-multibase:1.1.0") | ||
implementation("io.github.oshai:kotlin-logging-jvm:6.0.2") | ||
|
||
implementation("io.ktor:ktor-client-core:$ktor_version") | ||
implementation("io.ktor:ktor-client-okhttp:$ktor_version") | ||
|
@@ -38,4 +39,5 @@ dependencies { | |
testImplementation("commons-codec:commons-codec:1.16.0") | ||
|
||
implementation("dnsjava:dnsjava:3.5.2") | ||
testImplementation(project(mapOf("path" to ":testing"))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why was this needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package web5.sdk.dids | ||
|
||
/** | ||
* Represents a DID resolution error as described in https://w3c-ccg.github.io/did-resolution/#errors. | ||
*/ | ||
public enum class ResolutionError(public val value: String) { | ||
METHOD_NOT_SUPPORTED("methodNotSupported"), | ||
NOT_FOUND("notFound"), | ||
INVALID_DID("invalidDid"), | ||
INTERNAL_ERROR("internalError"), | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,19 @@ package web5.sdk.dids.methods.web | |
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import foundation.identity.did.DID | ||
import foundation.identity.did.DIDDocument | ||
import foundation.identity.did.parser.ParserException | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.engine.HttpClientEngine | ||
import io.ktor.client.engine.okhttp.OkHttp | ||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation | ||
import io.ktor.client.request.get | ||
import io.ktor.client.statement.HttpResponse | ||
import io.ktor.client.statement.bodyAsText | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.contentType | ||
import io.ktor.http.isSuccess | ||
import io.ktor.serialization.jackson.jackson | ||
import jdk.jshell.spi.ExecutionControl.NotImplementedException | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.Cache | ||
import okhttp3.HttpUrl.Companion.toHttpUrl | ||
|
@@ -24,13 +26,15 @@ import web5.sdk.dids.CreateDidOptions | |
import web5.sdk.dids.Did | ||
import web5.sdk.dids.DidMethod | ||
import web5.sdk.dids.DidResolutionResult | ||
import web5.sdk.dids.ResolutionError | ||
import web5.sdk.dids.ResolveDidOptions | ||
import web5.sdk.dids.methods.ion.InvalidStatusException | ||
import web5.sdk.dids.validateKeyMaterialInsideKeyManager | ||
import java.io.File | ||
import java.net.InetAddress | ||
import java.net.URL | ||
import java.net.URLDecoder | ||
import java.net.UnknownHostException | ||
import kotlin.text.Charsets.UTF_8 | ||
|
||
/** | ||
|
@@ -93,6 +97,8 @@ public sealed class DidWebApi( | |
configuration: DidWebApiConfiguration | ||
) : DidMethod<DidWeb, CreateDidOptions> { | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
private val mapper = jacksonObjectMapper() | ||
|
||
private val engine: HttpClientEngine = configuration.engine ?: OkHttp.create { | ||
|
@@ -117,12 +123,34 @@ public sealed class DidWebApi( | |
override val methodName: String = "web" | ||
|
||
override fun resolve(did: String, options: ResolveDidOptions?): DidResolutionResult { | ||
val docURL = getDocURL(did) | ||
return try { | ||
resolveInternal(did, options) | ||
} catch (e: Exception) { | ||
logger.warn(e) { "resolving DID $did failed" } | ||
DidResolutionResult.fromResolutionError(ResolutionError.INTERNAL_ERROR) | ||
} | ||
} | ||
|
||
private fun resolveInternal(did: String, options: ResolveDidOptions?): DidResolutionResult { | ||
val parsedDid = try { | ||
DID.fromString(did) | ||
} catch (_: ParserException) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we have another line to catch all other exceptions?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done at the public function level. |
||
return DidResolutionResult.fromResolutionError(ResolutionError.INVALID_DID) | ||
} | ||
|
||
val resp = runBlocking { | ||
client.get(docURL) { | ||
contentType(ContentType.Application.Json) | ||
if (parsedDid.methodName != methodName) { | ||
return DidResolutionResult.fromResolutionError(ResolutionError.METHOD_NOT_SUPPORTED) | ||
} | ||
val docURL = getDocURL(parsedDid) | ||
|
||
val resp: HttpResponse = try { | ||
runBlocking { | ||
client.get(docURL) { | ||
contentType(ContentType.Application.Json) | ||
} | ||
} | ||
} catch (_: UnknownHostException) { | ||
return DidResolutionResult.fromResolutionError(ResolutionError.NOT_FOUND) | ||
} | ||
|
||
val body = runBlocking { resp.bodyAsText() } | ||
|
@@ -140,12 +168,7 @@ public sealed class DidWebApi( | |
return DidWeb(uri, keyManager, this) | ||
} | ||
|
||
private fun getDocURL(didWebStr: String): String { | ||
val parsedDid = DID.fromString(didWebStr) | ||
require(parsedDid.methodName == methodName) { | ||
"$didWebStr is missing prefix \"did:$methodName\"" | ||
} | ||
|
||
private fun getDocURL(parsedDid: DID): String { | ||
val domainNameWithPath = parsedDid.methodSpecificId.replace(":", "/") | ||
val decodedDomain = URLDecoder.decode(domainNameWithPath, UTF_8) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err I wonder why my formatter is doing this.
for web5-js we have a prettier task - TBD54566975/web5-js#266
should do something similar across all our repos so formatting stays in sync
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #178
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weird, thought this would have been taken care of by ktlint/detekt