Skip to content

Commit bff6dbe

Browse files
committed
Misc stargate updates
1 parent 167cf0c commit bff6dbe

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

component-stargate/src/commonMain/kotlin/com/mooncloak/vpn/component/stargate/entanglement/DIDResolver.kt renamed to component-stargate/src/commonMain/kotlin/com/mooncloak/vpn/component/stargate/entanglement/ATProtocolDIDResolver.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ import io.ktor.client.request.get
66
import io.ktor.client.statement.bodyAsText
77
import io.ktor.http.isSuccess
88

9-
public class DIDResolver public constructor(
9+
public interface DIDResolver {
10+
11+
public suspend fun resolve(handle: IdentityHandle): DID
12+
13+
public companion object
14+
}
15+
16+
public class ATProtocolDIDResolver public constructor(
1017
private val defaultDomainProvider: DomainProvider,
1118
private val dnsTxtRecordResolver: DnsTxtRecordResolver,
1219
private val httpClient: HttpClient
13-
) {
20+
) : DIDResolver {
1421

15-
public suspend fun resolve(handle: IdentityHandle): DID =
22+
override suspend fun resolve(handle: IdentityHandle): DID =
1623
when (handle.format) {
1724
IdentityHandleFormat.DID -> DID.from(value = handle.value)
1825

component-stargate/src/commonMain/kotlin/com/mooncloak/vpn/component/stargate/entanglement/DIDDocumentResolver.kt

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ import io.ktor.client.HttpClient
44
import io.ktor.client.call.body
55
import io.ktor.client.request.get
66

7-
public class DIDDocumentResolver public constructor(
8-
private val httpClient: HttpClient
9-
) {
7+
public interface DIDDocumentResolver {
108

11-
public suspend fun resolve(did: DID): DIDDocument =
12-
when (val method = did.method) {
13-
"web" -> resolveDidWeb(did)
14-
"plc" -> resolveDidPlc(did)
15-
// TODO: Possibly support other well known DID methods.
16-
else -> error("Unsupported DID method '$method'")
17-
}
9+
public suspend fun resolve(did: DID): DIDDocument
10+
11+
public companion object
12+
}
1813

19-
private suspend fun resolveDidWeb(did: DID): DIDDocument {
14+
public class DIDWebDocumentResolver public constructor(
15+
private val httpClient: HttpClient
16+
) : DIDDocumentResolver {
17+
18+
override suspend fun resolve(did: DID): DIDDocument {
2019
val identifier = did.id ?: error("DID id part was required but was missing.")
2120

2221
// Convert identifier to URL (e.g., chris.keenan -> https://chris.keenan/.well-known/did.json)
@@ -40,7 +39,18 @@ public class DIDDocumentResolver public constructor(
4039
}
4140
}
4241

43-
private suspend fun resolveDidPlc(did: DID): DIDDocument {
42+
private fun DIDDocument.validateDidDocument(did: DID) {
43+
if (this.id != did.value) {
44+
error("DID document id '${this.id}' does not match expected DID '${did.value}'.")
45+
}
46+
}
47+
}
48+
49+
public class DIDPlcDocumentResolver public constructor(
50+
private val httpClient: HttpClient
51+
) : DIDDocumentResolver {
52+
53+
override suspend fun resolve(did: DID): DIDDocument {
4454
// Query PLC directory (e.g., https://plc.directory/did:plc:abc123)
4555
val url = "https://plc.directory/${did.value}"
4656

@@ -57,3 +67,19 @@ public class DIDDocumentResolver public constructor(
5767
}
5868
}
5969
}
70+
71+
public class DefaultDIDDocumentResolver public constructor(
72+
httpClient: HttpClient
73+
) : DIDDocumentResolver {
74+
75+
private val didWebDocumentResolver = DIDWebDocumentResolver(httpClient = httpClient)
76+
private val didPlcDocumentResolver = DIDPlcDocumentResolver(httpClient = httpClient)
77+
78+
override suspend fun resolve(did: DID): DIDDocument =
79+
when (val method = did.method) {
80+
"web" -> didWebDocumentResolver.resolve(did)
81+
"plc" -> didPlcDocumentResolver.resolve(did)
82+
// TODO: Possibly support other well known DID methods.
83+
else -> error("Unsupported DID method '$method'")
84+
}
85+
}

0 commit comments

Comments
 (0)