Search code examples
scalaplayframeworksilhouette

Play 2.6 Silhouette compose SecuredAction


I am trying to do something really similar to this. ContextRequest and ContextAction. I have a securedRequest from Silhouette and I want to compose an Action reading a cookie I set up.

the difference is that I extended the User from Silhouette Identity

DataSourceRequest

class DataSourceRequest[DefaultEnv, B](
  val dataSource: Option[String],
  val securedRequest: SecuredRequest[DefaultEnv, B]
) extends WrappedRequest[B](securedRequest.request) {
  def user: User = securedRequest.identity
  def requestV: Request[B] = securedRequest.request
}

DataSourceAction

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

  override protected def refine[A](securedRequest: SecuredRequest[DefaultEnv, A]) = Future.successful {
    try {
      Right(new DataSourceRequest[DefaultEnv, A](
        securedRequest.session.get("dataSource").getOrElse("dataSourceMissing"),
        securedRequest
      ))
    } catch {
      case _: Exception => Left(Results.NotFound)
    }
  }
}

DefaultEnv

trait DefaultEnv extends Env {
  type I = User
  type A = CookieAuthenticator
}

I am not able to get the User because

(Note that com.mohiva.play.silhouette.api.Identity does not match models.User: class User in package entities is a subclass of trait Identity in package api, but method parameter types must match exactly.)

I know that probably I am missing a wrapper.


Solution

  • Try giving a context bound to your Env, like this:

    class DataSourceRequest[Env <: DefaultEnv, B](
      val dataSource: Option[String],
      val securedRequest: SecuredRequest[Env, B]
    ) extends WrappedRequest[B](securedRequest.request) {
      def user: User = securedRequest.identity
      def requestV: Request[B] = securedRequest.request
    }