Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Use backend redirect in frontend, added portal URL to backend call
Browse files Browse the repository at this point in the history
  • Loading branch information
waltkb committed Oct 27, 2023
1 parent 9e56284 commit e8de105
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
21 changes: 15 additions & 6 deletions src/main/kotlin/id/walt/service/SSIKit2WalletService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class SSIKit2WalletService(accountId: UUID) : WalletService(accountId) {
/**
* @return redirect uri
*/
override suspend fun usePresentationRequest(request: String, did: String): String {
override suspend fun usePresentationRequest(request: String, did: String): Result<String?> {
val credentialWallet = getCredentialWallet(did)

val authReq = AuthorizationRequest.fromHttpQueryString(Url(request).encodedQuery)
Expand All @@ -178,12 +178,15 @@ class SSIKit2WalletService(accountId: UUID) : WalletService(accountId) {
entry.value.forEach { append(entry.key, it) }
}
})
println(resp)
println("HTTP Response: $resp, body: ${resp.bodyAsText()}")

if (!resp.status.isSuccess()) {
return "https://error.org/"
return if (resp.status.isSuccess()) {
val redirectUri = resp.bodyAsText()
if (redirectUri.startsWith("http"))
Result.success(redirectUri)
else Result.success(null)
} else {
return ""
Result.failure(IllegalStateException("https://error.org/"))
}
}

Expand All @@ -203,6 +206,7 @@ class SSIKit2WalletService(accountId: UUID) : WalletService(accountId) {
did
)
}

fun getAnyCredentialWallet() = credentialWallets.values.firstOrNull() ?: getCredentialWallet("did:test:test")

private val testCIClientConfig = OpenIDClientConfig("test-client", null, redirectUri = "http://blank")
Expand Down Expand Up @@ -288,6 +292,7 @@ class SSIKit2WalletService(accountId: UUID) : WalletService(accountId) {

credentialResponses.credentialResponses ?: throw IllegalArgumentException("No credential responses returned")
}

credReqs.size == 1 -> {
val credReq = credReqs.first()

Expand All @@ -300,6 +305,7 @@ class SSIKit2WalletService(accountId: UUID) : WalletService(accountId) {

listOf(credentialResponse)
}

else -> throw IllegalStateException("No credentials offered")
}

Expand Down Expand Up @@ -327,7 +333,8 @@ class SSIKit2WalletService(accountId: UUID) : WalletService(accountId) {
val key = args["keyId"]?.content?.takeIf { it.isNotEmpty() }?.let { getKey(it) } ?: LocalKey.generate(KeyType.Ed25519)
val options = getDidOptions(method, args)
val result = DidService.registerByKey(method, key, options)
val keyRef = KeysService.add(accountId,
val keyRef = KeysService.add(
accountId,
DbKey(
keyId = key.getKeyId(),
document = KeySerialization.serializeKey(key)
Expand Down Expand Up @@ -484,11 +491,13 @@ class SSIKit2WalletService(accountId: UUID) : WalletService(accountId) {
args["key"]?.let { enumValueIgnoreCase<KeyType>(it.content) } ?: KeyType.Ed25519,
args["useJwkJcsPub"]?.let { it.content.toBoolean() } ?: false
)

"jwk" -> DidJwkCreateOptions()
"web" -> DidWebCreateOptions(domain = args["domain"]?.content ?: "", path = args["path"]?.content ?: "")
"cheqd" -> DidCheqdCreateOptions(
network = args["network"]?.content ?: "testnet",
)

else -> throw IllegalArgumentException("Did method not supported: $method")
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/id/walt/service/WalletKitWalletService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class WalletKitWalletService(accountId: UUID) : WalletService(accountId) {
val state: String?
)

override suspend fun usePresentationRequest(request: String, did: String): String? {
override suspend fun usePresentationRequest(request: String, did: String): Result<String?> {
val decoded = URLDecoder.decode(request, Charset.defaultCharset())
val queryParams = getQueryParams(decoded)
val redirectUri = queryParams["redirect_uri"]?.first()
Expand Down Expand Up @@ -235,7 +235,7 @@ class WalletKitWalletService(accountId: UUID) : WalletService(accountId) {

val redirect = redirectUriResp.headers[HttpHeaders.Location]

return redirect
return Result.success(redirect)
}

override suspend fun resolvePresentationRequest(request: String): String {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/id/walt/service/WalletService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class WalletService(val accountId: UUID) {
abstract suspend fun getCredential(credentialId: String): String

// SIOP
abstract suspend fun usePresentationRequest(request: String, did: String): String?
abstract suspend fun usePresentationRequest(request: String, did: String): Result<String?>
abstract suspend fun resolvePresentationRequest(request: String): String
abstract suspend fun useOfferRequest(offer: String, did: String)

Expand Down
27 changes: 20 additions & 7 deletions src/main/kotlin/id/walt/web/controllers/ExchangeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,28 @@ fun Application.exchange() = walletRoute {

val request = call.receiveText()

val redirect = wallet.usePresentationRequest(request, did)
wallet.addOperationHistory(
WalletOperationHistory.new(
wallet, "usePresentationRequest",
mapOf("did" to did, "request" to request, "redirect" to redirect)
val result = wallet.usePresentationRequest(request, did)


if (result.isSuccess) {
wallet.addOperationHistory(
WalletOperationHistory.new(
wallet, "usePresentationRequest",
mapOf("did" to did, "request" to request, "success" to "true", "redirect" to result.getOrThrow()) // change string true to bool
)
)

context.respond(HttpStatusCode.OK, mapOf("redirectUri" to result.getOrThrow()))
} else {
wallet.addOperationHistory(
WalletOperationHistory.new(
wallet, "usePresentationRequest",
mapOf("did" to did, "request" to request, "success" to "false", "redirect" to result.getOrThrow()) // change string true to bool
)
)
)

context.respond(HttpStatusCode.OK, mapOf("redirectUri" to redirect))
context.respond(HttpStatusCode.BadRequest, mapOf("redirectUri" to result.exceptionOrNull()!!.message))
}
}
post("resolvePresentationRequest", {
summary = "Return resolved / parsed presentation request"
Expand Down
12 changes: 10 additions & 2 deletions web/src/pages/exchange/presentation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,24 @@ async function acceptPresentation() {
method: 'POST',
body: request
})
setInterval(async () => {
console.log(response)
if (response.redirectUri) {
window.location.href = response.redirectUri
}
/*setInterval(async () => {
let sessionId = presentationUrl.searchParams.get('state');
let response = await fetch(`https://verifier.portal.walt.id/vp/session/${sessionId}`);
response = await response.json();
if (response.verificationResult) {
window.location.href = `https://portal.walt.id/success/${sessionId}`;
}
}, 1000);
}, 1000);*/
} catch (e) {
failed.value = true
window.alert(e)
throw e
}
}
Expand Down

0 comments on commit e8de105

Please sign in to comment.