Extending SecuredRequest with ActionRefiner

Hi,

I am trying to add more information to SecuredRequest by doing this:

class ContextRequest[E <: Env, B](val context: Option[String], request: SecuredRequest[E, B]) extends WrappedRequest[B](request.request) {
  def identityV: Identity = request.identity
  def requestV: Request[B] = request.request
  def authenticatorV: Authenticator = request.authenticator
}

class ContextAction @Inject() (val parser: BodyParsers.Default)(implicit val executionContext: ExecutionContext)
  extends ActionRefiner[({ type R[B] = SecuredRequest[DefaultEnv, B] })#R, ({ type R[B] = ContextRequest[DefaultEnv, B] })#R] {

  override protected def refine[A](request: SecuredRequest[DefaultEnv, A]) = Future.successful {
    try {
      Right(new ContextRequest[DefaultEnv, A](Option("hello"), request))
    } catch {
      case _: Exception => Left(Results.NotFound)
    }
  }
}

And tried to use it by comsposing the action using andThen:

val silhouetteContextAction = silhouette.SecuredAction andThen contextAction

def testWithContext = silhouetteContextAction.async { request: ContextRequest[DefaultEnv, AnyContent] =>
    Future.successful(Ok(s"Processing: context:${request.context}"))
}

But I am running into this issue:

type mismatch; found : utils.ContextRequest[utils.auth.DefaultEnv,play.api.mvc.AnyContent] => scala.concurrent.Future[play.api.mvc.Result] required: play.api.mvc.WrappedRequest[play.api.mvc.AnyContent] => scala.concurrent.Future[play.api.mvc.Result]

Is this approach correct or I am missing anything?

Never mind, got it to work by adding the wrapper type’s for the SecuredAction and ContextAction:

type securedRequestWrapper[B] = SecuredRequest[DefaultEnv, B]
type contextRequestWrapper[B] = ContextRequest[DefaultEnv, B]

@ashrithr Thank you for your posts, they were helpful.

I am trying to achieve something really similar, but I am a little stuck.

I posted a question related to your posts on Stackoverflow -> https://stackoverflow.com/questions/53614284/play-2-6-silhouette-compose-securedaction