How to read Request payload in ServerServiceCall.composeAsync

(Amit) #1

In order to do authorization, we need to read the payload of in coming request in lagom service call method.
One way is to use ServerServiceCall.composeAsync but this api only gives us requestHeader.
Our requirement is to read the request payload to determine authorization parameter.

Below possible solution.

  def composeAsync1[Request, Response](block: (RequestHeader,Request) => Future[ServerServiceCall[Request, Response]]): ServerServiceCall[Request, Response] = {
    ServerServiceCall { (requestHeader, request) =>
      block(requestHeader,request).flatMap(_.invokeWithHeaders(requestHeader, request))
    }
  }

   override def securedRequest[Request, Response](serviceCall: String => ServerServiceCall[Request, Response]): ServerServiceCall[Request, Response] =
     composeAsync1 { (requestHeader,request) =>
      logger.debug(s"requestHeader  ${requestHeader.method.name} ${requestHeader.uri}")
     logger.debug(s"requestHeader  ${request.getClass.getName}  ${request.asInstanceOf[RepositoryTypeCreate].name}")
      requestHeader.principal match {
        case Some(userPrincipal: UserPrincipal) =>
          getContextActivity(requestHeader,None) flatMap{x =>
            checkAuthorization(x,userPrincipal.userId)map{ _ =>
              serviceCall(userPrincipal.userId)
            }
          }
        case other =>
          throw Forbidden("User not authenticated")
      }
    }

Will this usage cause any problem as i had to to remove “(Execution.trampoline)” from composeAsync1 method.

(Sergey Morgunov) #2

How to read Request payload in ServerServiceCall.composeAsync

It’s impossible now. We faced the same restriction in Lagom Pac4j (authenticate/authorize library for Lagom)

Regards,
Sergey