-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creation of VerifiablePresentations via Custodian REST API, close #62
- Loading branch information
Showing
5 changed files
with
153 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/id/walt/rest/custodian/PresentCredentialsRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package id.walt.rest.custodian | ||
|
||
import com.beust.klaxon.Json | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class PresentCredentialsRequest( | ||
val vcs: List<String>, | ||
val holderDid: String, | ||
@Json(serializeNull = false) val verifierDid: String? = null, | ||
@Json(serializeNull = false) val domain: String? = null, | ||
@Json(serializeNull = false) val challenge: String? = null | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,111 @@ | ||
package id.walt.rest | ||
|
||
import id.walt.auditor.Auditor | ||
import id.walt.auditor.SignaturePolicy | ||
import id.walt.model.DidMethod | ||
import id.walt.rest.custodian.CustodianAPI | ||
import id.walt.rest.custodian.PresentCredentialsRequest | ||
import id.walt.servicematrix.ServiceMatrix | ||
import id.walt.services.did.DidService | ||
import id.walt.signatory.ProofConfig | ||
import id.walt.signatory.ProofType | ||
import id.walt.signatory.Signatory | ||
import id.walt.vclib.Helpers.toCredential | ||
import id.walt.vclib.credentials.VerifiablePresentation | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.ktor.client.* | ||
import io.ktor.client.engine.cio.* | ||
import io.ktor.client.features.json.* | ||
import io.ktor.client.features.json.serializer.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import kotlinx.coroutines.runBlocking | ||
|
||
class CustodianApiTest : StringSpec({ | ||
|
||
val CUSTODIAN_API_URL = "http://localhost:7013" | ||
ServiceMatrix("service-matrix.properties") | ||
|
||
val client = HttpClient(CIO) { | ||
install(JsonFeature) { | ||
serializer = KotlinxSerializer() | ||
serializer = KotlinxSerializer(kotlinx.serialization.json.Json { encodeDefaults = false }) | ||
} | ||
expectSuccess = false | ||
} | ||
|
||
println("${CustodianAPI.DEFAULT_BIND_ADDRESS}/${CustodianAPI.DEFAULT_Custodian_API_PORT}") | ||
fun get(path: String): HttpResponse = runBlocking { | ||
val response: HttpResponse = | ||
client.get("http://${CustodianAPI.DEFAULT_BIND_ADDRESS}:${CustodianAPI.DEFAULT_Custodian_API_PORT}$path") { | ||
headers { | ||
append(HttpHeaders.Accept, "text/html") | ||
append(HttpHeaders.Authorization, "token") | ||
} | ||
} | ||
response.status.value shouldBe 200 | ||
return@runBlocking response | ||
} | ||
|
||
"Starting Custodian API" { | ||
CustodianAPI.start() | ||
} | ||
|
||
|
||
"Check Custodian Presentation generation LD_PROOF" { | ||
val did = DidService.create(DidMethod.key) | ||
|
||
// Issuance is Signatory stuff, we're just testing the Custodian here | ||
val vcJwt = Signatory.getService().issue( | ||
"VerifiableDiploma", | ||
ProofConfig( | ||
issuerDid = did, | ||
subjectDid = did, | ||
issuerVerificationMethod = "Ed25519Signature2018", | ||
proofType = ProofType.LD_PROOF | ||
) | ||
) | ||
|
||
val response: String = | ||
client.post("http://${CustodianAPI.DEFAULT_BIND_ADDRESS}:${CustodianAPI.DEFAULT_Custodian_API_PORT}/credentials/present") { | ||
contentType(ContentType.Application.Json) | ||
body = PresentCredentialsRequest(listOf(vcJwt), did) | ||
} | ||
|
||
val vp = response.toCredential() as VerifiablePresentation | ||
|
||
vp.type shouldBe VerifiablePresentation.type | ||
|
||
println("VP Response: $response") | ||
|
||
Auditor.getService().verify(response, listOf(SignaturePolicy())).valid shouldBe true | ||
} | ||
|
||
"Check Custodian Presentation generation JWT" { | ||
val did = DidService.create(DidMethod.key) | ||
|
||
// Issuance is Signatory stuff, we're just testing the Custodian here | ||
val vcJwt = Signatory.getService().issue( | ||
"VerifiableDiploma", | ||
ProofConfig( | ||
issuerDid = did, | ||
subjectDid = did, | ||
issuerVerificationMethod = "Ed25519Signature2018", | ||
proofType = ProofType.JWT | ||
) | ||
) | ||
|
||
val response: String = | ||
client.post("http://${CustodianAPI.DEFAULT_BIND_ADDRESS}:${CustodianAPI.DEFAULT_Custodian_API_PORT}/credentials/present") { | ||
contentType(ContentType.Application.Json) | ||
body = PresentCredentialsRequest(listOf(vcJwt), did) | ||
} | ||
|
||
response.count { it == '.' } shouldBe 2 | ||
|
||
println("VP Response: $response") | ||
|
||
Auditor.getService().verify(response, listOf(SignaturePolicy())).valid shouldBe true | ||
} | ||
|
||
/*"Test documentation" { | ||
val response = get("/v1/api-documentation").readText() | ||
response shouldContain "\"operationId\":\"health\"" | ||
response shouldContain "Returns HTTP 200 in case all services are up and running" | ||
}*/ | ||
}) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|