Skip to content

Commit

Permalink
feat: Default Backend API to Array Of Credential Schema (#1366)
Browse files Browse the repository at this point in the history
Signed-off-by: Bassam Riman <[email protected]>
  • Loading branch information
CryptoKnightIOG authored Sep 26, 2024
1 parent e532ba6 commit 693dcc4
Show file tree
Hide file tree
Showing 22 changed files with 205 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import org.hyperledger.identus.api.util.PaginationUtils
import org.hyperledger.identus.castor.core.model.did.{PrismDID, VerificationRelationship}
import org.hyperledger.identus.castor.core.service.DIDService
import org.hyperledger.identus.connect.core.service.ConnectionService
import org.hyperledger.identus.issue.controller.http.{
AcceptCredentialOfferInvitation,
AcceptCredentialOfferRequest,
CreateIssueCredentialRecordRequest,
IssueCredentialRecord,
IssueCredentialRecordPage
}
import org.hyperledger.identus.issue.controller.http.*
import org.hyperledger.identus.mercury.model.DidId
import org.hyperledger.identus.pollux.core.model.{CredentialFormat, DidCommID}
import org.hyperledger.identus.pollux.core.model.CredentialFormat.{AnonCreds, JWT, SDJWT}
Expand Down Expand Up @@ -48,6 +42,7 @@ class IssueControllerImpl(
request: CreateIssueCredentialRecordRequest,
offerContext: OfferContext
): ZIO[WalletAccessContext, ErrorResponse, IssueCredentialRecord] = {

for {
jsonClaims <- ZIO
.fromEither(io.circe.parser.parse(request.claims.toString()))
Expand All @@ -69,7 +64,10 @@ class IssueControllerImpl(
pairwiseHolderDID = offerContext.pairwiseHolderDID,
kidIssuer = request.issuingKid,
thid = DidCommID(),
maybeSchemaId = request.schemaId,
maybeSchemaIds = request.schemaId.map {
case schemaId: String => List(schemaId)
case schemaIds: List[String] => schemaIds
},
claims = jsonClaims,
validityPeriod = request.validityPeriod,
automaticIssuance = request.automaticIssuance.orElse(Some(true)),
Expand All @@ -94,7 +92,10 @@ class IssueControllerImpl(
pairwiseHolderDID = offerContext.pairwiseHolderDID,
kidIssuer = request.issuingKid,
thid = DidCommID(),
maybeSchemaId = request.schemaId,
maybeSchemaIds = request.schemaId.map {
case schemaId: String => List(schemaId)
case schemaIds: List[String] => schemaIds
},
claims = jsonClaims,
validityPeriod = request.validityPeriod,
automaticIssuance = request.automaticIssuance.orElse(Some(true)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import sttp.tapir.Schema.annotations.{description, encodedExample}
import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder}

import java.util.UUID
import scala.language.implicitConversions

/** A class to represent an incoming request to create a new credential offer.
*
Expand All @@ -33,7 +34,7 @@ final case class CreateIssueCredentialRecordRequest(
validityPeriod: Option[Double] = None,
@description(annotations.schemaId.description)
@encodedExample(annotations.schemaId.example)
schemaId: Option[String],
schemaId: Option[String | List[String]] = None,
@description(annotations.credentialDefinitionId.description)
@encodedExample(annotations.credentialDefinitionId.example)
credentialDefinitionId: Option[UUID],
Expand Down Expand Up @@ -178,13 +179,37 @@ object CreateIssueCredentialRecordRequest {
)
}

given schemaIdEncoder: JsonEncoder[String | List[String]] =
JsonEncoder[String]
.orElseEither(JsonEncoder[List[String]])
.contramap[String | List[String]] {
case schemaId: String => Left(schemaId)
case schemaIds: List[String] => Right(schemaIds)
}

given schemaIdDecoder: JsonDecoder[String | List[String]] =
JsonDecoder[List[String]]
.map(schemaId => schemaId: String | List[String])
.orElse(JsonDecoder[String].map(schemaId => schemaId: String | List[String]))

given encoder: JsonEncoder[CreateIssueCredentialRecordRequest] =
DeriveJsonEncoder.gen[CreateIssueCredentialRecordRequest]

given decoder: JsonDecoder[CreateIssueCredentialRecordRequest] =
DeriveJsonDecoder.gen[CreateIssueCredentialRecordRequest]

given schemaJson: Schema[KeyId] = Schema.schemaForString.map[KeyId](v => Some(KeyId(v)))(KeyId.value)

given schemaId: Schema[String | List[String]] = Schema
.schemaForEither(Schema.schemaForString, Schema.schemaForArray[String])
.map[String | List[String]] {
case Left(value) => Some(value)
case Right(values) => Some(values.toList)
} {
case value: String => Left(value)
case values: List[String] => Right(values.toArray)
}

given schema: Schema[CreateIssueCredentialRecordRequest] = Schema.derived

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ object VcVerificationControllerImplSpec extends ZIOSpecDefault with VcVerificati
maybeValidFrom = Some(Instant.parse("2010-01-12T00:00:00Z")),
maybeValidUntil = Some(Instant.parse("2010-01-12T00:00:00Z")),
maybeCredentialSchema = Some(
Left(
CredentialSchema(
id = "did:work:MDP8AsFhHzhwUvGNuYkX7T;id=06e126d1-fa44-4882-a243-1e326fbe21db;version=1.0",
`type` = "JsonSchemaValidator2018"
)
CredentialSchema(
id = "did:work:MDP8AsFhHzhwUvGNuYkX7T;id=06e126d1-fa44-4882-a243-1e326fbe21db;version=1.0",
`type` = "JsonSchemaValidator2018"
)
),
credentialSubject = Json.obj(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ import io.circe.generic.semiauto.*
*/
final case class CredentialPreview(
`type`: String = "https://didcomm.org/issue-credential/3.0/credential-credential",
schema_ids: Option[List[String]] = None,
schema_id: Option[String] = None,
body: CredentialPreviewBody,
)

object CredentialPreview {
def apply(attributes: Seq[Attribute]) = new CredentialPreview(body = CredentialPreviewBody(attributes))
def apply(schema_id: Option[String], attributes: Seq[Attribute]) =
new CredentialPreview(schema_id = schema_id, body = CredentialPreviewBody(attributes))
def apply(schema_ids: Option[List[String]], attributes: Seq[Attribute]) =
new CredentialPreview(
schema_ids = schema_ids,
// Done for backward compatibility
schema_id = schema_ids.flatMap(s => s.headOption),
body = CredentialPreviewBody(attributes)
)

given Encoder[CredentialPreview] = deriveEncoder[CredentialPreview]
given Decoder[CredentialPreview] = deriveDecoder[CredentialPreview]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final case class IssueCredentialRecord(
createdAt: Instant,
updatedAt: Option[Instant],
thid: DidCommID,
schemaUri: Option[String],
schemaUris: Option[List[String]],
credentialDefinitionId: Option[UUID],
credentialDefinitionUri: Option[String],
credentialFormat: CredentialFormat,
Expand Down Expand Up @@ -86,7 +86,7 @@ final case class ValidFullIssuedCredentialRecord(
id: DidCommID,
issuedCredential: Option[IssueCredential],
credentialFormat: CredentialFormat,
schemaUri: Option[String],
schemaUris: Option[List[String]],
credentialDefinitionUri: Option[String],
subjectId: Option[String],
keyId: Option[KeyId],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ trait CredentialRepository {
recordId: DidCommID,
issue: IssueCredential,
issuedRawCredential: String,
schemaUri: Option[String],
schemaUris: Option[List[String]],
credentialDefinitionUri: Option[String],
protocolState: ProtocolState
): URIO[WalletAccessContext, Unit]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait CredentialService {
pairwiseHolderDID: Option[DidId],
kidIssuer: Option[KeyId],
thid: DidCommID,
maybeSchemaId: Option[String],
maybeSchemaIds: Option[List[String]],
claims: io.circe.Json,
validityPeriod: Option[Double] = None,
automaticIssuance: Option[Boolean],
Expand All @@ -43,7 +43,7 @@ trait CredentialService {
pairwiseHolderDID: Option[DidId],
kidIssuer: Option[KeyId],
thid: DidCommID,
maybeSchemaId: Option[String],
maybeSchemaIds: Option[List[String]],
claims: io.circe.Json,
validityPeriod: Option[Double] = None,
automaticIssuance: Option[Boolean],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class CredentialServiceImpl(
pairwiseIssuerDID: DidId,
kidIssuer: Option[KeyId],
thid: DidCommID,
schemaUri: Option[String],
schemaUris: Option[List[String]],
validityPeriod: Option[Double],
automaticIssuance: Option[Boolean],
issuingDID: Option[CanonicalPrismDID],
Expand Down Expand Up @@ -161,7 +161,7 @@ class CredentialServiceImpl(
createdAt = Instant.now,
updatedAt = None,
thid = thid,
schemaUri = schemaUri,
schemaUris = schemaUris,
credentialDefinitionId = credentialDefinitionGUID,
credentialDefinitionUri = credentialDefinitionId,
credentialFormat = credentialFormat,
Expand Down Expand Up @@ -196,7 +196,7 @@ class CredentialServiceImpl(
pairwiseHolderDID: Option[DidId],
kidIssuer: Option[KeyId],
thid: DidCommID,
maybeSchemaId: Option[String],
maybeSchemaIds: Option[List[String]],
claims: Json,
validityPeriod: Option[Double],
automaticIssuance: Option[Boolean],
Expand All @@ -207,12 +207,12 @@ class CredentialServiceImpl(
connectionId: Option[UUID],
): URIO[WalletAccessContext, IssueCredentialRecord] = {
for {
_ <- validateClaimsAgainstSchemaIfAny(claims, maybeSchemaId)
_ <- validateClaimsAgainstSchemaIfAny(claims, maybeSchemaIds)
attributes <- CredentialService.convertJsonClaimsToAttributes(claims)
offer <- createDidCommOfferCredential(
pairwiseIssuerDID = pairwiseIssuerDID,
pairwiseHolderDID = pairwiseHolderDID,
maybeSchemaId = maybeSchemaId,
maybeSchemaIds = maybeSchemaIds,
claims = attributes,
thid = thid,
UUID.randomUUID().toString,
Expand All @@ -223,7 +223,7 @@ class CredentialServiceImpl(
pairwiseIssuerDID = pairwiseIssuerDID,
kidIssuer = kidIssuer,
thid = thid,
schemaUri = maybeSchemaId,
schemaUris = maybeSchemaIds,
validityPeriod = validityPeriod,
automaticIssuance = automaticIssuance,
issuingDID = Some(issuingDID),
Expand All @@ -244,7 +244,7 @@ class CredentialServiceImpl(
pairwiseHolderDID: Option[DidId],
kidIssuer: Option[KeyId],
thid: DidCommID,
maybeSchemaId: Option[String],
maybeSchemaIds: Option[List[String]],
claims: io.circe.Json,
validityPeriod: Option[Double] = None,
automaticIssuance: Option[Boolean],
Expand All @@ -255,12 +255,12 @@ class CredentialServiceImpl(
connectionId: Option[UUID],
): URIO[WalletAccessContext, IssueCredentialRecord] = {
for {
_ <- validateClaimsAgainstSchemaIfAny(claims, maybeSchemaId)
_ <- validateClaimsAgainstSchemaIfAny(claims, maybeSchemaIds)
attributes <- CredentialService.convertJsonClaimsToAttributes(claims)
offer <- createDidCommOfferCredential(
pairwiseIssuerDID = pairwiseIssuerDID,
pairwiseHolderDID = pairwiseHolderDID,
maybeSchemaId = maybeSchemaId,
maybeSchemaIds = maybeSchemaIds,
claims = attributes,
thid = thid,
UUID.randomUUID().toString,
Expand All @@ -271,7 +271,7 @@ class CredentialServiceImpl(
pairwiseIssuerDID = pairwiseIssuerDID,
kidIssuer = kidIssuer,
thid = thid,
schemaUri = maybeSchemaId,
schemaUris = maybeSchemaIds,
validityPeriod = validityPeriod,
automaticIssuance = automaticIssuance,
issuingDID = Some(issuingDID),
Expand Down Expand Up @@ -320,7 +320,7 @@ class CredentialServiceImpl(
pairwiseIssuerDID = pairwiseIssuerDID,
kidIssuer = None,
thid = thid,
schemaUri = Some(credentialDefinition.schemaId),
schemaUris = Some(List(credentialDefinition.schemaId)),
validityPeriod = validityPeriod,
automaticIssuance = automaticIssuance,
issuingDID = None,
Expand Down Expand Up @@ -375,7 +375,7 @@ class CredentialServiceImpl(
createdAt = Instant.now,
updatedAt = None,
thid = DidCommID(offer.thid.getOrElse(offer.id)),
schemaUri = None,
schemaUris = None,
credentialDefinitionId = None,
credentialDefinitionUri = None,
credentialFormat = credentialFormat,
Expand Down Expand Up @@ -438,12 +438,19 @@ class CredentialServiceImpl(

private[this] def validateClaimsAgainstSchemaIfAny(
claims: Json,
maybeSchemaId: Option[String]
): UIO[Unit] = maybeSchemaId match
case Some(schemaId) =>
CredentialSchema
.validateJWTCredentialSubject(schemaId, claims.noSpaces, uriDereferencer)
.orDieAsUnmanagedFailure
maybeSchemaIds: Option[List[String]]
): UIO[Unit] = maybeSchemaIds match
case Some(schemaIds) =>
for {
_ <- ZIO
.collectAll(
schemaIds.map(schemaId =>
CredentialSchema
.validateJWTCredentialSubject(schemaId, claims.noSpaces, uriDereferencer)
)
)
.orDieAsUnmanagedFailure
} yield ZIO.unit
case None =>
ZIO.unit

Expand Down Expand Up @@ -806,7 +813,7 @@ class CredentialServiceImpl(
processedIssuedCredential,
record,
attachment,
Some(processedCredential.getSchemaId),
Some(List(processedCredential.getSchemaId)),
Some(processedCredential.getCredDefId)
)
} yield result
Expand All @@ -822,7 +829,7 @@ class CredentialServiceImpl(
issueCredential: IssueCredential,
record: IssueCredentialRecord,
attachment: AttachmentDescriptor,
schemaId: Option[String],
schemaId: Option[List[String]],
credDefId: Option[String]
) = {
credentialRepository
Expand Down Expand Up @@ -957,15 +964,15 @@ class CredentialServiceImpl(
private def createDidCommOfferCredential(
pairwiseIssuerDID: DidId,
pairwiseHolderDID: Option[DidId],
maybeSchemaId: Option[String],
maybeSchemaIds: Option[List[String]],
claims: Seq[Attribute],
thid: DidCommID,
challenge: String,
domain: String,
offerFormat: IssueCredentialOfferFormat
): UIO[OfferCredential] = {
for {
credentialPreview <- ZIO.succeed(CredentialPreview(schema_id = maybeSchemaId, attributes = claims))
credentialPreview <- ZIO.succeed(CredentialPreview(schema_ids = maybeSchemaIds, attributes = claims))
body = OfferCredential.Body(
goal_code = Some("Offer Credential"),
credential_preview = credentialPreview,
Expand Down Expand Up @@ -1001,7 +1008,7 @@ class CredentialServiceImpl(
thid: DidCommID
): URIO[WalletAccessContext, OfferCredential] = {
for {
credentialPreview <- ZIO.succeed(CredentialPreview(schema_id = Some(schemaUri), attributes = claims))
credentialPreview <- ZIO.succeed(CredentialPreview(schema_ids = Some(List(schemaUri)), attributes = claims))
body = OfferCredential.Body(
goal_code = Some("Offer Credential"),
credential_preview = credentialPreview,
Expand Down Expand Up @@ -1143,8 +1150,8 @@ class CredentialServiceImpl(
issuer = CredentialIssuer(jwtIssuer.did.toString, `type` = "Profile"),
issuanceDate = issuanceDate,
maybeExpirationDate = record.validityPeriod.map(sec => issuanceDate.plusSeconds(sec.toLong)),
maybeCredentialSchema = record.schemaUri.map(id =>
Left(org.hyperledger.identus.pollux.vc.jwt.CredentialSchema(id, VC_JSON_SCHEMA_TYPE))
maybeCredentialSchema = record.schemaUris.map(ids =>
ids.map(id => org.hyperledger.identus.pollux.vc.jwt.CredentialSchema(id, VC_JSON_SCHEMA_TYPE))
),
maybeCredentialStatus = Some(credentialStatus),
credentialSubject = claims.add("id", jwtPresentation.iss.asJson).asJson,
Expand Down
Loading

0 comments on commit 693dcc4

Please sign in to comment.