Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: VC Verification Abstraction (#959)
Signed-off-by: Bassam Riman <bassam.riman@iohk.io>
- Loading branch information
1 parent
1e0a1a2
commit 596f8ed
Showing
10 changed files
with
168 additions
and
35 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...b/core/src/main/scala/io/iohk/atala/pollux/core/service/verification/VcVerification.scala
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,37 @@ | ||
package io.iohk.atala.pollux.core.service.verification | ||
|
||
import io.iohk.atala.pollux.core.service.verification.VcVerificationFailureType.ERROR | ||
import sttp.tapir.Schema | ||
import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder} | ||
|
||
enum VcVerificationFailureType { | ||
case WARN extends VcVerificationFailureType | ||
case ERROR extends VcVerificationFailureType | ||
} | ||
|
||
enum VcVerification( | ||
val failureType: VcVerificationFailureType | ||
) { | ||
case SignatureVerification extends VcVerification(ERROR) | ||
case IssuerIdentification extends VcVerification(ERROR) | ||
case ExpirationCheck extends VcVerification(ERROR) | ||
case NotBeforeCheck extends VcVerification(ERROR) | ||
case AudienceCheck extends VcVerification(ERROR) | ||
case SubjectVerification extends VcVerification(ERROR) | ||
case IntegrityOfClaims extends VcVerification(ERROR) | ||
case ComplianceWithStandards extends VcVerification(ERROR) | ||
case RevocationCheck extends VcVerification(ERROR) | ||
case AlgorithmVerification extends VcVerification(ERROR) | ||
case SchemaCheck extends VcVerification(ERROR) | ||
case SemanticCheckOfClaims extends VcVerification(ERROR) | ||
} | ||
|
||
object VcVerification { | ||
given encoder: JsonEncoder[VcVerification] = | ||
DeriveJsonEncoder.gen[VcVerification] | ||
|
||
given decoder: JsonDecoder[VcVerification] = | ||
DeriveJsonDecoder.gen[VcVerification] | ||
|
||
given schema: Schema[VcVerification] = Schema.derivedEnumeration.defaultStringBased | ||
} |
20 changes: 20 additions & 0 deletions
20
...src/main/scala/io/iohk/atala/pollux/core/service/verification/VcVerificationService.scala
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,20 @@ | ||
package io.iohk.atala.pollux.core.service.verification | ||
|
||
import zio.* | ||
|
||
trait VcVerificationService { | ||
def verify(request: List[VcVerificationRequest]): IO[VcVerificationServiceError, List[VcVerificationResult]] | ||
} | ||
|
||
final case class VcVerificationRequest( | ||
credential: String, | ||
verifications: List[VcVerification] | ||
) | ||
|
||
final case class VcVerificationResult( | ||
credential: String, | ||
checks: List[VcVerification], | ||
successfulChecks: List[VcVerification], | ||
failedChecks: List[VcVerification], | ||
failedAsWarningChecks: List[VcVerification] | ||
) |
9 changes: 9 additions & 0 deletions
9
...ain/scala/io/iohk/atala/pollux/core/service/verification/VcVerificationServiceError.scala
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,9 @@ | ||
package io.iohk.atala.pollux.core.service.verification | ||
|
||
sealed trait VcVerificationServiceError { | ||
def error: String | ||
} | ||
|
||
object VcVerificationServiceError { | ||
final case class UnexpectedError(error: String) extends VcVerificationServiceError | ||
} |
57 changes: 57 additions & 0 deletions
57
...main/scala/io/iohk/atala/pollux/core/service/verification/VcVerificationServiceImpl.scala
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,57 @@ | ||
package io.iohk.atala.pollux.core.service.verification | ||
|
||
import zio.{IO, *} | ||
|
||
class VcVerificationServiceImpl extends VcVerificationService { | ||
override def verify( | ||
vcVerificationRequests: List[VcVerificationRequest] | ||
): IO[VcVerificationServiceError, List[VcVerificationResult]] = { | ||
vcVerificationRequests.map(vcVerificationRequest => | ||
val verificationOutcomesZIO = ZIO.collectAll( | ||
vcVerificationRequest.verifications | ||
.map(verification => verify(vcVerificationRequest.credential, verification)) | ||
) | ||
|
||
verificationOutcomesZIO.map(verificationOutcomes => { | ||
val successfulChecks = verificationOutcomes.filter(_.success).map(_.verification) | ||
|
||
val failedVerifications = verificationOutcomes.filterNot(_.success).map(_.verification) | ||
|
||
val failedAsErrorChecks = | ||
failedVerifications.filter(verification => verification.failureType == VcVerificationFailureType.ERROR) | ||
|
||
val failedAsWarningChecks = | ||
failedVerifications.filter(verification => verification.failureType == VcVerificationFailureType.WARN) | ||
|
||
VcVerificationResult( | ||
credential = vcVerificationRequest.credential, | ||
checks = vcVerificationRequest.verifications, | ||
successfulChecks = successfulChecks, | ||
failedChecks = failedAsErrorChecks, | ||
failedAsWarningChecks = failedAsWarningChecks | ||
) | ||
}) | ||
) | ||
ZIO.succeed(List.empty) | ||
} | ||
|
||
private case class VcVerificationOutcome(verification: VcVerification, success: Boolean) | ||
private def verify( | ||
credential: String, | ||
verification: VcVerification | ||
): IO[VcVerificationServiceError, VcVerificationOutcome] = { | ||
verification match { | ||
case VcVerification.SchemaCheck => verifySchema(credential) | ||
case _ => ZIO.fail(VcVerificationServiceError.UnexpectedError(s"Unsupported Verification $verification")) | ||
} | ||
} | ||
|
||
private def verifySchema(credential: String): IO[VcVerificationServiceError, VcVerificationOutcome] = { | ||
ZIO.succeed(VcVerificationOutcome(verification = VcVerification.SchemaCheck, success = true)) | ||
} | ||
} | ||
|
||
object VcVerificationServiceImpl { | ||
val layer: ULayer[VcVerificationService] = | ||
ZLayer.succeed(VcVerificationServiceImpl()) | ||
} |
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
37 changes: 32 additions & 5 deletions
37
...r/src/main/scala/io/iohk/atala/verification/controller/VcVerificationControllerImpl.scala
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,19 +1,46 @@ | ||
package io.iohk.atala.verification.controller | ||
|
||
import io.iohk.atala.api.http.{ErrorResponse, RequestContext} | ||
import io.iohk.atala.pollux.core.service.verification.{VcVerification, VcVerificationRequest, VcVerificationService} | ||
import io.iohk.atala.verification.controller | ||
import zio.* | ||
|
||
class VcVerificationControllerImpl extends VcVerificationController { | ||
class VcVerificationControllerImpl(vcVerificationService: VcVerificationService) extends VcVerificationController { | ||
|
||
override def verify( | ||
request: List[controller.http.VcVerificationRequest] | ||
requests: List[controller.http.VcVerificationRequest] | ||
)(implicit rc: RequestContext): IO[ErrorResponse, List[controller.http.VcVerificationResponse]] = { | ||
ZIO.succeed(List.empty) | ||
val serviceRequests = | ||
requests.map(request => { | ||
val verifications = | ||
if (request.verifications.isEmpty) | ||
VcVerification.values.toList | ||
else | ||
request.verifications | ||
|
||
VcVerificationRequest( | ||
credential = request.credential, | ||
verifications = verifications | ||
) | ||
}) | ||
for { | ||
results <- | ||
vcVerificationService | ||
.verify(serviceRequests) | ||
.mapError(error => VcVerificationController.toHttpError(error)) | ||
} yield results.map(result => | ||
controller.http.VcVerificationResponse( | ||
result.credential, | ||
result.checks, | ||
result.successfulChecks, | ||
result.failedChecks, | ||
result.failedAsWarningChecks | ||
) | ||
) | ||
} | ||
} | ||
|
||
object VcVerificationControllerImpl { | ||
val layer: ULayer[VcVerificationController] = | ||
ZLayer.succeed(VcVerificationControllerImpl()) | ||
val layer: URLayer[VcVerificationService, VcVerificationController] = | ||
ZLayer.fromFunction(VcVerificationControllerImpl(_)) | ||
} |
30 changes: 0 additions & 30 deletions
30
...ice/server/src/main/scala/io/iohk/atala/verification/controller/http/VcVerification.scala
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...ver/src/main/scala/io/iohk/atala/verification/controller/http/VcVerificationRequest.scala
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
1 change: 1 addition & 0 deletions
1
...er/src/main/scala/io/iohk/atala/verification/controller/http/VcVerificationResponse.scala
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