Skip to content
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

style: apply linters automatic fixes #1500

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,38 @@ class IssueControllerImpl(
)
} yield ()

private def getIssuingDIDFromRequestJwtProperties(
request: CreateIssueCredentialRecordRequest
): IO[ErrorResponse, PrismDID] =
ZIO
.fromOption(request.jwtVcPropertiesV1.map(_.issuingDID))
.orElse(ZIO.fromOption(request.issuingDID))
.orElseFail(ErrorResponse.badRequest(detail = Some("Missing request parameter: issuingDID")))
.flatMap(extractPrismDIDFromString)

private def getIssuingDIDFromRequestSDJWTProperties(
request: CreateIssueCredentialRecordRequest
): IO[ErrorResponse, PrismDID] =
ZIO
.fromOption(request.sdJwtVcPropertiesV1.map(_.issuingDID))
.orElse(ZIO.fromOption(request.issuingDID))
.orElseFail(ErrorResponse.badRequest(detail = Some("Missing request parameter: issuingDID")))
.flatMap(extractPrismDIDFromString)

private def getIssuingDIDFromAnonCredsProperties(
request: CreateIssueCredentialRecordRequest
): IO[ErrorResponse, PrismDID] =
ZIO
.fromOption(request.anoncredsVcPropertiesV1.map(_.issuingDID))
.orElse(ZIO.fromOption(request.issuingDID))
.orElseFail(ErrorResponse.badRequest(detail = Some("Missing request parameter: issuingDID")))
.flatMap(extractPrismDIDFromString)

private def createCredentialOfferRecord(
request: CreateIssueCredentialRecordRequest,
offerContext: OfferContext
): ZIO[WalletAccessContext, ErrorResponse, IssueCredentialRecord] = {

def getIssuingDidFromRequest(request: CreateIssueCredentialRecordRequest) = extractPrismDIDFromString(
request.issuingDID
)

for {
credentialFormat <- ZIO.succeed(
request.credentialFormat.map(CredentialFormat.valueOf).getOrElse(CredentialFormat.JWT)
Expand All @@ -81,20 +104,26 @@ class IssueControllerImpl(
credentialFormat match
case JWT =>
for {
issuingDID <- getIssuingDidFromRequest(request)
issuingDID <- getIssuingDIDFromRequestJwtProperties(request)
_ <- validatePrismDID(issuingDID, allowUnpublished = true, Role.Issuer)
credentialSchemaRef <- parseCredentialSchemaRef_VCDM1_1(
request.schemaId,
request.jwtVcPropertiesV1.map(_.credentialSchema)
)
claims <- ZIO
.fromOption(request.jwtVcPropertiesV1.map(_.claims).orElse(request.claims))
.orElseFail(ErrorResponse.badRequest(detail = Some("Missing request parameter: claims")))
kid = request.jwtVcPropertiesV1
.map(_.issuingKid)
.orElse(request.issuingKid) // TODO: should it be Option[KeyId]?
record <- credentialService
.createJWTIssueCredentialRecord(
pairwiseIssuerDID = offerContext.pairwiseIssuerDID,
pairwiseHolderDID = offerContext.pairwiseHolderDID,
kidIssuer = request.issuingKid,
thid = DidCommID(),
credentialSchemaRef = Some(credentialSchemaRef),
claims = request.claims,
claims = claims,
validityPeriod = request.validityPeriod,
automaticIssuance = request.automaticIssuance.orElse(Some(true)),
issuingDID = issuingDID.asCanonical,
Expand All @@ -107,20 +136,26 @@ class IssueControllerImpl(
} yield record
case SDJWT =>
for {
issuingDID <- getIssuingDidFromRequest(request)
issuingDID <- getIssuingDIDFromRequestSDJWTProperties(request)
_ <- validatePrismDID(issuingDID, allowUnpublished = true, Role.Issuer)
credentialSchemaRef <- parseCredentialSchemaRef_VCDM1_1(
request.schemaId,
request.sdJwtVcPropertiesV1.map(_.credentialSchema)
)
claims <- ZIO
.fromOption(request.sdJwtVcPropertiesV1.map(_.claims).orElse(request.claims))
.orElseFail(ErrorResponse.badRequest(detail = Some("Missing request parameter: claims")))
kid = request.sdJwtVcPropertiesV1
.flatMap(_.issuingKid)
.orElse(request.issuingKid) // TODO: should it be Option[KeyId]?
record <- credentialService
.createSDJWTIssueCredentialRecord(
pairwiseIssuerDID = offerContext.pairwiseIssuerDID,
pairwiseHolderDID = offerContext.pairwiseHolderDID,
kidIssuer = request.issuingKid,
kidIssuer = kid,
thid = DidCommID(),
credentialSchemaRef = Option(credentialSchemaRef),
claims = request.claims,
claims = claims,
validityPeriod = request.validityPeriod,
automaticIssuance = request.automaticIssuance.orElse(Some(true)),
issuingDID = issuingDID.asCanonical,
Expand All @@ -133,7 +168,7 @@ class IssueControllerImpl(
} yield record
case AnonCreds =>
for {
issuingDID <- getIssuingDidFromRequest(request)
issuingDID <- getIssuingDIDFromAnonCredsProperties(request)
credentialDefinitionGUID <- ZIO
.fromOption(request.credentialDefinitionId)
.mapError(_ =>
Expand Down Expand Up @@ -175,14 +210,17 @@ class IssueControllerImpl(
val urlPrefix = if (publicEndpointUrl.endsWith("/")) publicEndpointUrl else publicEndpointUrl + "/"
ZIO.succeed(s"$urlPrefix$httpUrlSuffix")
}
claims <- ZIO
.fromOption(request.anoncredsVcPropertiesV1.map(_.claims).orElse(request.claims))
.orElseFail(ErrorResponse.badRequest(detail = Some("Missing request parameter: claims")))
record <- credentialService
.createAnonCredsIssueCredentialRecord(
pairwiseIssuerDID = offerContext.pairwiseIssuerDID,
pairwiseHolderDID = offerContext.pairwiseHolderDID,
thid = DidCommID(),
credentialDefinitionGUID = credentialDefinitionGUID,
credentialDefinitionId = credentialDefinitionId,
claims = request.claims,
claims = claims,
validityPeriod = request.validityPeriod,
automaticIssuance = request.automaticIssuance.orElse(Some(true)),
goalCode = offerContext.goalCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ final case class CreateIssueCredentialRecordRequest(
@description(annotations.claims.description)
@encodedExample(annotations.claims.example)
@deprecated("Use specific properties of the verifiable credentials *.claims instead", "2.0.0")
claims: zio.json.ast.Json,
claims: Option[zio.json.ast.Json],
@description(annotations.automaticIssuance.description)
@encodedExample(annotations.automaticIssuance.example)
automaticIssuance: Option[Boolean] = None,
@description(annotations.issuingDID.description)
@encodedExample(annotations.issuingDID.example)
@deprecated("Use specific properties of the verifiable credentials *.issuingDID instead", "2.0.0")
issuingDID: String,
issuingDID: Option[String],
@description(annotations.issuingKid.description)
@encodedExample(annotations.issuingKid.example)
@deprecated("Use specific jwtVcPropertiesV1.issuingKid instead", "2.0.0")
Expand Down Expand Up @@ -130,16 +130,23 @@ case class JwtVCPropertiesV1(
@description(JwtVCPropertiesV1.annotations.issuingDID.description)
@encodedExample(JwtVCPropertiesV1.annotations.issuingDID.example)
issuingDID: String,
@description(annotations.issuingKid.description)
@encodedExample(annotations.issuingKid.example)
issuingKid: Option[KeyId],
@description(JwtVCPropertiesV1.annotations.validityPeriod.description)
@encodedExample(JwtVCPropertiesV1.annotations.validityPeriod.example)
validityPeriod: Double,
@description(JwtVCPropertiesV1.annotations.claims.description)
@encodedExample(JwtVCPropertiesV1.annotations.claims.example)
claims: zio.json.ast.Json,
@description(JwtVCPropertiesV1.annotations.credentialSchema.description)
@encodedExample(JwtVCPropertiesV1.annotations.credentialSchema.example)
credentialSchema: CredentialSchemaRef
)

object JwtVCPropertiesV1 {
import CreateIssueCredentialRecordRequest.schemaJson

given schema: Schema[JwtVCPropertiesV1] = Schema.derived
given encoder: JsonEncoder[JwtVCPropertiesV1] = DeriveJsonEncoder.gen
given decoder: JsonDecoder[JwtVCPropertiesV1] = DeriveJsonDecoder.gen
Expand Down Expand Up @@ -233,9 +240,18 @@ object AnonCredsVCPropertiesV1 {
}
}

case class SDJWTVCPropertiesV1(issuingDID: String, credentialSchema: CredentialSchemaRef, claims: zio.json.ast.Json)
case class SDJWTVCPropertiesV1(
issuingDID: String,
@description(annotations.issuingKid.description)
@encodedExample(annotations.issuingKid.example)
issuingKid: Option[KeyId],
credentialSchema: CredentialSchemaRef,
claims: zio.json.ast.Json
)

object SDJWTVCPropertiesV1 {
import CreateIssueCredentialRecordRequest.schemaJson

given schema: Schema[SDJWTVCPropertiesV1] = Schema.derived
given encoder: JsonEncoder[SDJWTVCPropertiesV1] = DeriveJsonEncoder.gen
given decoder: JsonDecoder[SDJWTVCPropertiesV1] = DeriveJsonDecoder.gen
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.hyperledger.identus.issue.controller

import org.hyperledger.identus.api.http.ErrorResponse
import org.hyperledger.identus.issue.controller.http.CredentialSchemaRef as HTTPCredentialSchemaRef
import org.hyperledger.identus.pollux.core.model.primitives.UriString
import org.hyperledger.identus.pollux.core.model.schema.{
CredentialSchemaRef as DomainCredentialSchemaRef,
CredentialSchemaRefType
}
import zio.test.*
import zio.test.Assertion.*

object CredentialSchemaReferenceParsingLogicSpec extends ZIOSpecDefault with CredentialSchemaReferenceParsingLogic {

private val credentialSchemaExample = "http://example.com/schema"

private def isErrorResponseWithDetailFieldEqualTo(detail: String) =
isSubtype[ErrorResponse](hasField("detail", _.detail, isSome(equalTo(detail))))

def spec = suite("CredentialSchemaReferenceParsingLogic")(
suite("parseCredentialSchemaRef_VCDM1_1")(
test("should parse valid schema ref with correct type") {
val httpSchemaRef = HTTPCredentialSchemaRef(credentialSchemaExample, "JsonSchemaValidator2018")
for {
result <- parseCredentialSchemaRef_VCDM1_1(None, Some(httpSchemaRef)).either
expectedUriString <- UriString.make(credentialSchemaExample).toZIO
} yield assert(result)(
isRight(
equalTo(DomainCredentialSchemaRef(CredentialSchemaRefType.JsonSchemaValidator2018, expectedUriString))
)
)
},
test("should fail for schema ref with invalid type") {
val httpSchemaRef = HTTPCredentialSchemaRef(credentialSchemaExample, "InvalidType")
for {
result <- parseCredentialSchemaRef_VCDM1_1(None, Some(httpSchemaRef)).either
} yield assert(result)(
isLeft(isErrorResponseWithDetailFieldEqualTo("Invalid credentialSchema type: InvalidType."))
)
},
test("should parse deprecated schema ID property") {
for {
result <- parseCredentialSchemaRef_VCDM1_1(Some(credentialSchemaExample), None).either
expectedUriString <- UriString.make(credentialSchemaExample).toZIO
} yield assert(result)(
isRight(
equalTo(DomainCredentialSchemaRef(CredentialSchemaRefType.JsonSchemaValidator2018, expectedUriString))
)
)
},
test("should fail if no schema is provided") {
for {
result <- parseCredentialSchemaRef_VCDM1_1(None, None).either
} yield assert(result)(
isLeft(isErrorResponseWithDetailFieldEqualTo("Credential schema property missed."))
)
}
),
suite("parseSchemaIdForAnonCredsModelV1")(
test("should parse schema ID property") {
for {
result <- parseSchemaIdForAnonCredsModelV1(None, Some(credentialSchemaExample)).either
expectedUriString <- UriString.make(credentialSchemaExample).toZIO
} yield assert(result)(isRight(equalTo(expectedUriString)))
},
test("should parse deprecated schema ID property") {
for {
result <- parseSchemaIdForAnonCredsModelV1(Some(credentialSchemaExample), None).either
expectedUriString <- UriString.make(credentialSchemaExample).toZIO
} yield assert(result)(isRight(equalTo(expectedUriString)))
},
test("should fail if no schema ID is provided") {
for {
result <- parseSchemaIdForAnonCredsModelV1(None, None).either
} yield assert(result)(
isLeft(isErrorResponseWithDetailFieldEqualTo("Credential schema property missed."))
)
}
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ object IssueControllerImplSpec extends ZIOSpecDefault with IssueControllerTestTo
schemaId = Some("mySchemaId"),
credentialDefinitionId = Some(UUID.fromString("123e4567-e89b-12d3-a456-426614174000")),
credentialFormat = Some("JWT"),
claims = json.fromJson[Json].toOption.get,
claims = json.fromJson[Json].toOption,
automaticIssuance = Some(true),
issuingDID =
"did:prism:332518729a7b7805f73a788e0944802527911901d9b7c16152281be9bc62d944:CosBCogBEkkKFW15LWtleS1hdXRoZW50aWNhdGlvbhAESi4KCXNlY3AyNTZrMRIhAuYoRIefsLhkvYwHz8gDtkG2b0kaZTDOLj_SExWX1fOXEjsKB21hc3RlcjAQAUouCglzZWNwMjU2azESIQLOzab8f0ibt1P0zdMfoWDQTSlPc8_tkV9Jk5BBsXB8fA",
issuingDID = Option(
"did:prism:332518729a7b7805f73a788e0944802527911901d9b7c16152281be9bc62d944:CosBCogBEkkKFW15LWtleS1hdXRoZW50aWNhdGlvbhAESi4KCXNlY3AyNTZrMRIhAuYoRIefsLhkvYwHz8gDtkG2b0kaZTDOLj_SExWX1fOXEjsKB21hc3RlcjAQAUouCglzZWNwMjU2azESIQLOzab8f0ibt1P0zdMfoWDQTSlPc8_tkV9Jk5BBsXB8fA"
),
issuingKid = Some(KeyId("some_kid_id")),
connectionId = Some(UUID.fromString("123e4567-e89b-12d3-a456-426614174000"))
)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies {
testImplementation("io.ktor:ktor-server-netty:2.3.0")
testImplementation("io.ktor:ktor-client-apache:2.3.0")
// RestAPI client
testImplementation("org.hyperledger.identus:cloud-agent-client-kotlin:1.40.1-d092f31")
testImplementation("org.hyperledger.identus:cloud-agent-client-kotlin:1.40.1-16c17ce")
// Test helpers library
testImplementation("io.iohk.atala:atala-automation:0.4.0")
// Hoplite for configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package common

import org.hyperledger.identus.client.models.*
import java.util.UUID

enum class CreateCredentialOfferAPIVersion {
V0 {
override fun buildCredentialOfferRequest(
credentialType: CredentialType,
did: String,
assertionKey: String,
schemaUrl: String?,
claims: Map<String, Any>,
connectionId: UUID,
validityPeriod: Double?,
): CreateIssueCredentialRecordRequest = CreateIssueCredentialRecordRequest(
schemaId = schemaUrl,
claims = claims,
issuingDID = did,
issuingKid = assertionKey,
connectionId = connectionId,
validityPeriod = validityPeriod ?: 3600.0,
credentialFormat = credentialType.format,
automaticIssuance = false,
)
},

V1 {
override fun buildCredentialOfferRequest(
credentialType: CredentialType,
did: String,
assertionKey: String,
schemaUrl: String?,
claims: Map<String, Any>,
connectionId: UUID,
validityPeriod: Double?,
): CreateIssueCredentialRecordRequest = CreateIssueCredentialRecordRequest(
issuingKid = assertionKey,
connectionId = connectionId,
credentialFormat = credentialType.format,
automaticIssuance = false,
claims = null,
issuingDID = "",
jwtVcPropertiesV1 = JwtVCPropertiesV1(
credentialSchema = CredentialSchemaRef(
id = schemaUrl!!,
type = "JsonSchemaValidator2018",
),
claims = claims,
issuingDID = did,
validityPeriod = validityPeriod ?: 3600.0,
),
)
},
;

abstract fun buildCredentialOfferRequest(
credentialType: CredentialType,
did: String,
assertionKey: String,
schemaUrl: String?,
claims: Map<String, Any>,
connectionId: UUID,
validityPeriod: Double? = null,
): CreateIssueCredentialRecordRequest
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package common

enum class CredentialClaims {
STUDENT_CLAIMS {
override val claims: Map<String, Any> = linkedMapOf(
"name" to "Name",
"age" to 18,
)
},
ID_CLAIMS {
override val claims: Map<String, Any> = linkedMapOf(
"firstName" to "First Name",
"lastName" to "Last Name",
)
},
;

abstract val claims: Map<String, Any>
}
Loading
Loading