Skip to content

Commit

Permalink
feat: Return 202 HTTP code (for DID Comm) (#162)
Browse files Browse the repository at this point in the history
The recommendation from the didcomm-messaging specs is to return a 202 Accepted.
See https://identity.foundation/didcomm-messaging/spec/#https
Resolves issue #160
Update integration-tests

Signed-off-by: Fabio Pinheiro <fabiomgpinheiro@gmail.com>

Signed-off-by: Shailesh Patil <shailesh.patil@iohk.io>
  • Loading branch information
FabioPinheiro authored and mineme0110 committed May 1, 2024
1 parent 51f2c27 commit 2ceab73
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 50 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
ATALA_GITHUB_ACTOR: ${{ secrets.ATALA_GITHUB_ACTOR }}
ATALA_GITHUB_TOKEN: ${{ secrets.ATALA_GITHUB_TOKEN }}
REPORTS_DIR: "didcomm-v2-mediator-test-suite/target/site/serenity"
DIDCOMM_V2_TESTSUITE_VERSION: "v0.1.0"
DIDCOMM_V2_TESTSUITE_VERSION: "3ed43f8f864ef0ea1cd89a9f81d3f379e8c0c217" # old "v0.1.0"
MEDIATOR_DID: "did:peer:2.Ez6LSghwSE437wnDE1pt3X6hVDUQzSjsHzinpX3XFvMjRAm7y.Vz6Mkhh1e5CEYYq6JBUcTZ6Cp2ranCWRrv7Yax3Le4N59R6dd.SeyJ0IjoiZG0iLCJzIjoiaHR0cDovL2xvY2FsaG9zdDo4MDgwIiwiciI6W10sImEiOlsiZGlkY29tbS92MiJdfQ"
steps:
- name: Checkout mediator
Expand All @@ -35,7 +35,7 @@ jobs:
uses: actions/checkout@v3
with:
repository: input-output-hk/didcomm-v2-mediator-test-suite
path: './didcomm-v2-mediator-test-suite'
path: "./didcomm-v2-mediator-test-suite"
ref: ${{ env.DIDCOMM_V2_TESTSUITE_VERSION }}

- name: Setup Java and Scala
Expand All @@ -62,7 +62,7 @@ jobs:
uses: ndeloof/install-compose-action@v0.0.1
with:
version: v2.19.1 # defaults to 'latest'
legacy: true # will also install in PATH as `docker-compose`
legacy: true # will also install in PATH as `docker-compose`

- name: Build local version of Mediator Agent
env:
Expand Down
104 changes: 57 additions & 47 deletions mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import scala.util.Try
import scala.io.Source
import zio.http.Header.AccessControlAllowOrigin
import zio.http.Header.AccessControlAllowMethods
import zio.http.Header.HeaderType

case class MediatorAgent(
override val id: DID,
Expand Down Expand Up @@ -267,53 +268,62 @@ object MediatorAgent {
} yield (ret)
},
Method.POST / trailing -> handler { (req: Request) =>
{
if (
req.headers
.get("content-type")
.exists { h => h == MediaTypes.SIGNED.typ || h == MediaTypes.ENCRYPTED.typ }
) {
for {
agent <- ZIO.service[MediatorAgent]
data <- req.body.asString
.catchAll(ex => ZIO.fail(Response.badRequest("Unable to read the body of the request")))
ret <- agent
.receiveMessage(data)
.map {
case None => Response.ok
case Some(value: SignedMessage) => Response.json(value.toJson)
case Some(value: EncryptedMessage) => Response.json(value.toJson)
}
.catchAll {
case MediatorDidError(error) =>
ZIO.logError(s"Error MediatorDidError: $error") *>
ZIO.succeed(Response.status(Status.BadRequest))
case MediatorThrowable(error) =>
ZIO.logError(s"Error MediatorThrowable: $error") *>
ZIO.succeed(Response.status(Status.BadRequest))
case StorageCollection(error) =>
ZIO.logError(s"Error StorageCollection: $error") *>
ZIO.succeed(Response.status(Status.BadRequest))
case StorageThrowable(error) =>
ZIO.logError(s"Error StorageThrowable: $error") *>
ZIO.succeed(Response.status(Status.BadRequest))
case DuplicateMessage(error) =>
ZIO.logError(s"Error DuplicateKeyError: $error") *>
ZIO.succeed(Response.status(Status.BadRequest))
case MissingProtocolError(piuri) =>
ZIO.logError(s"MissingProtocolError ('$piuri')") *>
ZIO.succeed(Response.status(Status.BadRequest)) // TODO
}
} yield ret
} else
ZIO
.logError(s"Request Headers: ${req.headers.mkString(",")}")
.as(
Response
.text(s"The content-type must be ${MediaTypes.SIGNED.typ} or ${MediaTypes.ENCRYPTED.typ}")
.copy(status = Status.BadRequest)
)
}
if (
req.headers
.get("content-type")
.exists { h => h == MediaTypes.SIGNED.typ || h == MediaTypes.ENCRYPTED.typ }
) {
for {
agent <- ZIO.service[MediatorAgent]
data <- req.body.asString
.catchAll(ex => ZIO.fail(Response.badRequest("Unable to read the body of the request")))
ret <- agent
.receiveMessage(data)
.map {
case None => Response(status = Status.Accepted)
case Some(value: SignedMessage) =>
Response(
status = Status.Accepted,
headers = Headers(Header.ContentType(MediaType.apply("application", "didcomm-signed+json"))),
body = Body.fromCharSequence(value.toJson)
)
case Some(value: EncryptedMessage) =>
Response(
status = Status.Accepted,
headers = Headers(Header.ContentType(MediaType.apply("application", "didcomm-encrypted+json"))),
body = Body.fromCharSequence(value.toJson)
)
}
.catchAll {
case MediatorDidError(error) =>
ZIO.logError(s"Error MediatorDidError: $error") *>
ZIO.succeed(Response.status(Status.BadRequest))
case MediatorThrowable(error) =>
ZIO.logError(s"Error MediatorThrowable: $error") *>
ZIO.succeed(Response.status(Status.BadRequest))
case StorageCollection(error) =>
ZIO.logError(s"Error StorageCollection: $error") *>
ZIO.succeed(Response.status(Status.BadRequest))
case StorageThrowable(error) =>
ZIO.logError(s"Error StorageThrowable: $error") *>
ZIO.succeed(Response.status(Status.BadRequest))
case DuplicateMessage(error) =>
ZIO.logError(s"Error DuplicateKeyError: $error") *>
ZIO.succeed(Response.status(Status.BadRequest))
case MissingProtocolError(piuri) =>
ZIO.logError(s"MissingProtocolError ('$piuri')") *>
ZIO.succeed(Response.status(Status.BadRequest)) // TODO
}
} yield ret
} else
ZIO
.logError(s"Request Headers: ${req.headers.mkString(",")}")
.as(
Response
.text(s"The content-type must be ${MediaTypes.SIGNED.typ} or ${MediaTypes.ENCRYPTED.typ}")
.copy(status = Status.BadRequest)
)

},
Method.GET / trailing -> handler { (req: Request) =>
for {
Expand Down

0 comments on commit 2ceab73

Please sign in to comment.