Skip to content

Commit

Permalink
fix: cleanup and minor refactoring to remove duplicates (#1309)
Browse files Browse the repository at this point in the history
Signed-off-by: mineme0110 <shailesh.patil@iohk.io>
  • Loading branch information
mineme0110 authored Aug 29, 2024
1 parent 75b2736 commit 238492b
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ object IssueBackgroundJobs extends BackgroundJobsHelper {
walletAccessContext <- ZIO
.fromOption(offer.to)
.flatMap(buildWalletAccessContextLayer)
.orElseFail(ZIO.die(new IllegalArgumentException("OfferCredential must have a recipient")))
.mapError(e => CredentialServiceError.CredentialOfferMissingField(id.value, "recipient"))
result <- for {
credentialService <- ZIO.service[CredentialService]
_ <- credentialService
Expand Down Expand Up @@ -274,7 +274,7 @@ object IssueBackgroundJobs extends BackgroundJobsHelper {
walletAccessContext <- ZIO
.fromOption(offer.to)
.flatMap(buildWalletAccessContextLayer)
.orElseFail(ZIO.die(new IllegalArgumentException("OfferCredential must have a recipient")))
.mapError(e => CredentialServiceError.CredentialOfferMissingField(id.value, "recipient"))
result <- for {
credentialService <- ZIO.service[CredentialService]
_ <- credentialService
Expand Down Expand Up @@ -320,7 +320,8 @@ object IssueBackgroundJobs extends BackgroundJobsHelper {
walletAccessContext <- ZIO
.fromOption(offer.to)
.flatMap(buildWalletAccessContextLayer)
.orElseFail(ZIO.die(new IllegalArgumentException("OfferCredential must have a recipient")))
.mapError(e => CredentialServiceError.CredentialOfferMissingField(id.value, "recipient"))

result <- for {
credentialService <- ZIO.service[CredentialService]
_ <- credentialService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,10 @@ object PresentBackgroundJobs extends BackgroundJobsHelper {
Unit
] = {
val proverPresentationPendingToGeneratedFlow = for {
walletAccessContext <- buildWalletAccessContextLayer(
requestPresentation.to.getOrElse(throw new RuntimeException("to is None is not possible"))
)
walletAccessContext <- ZIO
.fromOption(requestPresentation.to)
.flatMap(buildWalletAccessContextLayer)
.mapError(_ => PresentationError.RequestPresentationMissingField(id.value, "recipient"))
_ <- for {
presentationService <- ZIO.service[PresentationService]
prover <- createPrismDIDIssuerFromPresentationCredentials(id, credentialsToUse.getOrElse(Nil))
Expand All @@ -696,24 +697,7 @@ object PresentBackgroundJobs extends BackgroundJobsHelper {
presentationPayload.toW3CPresentationPayload,
prover
)
presentation <- ZIO.succeed(
Presentation(
body = Presentation.Body(
goal_code = requestPresentation.body.goal_code,
comment = requestPresentation.body.comment
),
attachments = Seq(
AttachmentDescriptor
.buildBase64Attachment(
payload = signedJwtPresentation.value.getBytes(),
mediaType = Some(PresentCredentialFormat.JWT.name)
)
),
thid = requestPresentation.thid.orElse(Some(requestPresentation.id)),
from = requestPresentation.to.getOrElse(throw new RuntimeException("to is None is not possible")),
to = requestPresentation.from.getOrElse(throw new RuntimeException("from is None is not possible"))
)
)
presentation <- createPresentation(id, requestPresentation, signedJwtPresentation)
} yield presentation
_ <- presentationService
.markPresentationGenerated(id, presentation)
Expand All @@ -724,6 +708,36 @@ object PresentBackgroundJobs extends BackgroundJobsHelper {
proverPresentationPendingToGeneratedFlow
}

private def createPresentation(
id: DidCommID,
requestPresentation: RequestPresentation,
signedJwtPresentation: JWT
): ZIO[Any, PresentationError, Presentation] = {
for {
from <- ZIO
.fromOption(requestPresentation.to)
.mapError(_ => PresentationError.RequestPresentationMissingField(id.value, "recipient"))
to <- ZIO
.fromOption(requestPresentation.from)
.mapError(_ => PresentationError.RequestPresentationMissingField(id.value, "sender"))
} yield Presentation(
body = Presentation.Body(
goal_code = requestPresentation.body.goal_code,
comment = requestPresentation.body.comment
),
attachments = Seq(
AttachmentDescriptor
.buildBase64Attachment(
payload = signedJwtPresentation.value.getBytes(),
mediaType = Some(PresentCredentialFormat.JWT.name)
)
),
thid = requestPresentation.thid.orElse(Some(requestPresentation.id)),
from = from,
to = to
)
}

private def handleSDJWT(
id: DidCommID,
credentialsToUse: Option[List[String]],
Expand All @@ -733,9 +747,10 @@ object PresentBackgroundJobs extends BackgroundJobsHelper {
ERROR,
Unit
] = for {
walletAccessContext <- buildWalletAccessContextLayer(
requestPresentation.to.getOrElse(throw new RuntimeException("to is None is not possible"))
)
walletAccessContext <- ZIO
.fromOption(requestPresentation.to)
.flatMap(buildWalletAccessContextLayer)
.mapError(_ => PresentationError.RequestPresentationMissingField(id.value, "recipient"))
result <-
for {
presentationService <- ZIO.service[PresentationService]
Expand Down Expand Up @@ -765,9 +780,10 @@ object PresentBackgroundJobs extends BackgroundJobsHelper {
maybeCredentialsToUseJson match {
case Some(credentialsToUseJson) =>
val proverPresentationPendingToGeneratedFlow = for {
walletAccessContext <- buildWalletAccessContextLayer(
requestPresentation.to.getOrElse(throw new RuntimeException("to is None is not possible"))
)
walletAccessContext <- ZIO
.fromOption(requestPresentation.to)
.flatMap(buildWalletAccessContextLayer)
.mapError(_ => PresentationError.RequestPresentationMissingField(id.value, "recipient"))
result <- for {
presentationService <- ZIO.service[PresentationService]
anoncredCredentialProofs <-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ trait PresentProofController {
): ZIO[WalletAccessContext, ErrorResponse, PresentationStatus]

def createOOBRequestPresentationInvitation(
request: OOBRequestPresentationInput
request: RequestPresentationInput
)(implicit
rc: RequestContext
): ZIO[WalletAccessContext, ErrorResponse, PresentationStatus]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,32 @@ class PresentProofControllerImpl(
override def requestPresentation(request: RequestPresentationInput)(implicit
rc: RequestContext
): ZIO[WalletAccessContext, ErrorResponse, PresentationStatus] = {
val result: ZIO[WalletAccessContext, ConnectionServiceError | PresentationError, PresentationStatus] = for {
didIdPair <- getPairwiseDIDs(request.connectionId).provideSomeLayer(ZLayer.succeed(connectionService))
record <- createRequestPresentation(
verifierDID = didIdPair.myDID,
proverDID = Some(didIdPair.theirDid),
connectionId = Some(request.connectionId.toString),
request = request
)
} yield PresentationStatus.fromDomain(record)
val result: ZIO[WalletAccessContext, ConnectionServiceError | PresentationError, PresentationStatus] =
for {
connectionId <- ZIO
.fromOption(request.connectionId)
.mapError(_ => PresentationError.MissingConnectionIdForPresentationRequest)
didIdPair <- getPairwiseDIDs(connectionId).provideSomeLayer(ZLayer.succeed(connectionService))
record <- createRequestPresentation(
verifierDID = didIdPair.myDID,
proverDID = Some(didIdPair.theirDid),
request = request,
expirationDuration = None
)
} yield PresentationStatus.fromDomain(record)
result
}

override def createOOBRequestPresentationInvitation(request: OOBRequestPresentationInput)(implicit
override def createOOBRequestPresentationInvitation(request: RequestPresentationInput)(implicit
rc: RequestContext
): ZIO[WalletAccessContext, ErrorResponse, PresentationStatus] = {
val result: ZIO[WalletAccessContext, ConnectionServiceError | PresentationError, PresentationStatus] = for {
peerDid <- managedDIDService.createAndStorePeerDID(appConfig.agent.didCommEndpoint.publicEndpointUrl)
record <- createRequestPresentation(
verifierDID = peerDid.did,
proverDID = None,
connectionId = None,
request = request
request = request,
expirationDuration = Some(appConfig.pollux.presentationInvitationExpiry)
)
} yield PresentationStatus.fromDomain(record)
result
Expand All @@ -64,39 +68,22 @@ class PresentProofControllerImpl(
private def createRequestPresentation(
verifierDID: DidId,
proverDID: Option[DidId],
connectionId: Option[String],
request: RequestPresentationInput | OOBRequestPresentationInput
request: RequestPresentationInput,
expirationDuration: Option[Duration]
): ZIO[WalletAccessContext, PresentationError, PresentationRecord] = {
request match {
case req: RequestPresentationInput =>
createPresentationRecord(
verifierDID,
proverDID,
connectionId,
req.credentialFormat,
req.proofs,
req.options.map(o => Options(o.challenge, o.domain)),
req.claims,
req.anoncredPresentationRequest,
None,
None,
None
)
case req: OOBRequestPresentationInput =>
createPresentationRecord(
verifierDID,
proverDID,
connectionId,
req.credentialFormat,
req.proofs,
req.options.map(o => Options(o.challenge, o.domain)),
req.claims,
req.anoncredPresentationRequest,
req.goalCode,
req.goal,
Some(appConfig.pollux.presentationInvitationExpiry)
)
}
createPresentationRecord(
verifierDID,
proverDID,
request.connectionId.map(_.toString),
request.credentialFormat,
request.proofs,
request.options.map(o => Options(o.challenge, o.domain)),
request.claims,
request.anoncredPresentationRequest,
request.goalCode,
request.goal,
expirationDuration
)
}

private def createPresentationRecord(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ object PresentProofEndpoints {

val createOOBRequestPresentationInvitation: Endpoint[
(ApiKeyCredentials, JwtCredentials),
(RequestContext, OOBRequestPresentationInput),
(RequestContext, RequestPresentationInput),
ErrorResponse,
PresentationStatus,
Any
Expand All @@ -143,7 +143,7 @@ object PresentProofEndpoints {
.securityIn(jwtAuthHeader)
.in("present-proof" / "presentations" / "invitation")
.in(extractFromRequest[RequestContext](RequestContext.apply))
.in(jsonBody[OOBRequestPresentationInput].description("The present proof creation request."))
.in(jsonBody[RequestPresentationInput].description("The present proof creation request."))
.out(
statusCode(StatusCode.Created).description(
"The proof presentation request invitation was created successfully and that can be delivered as out-of-band to a peer Agent.."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.hyperledger.identus.api.http.RequestContext
import org.hyperledger.identus.iam.authentication.{Authenticator, Authorizer, DefaultAuthenticator, SecurityLogic}
import org.hyperledger.identus.presentproof.controller.http.{
AcceptRequestPresentationInvitation,
OOBRequestPresentationInput,
RequestPresentationAction,
RequestPresentationInput
}
Expand Down Expand Up @@ -82,7 +81,7 @@ class PresentProofServerEndpoints(
createOOBRequestPresentationInvitation
.zServerSecurityLogic(SecurityLogic.authorizeWalletAccessWith(_)(authenticator, authorizer))
.serverLogic { wac =>
{ case (ctx: RequestContext, action: OOBRequestPresentationInput) =>
{ case (ctx: RequestContext, action: RequestPresentationInput) =>
presentProofController
.createOOBRequestPresentationInvitation(action)(ctx)
.provideSomeLayer(ZLayer.succeed(wac))
Expand Down
Loading

0 comments on commit 238492b

Please sign in to comment.