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

feat(prism-agent): fix DID service URI class and improve validation error response message #389

Merged
merged 3 commits into from
Feb 22, 2023
Merged
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
4 changes: 2 additions & 2 deletions prism-agent/service/project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ object Dependencies {
val zioInteropCats = "3.3.0" // scala-steward:off
val akka = "2.6.20"
val akkaHttp = "10.2.9"
val castor = "0.8.0"
val pollux = "0.28.1"
val castor = "0.8.1"
val pollux = "0.29.0"
val connect = "0.10.1"
val bouncyCastle = "1.70"
val logback = "1.4.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import io.iohk.atala.shared.models.HexStrings.*
import io.iohk.atala.shared.models.Base64UrlStrings.*
import io.iohk.atala.shared.utils.Traverse.*

import java.net.URI
import io.lemonlabs.uri.Uri
import scala.util.Try
import java.time.OffsetDateTime
import java.time.ZoneOffset
Expand All @@ -50,16 +50,18 @@ trait OASDomainModelHelper {
def toDomain: Either[String, castorDomain.Service] = {
for {
serviceEndpoint <- service.serviceEndpoint.traverse(s =>
Try(URI.create(s)).toEither.left.map(_ => s"unable to parse serviceEndpoint $s as URI")
Uri.parseTry(s).toEither.left.map(_ => s"unable to parse serviceEndpoint $s as URI")
)
serviceType <- castorDomain.ServiceType
.parseString(service.`type`)
.toRight(s"unsupported serviceType ${service.`type`}")
} yield castorDomain.Service(
id = service.id,
`type` = serviceType,
serviceEndpoint = serviceEndpoint
)
} yield castorDomain
.Service(
id = service.id,
`type` = serviceType,
serviceEndpoint = serviceEndpoint
)
.normalizeServiceEndpoint()
}
}

Expand Down Expand Up @@ -112,7 +114,7 @@ trait OASDomainModelHelper {
for {
serviceEndpoint <- servicePatch.serviceEndpoint
.getOrElse(Nil)
.traverse(s => Try(URI.create(s)).toEither.left.map(_ => s"unable to parse serviceEndpoint $s as URI"))
.traverse(s => Uri.parseTry(s).toEither.left.map(_ => s"unable to parse serviceEndpoint $s as URI"))
serviceType <- servicePatch.`type`.fold[Either[String, Option[ServiceType]]](Right(None))(s =>
castorDomain.ServiceType.parseString(s).toRight(s"unsupported serviceType $s").map(Some(_))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.iohk.atala.agent.walletapi.model.error.{
}
import io.iohk.atala.castor.core.model.did.w3c.DIDResolutionErrorRepr
import io.iohk.atala.castor.core.model.error.DIDOperationError
import io.iohk.atala.castor.core.model.error.OperationValidationError
import io.iohk.atala.castor.core.model.error.DIDResolutionError
import io.iohk.atala.connect.core.model.error.ConnectionServiceError
import io.iohk.atala.pollux.core.model.error.CredentialServiceError
Expand Down Expand Up @@ -44,18 +45,6 @@ trait OASErrorModelHelper {
}
}

given ToErrorResponse[DIDOperationError] with {
override def toErrorResponse(e: DIDOperationError): ErrorResponse = {
ErrorResponse(
`type` = "error-type",
title = "error-title",
status = 500,
detail = Some(e.toString),
instance = "error-instance"
)
}
}

given ToErrorResponse[GetManagedDIDError] with {
override def toErrorResponse(e: GetManagedDIDError): ErrorResponse = {
ErrorResponse(
Expand All @@ -82,11 +71,18 @@ trait OASErrorModelHelper {

given ToErrorResponse[CreateManagedDIDError] with {
override def toErrorResponse(e: CreateManagedDIDError): ErrorResponse = {
val (status, detail) = e match {
case CreateManagedDIDError.InvalidArgument(msg) => (422, s"Unable to construct a DID operation: $msg")
case CreateManagedDIDError.DIDAlreadyExists(did) => (409, s"DID ${did.toString} already exists")
case CreateManagedDIDError.KeyGenerationError(_) => (500, s"Internal server error (key-pair generation)")
case CreateManagedDIDError.WalletStorageError(_) => (500, s"Internal server error (storage)")
case CreateManagedDIDError.InvalidOperation(cause) => (422, s"Create DID payload is invalid: ${cause.toString}")
}
ErrorResponse(
`type` = "error-type",
title = "error-title",
status = 500,
detail = Some(e.toString),
status = status,
detail = Some(detail),
instance = "error-instance"
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.iohk.atala.agent.walletapi.model

import io.iohk.atala.castor.core.model.did.{Service, VerificationRelationship, ServiceType}

import java.net.URI
import io.lemonlabs.uri.Uri

final case class ManagedDIDTemplate(
publicKeys: Seq[DIDPublicKeyTemplate],
Expand All @@ -24,4 +24,4 @@ object UpdateManagedDIDAction {
final case class UpdateService(patch: UpdateServicePatch) extends UpdateManagedDIDAction
}

final case class UpdateServicePatch(id: String, serviceType: Option[ServiceType], serviceEndpoints: Seq[URI])
final case class UpdateServicePatch(id: String, serviceType: Option[ServiceType], serviceEndpoints: Seq[Uri])
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import zio.*
import zio.test.*
import zio.test.Assertion.*

import java.net.URI
import io.lemonlabs.uri.Uri

object ManagedDIDTemplateValidatorSpec extends ZIOSpecDefault {

Expand All @@ -28,7 +28,7 @@ object ManagedDIDTemplateValidatorSpec extends ZIOSpecDefault {
Service(
id = "service0",
`type` = ServiceType.LinkedDomains,
serviceEndpoint = Seq(URI.create("http://example.com"))
serviceEndpoint = Seq(Uri.parse("http://example.com/"))
)
)
)
Expand Down