Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.6 ActionBuilder

I upgraded my Play application from 2.5 to 2.6 today and i got a problem with ActionBuilder. The docs state:

The Scala ActionBuilder trait has been modified to specify the type of the body as a type parameter, and add an abstract parser member as the default body parsers. You will need to modify your ActionBuilders and pass the body parser directly.

documentation

Sadly I haven't found any example and i don't know how to fix that:

class AuthenticatedRequest[A](val token: ProfileTokenData, request: Request[A]) extends WrappedRequest[A](request)

trait Secured {

  object SetExtractor {
    def unapplySeq[T](s: Set[T]): Option[Seq[T]] = Some(s.toSeq)
  }

  def Authenticated = new ActionBuilder[AuthenticatedRequest] with JWTTokenProcess {
    override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] = {
      request.jwtSession.claimData.asOpt[JWTToken] match {
        case Some(token) => block(new AuthenticatedRequest(ProfileTokenData(null, token.sub, AuthRole.None), request)).map(_.refreshJwtSession(request))
        case _ => Future.successful(Unauthorized)
      }
    }
  }

  def Registered = new ActionBuilder[AuthenticatedRequest] with JWTTokenProcess {
    override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] =
      this.processJWTToken(request, block, Seq(AuthRole.Admin, AuthRole.Customer, AuthRole.Registered))
  }

  def Customer = new ActionBuilder[AuthenticatedRequest] with JWTTokenProcess {
    override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] =
      this.processJWTToken(request, block, Seq(AuthRole.Admin, AuthRole.Customer))
  }

  def Admin = new ActionBuilder[AuthenticatedRequest] with JWTTokenProcess {
    override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] =
      this.processJWTToken(request, block, Seq(AuthRole.Admin))
  }

}

Does anyone know whic BodyParser i have to pass as second argument?

like image 780
perotom Avatar asked May 07 '17 18:05

perotom


2 Answers

Had a similar problem. Play 2.6 injects a ControllerComponents, which has a default body parser. Maybe this helps:

class CheckApiKey(apiKeyToCheck: String, cc: ControllerComponents)
  extends ActionBuilder[Request, AnyContent] with ActionFilter[Request] {
  ...
  override protected def executionContext: ExecutionContext = cc.executionContext
  override def parser: BodyParser[AnyContent] = cc.parsers.defaultBodyParser
}
like image 150
longliveenduro Avatar answered Oct 06 '22 18:10

longliveenduro


I got the same problem. In Play 2.6.x you need to inject BodyParser and extend ActionBuilder. You can also try it this way

     class SecuredActionBuilder(val parser: BodyParser[AnyContent])(implicit val executionContext: ExecutionContext) extends ActionBuilder[SecuredRequest, AnyContent] {
 def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] = {
  request.jwtSession.claimData.asOpt[JWTToken] match {
   ...
  }
}
like image 26
Teena Vashist Avatar answered Oct 06 '22 18:10

Teena Vashist