Play Framework 2.3.x: Wrap Request object using Scala Oauth in Play Framework
Asked Answered
L

1

0

I am trying to Create customize action for security. I am using Scala Oauth for handling security in my application and trying to create custom action and wrap the Scala Oauth security in my custom action. According to Play Framework Documentation, i am using two ways for wrapped request object, but unfortunately, i am not getting my custom Request object in custom Action handler. Following are the Ways:

case class AuthRequest[A](user: User, request: Request[A]) extends WrappedRequest[A](request)

First Way

case class CustomSecurityAction[A](action: Action[A]) extends Action[A] with OAuth2Provider{

 def apply(request: Request[A]): Future[Result] = {
  implicit val executionContext: ExecutionContext = play.api.libs.concurrent.Execution.defaultContext

  request.headers.get("Host").map { host =>
   authorize(new SecurityDataHandler(host)) { authInfo =>
    action(AuthRequest(authInfo.user, request))
   }(request, executionContext)
  } getOrElse {
   Future.successful(Unauthorized("401 No user\n"))
  }}

  lazy val parser = action.parser
}

object SecurityAction extends ActionBuilder[Request] with OAuth2Provider {
 def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) = {
  block(request)
 }
 override def composeAction[A](action: Action[A]) = new CustomSecurityAction(action)
}

Second Way

object SecurityAction extends ActionBuilder[Request] with OAuth2Provider {

 def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) = {
  implicit val executionContext: ExecutionContext = play.api.libs.concurrent.Execution.defaultContext

  request.headers.get("Host").map { host =>
   authorize(new SecurityDataHandler(host)) { authInfo =>
     block(AuthRequest(authInfo.user, request))
   }(request, executionContext)
  } getOrElse {
    Future.successful(Unauthorized("401 No user\n"))
  }
}

According to both ways, when i am trying to use user object in my custom handler i am getting following compile time error:

value user is not a member of play.api.mvc.Request[play.api.mvc.AnyContent]
 val user = request.user

Following is my handler code:

def testCustomAction = SecurityAction { request =>
 val user = request.user
 Future.successful(Ok("Apna To Chal Gya"))
} 
Latrell answered 17/6, 2016 at 4:49 Comment(0)
L
0

There is simple problem, in above code. I am using play.api.mvc.Request in invokeBlock method instead of AuthRequest. Please find below code for correction.

object SecurityAction extends ActionBuilder[AuthRequest] {

  override def invokeBlock[A](request: Request[A], block: (AuthRequest[A]) => Future[Result]) = {

    request match {
     case re: AuthRequest[A] => block(re)
     case _ => Future.successful(Results.Unauthorized("401 No user\n"))
    }
  }
  override def composeAction[A](action: Action[A]) = CustomSecurityAction(action)
}
Latrell answered 17/6, 2016 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.