Skip to content

Instantly share code, notes, and snippets.

@vdebergue
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save vdebergue/c2a6d63ed2d396fe8a6f to your computer and use it in GitHub Desktop.

Select an option

Save vdebergue/c2a6d63ed2d396fe8a6f to your computer and use it in GitHub Desktop.
Play Action composition
package controllers
import scala.concurrent.Future
import play.api._
import play.api.mvc._
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
def myAuthAction = Auth(Action.async(parse.json) { request =>
Future.successful(Ok(request.body))
})
def sync = Auth(Action { request =>
Ok("toto")
})
}
case class Auth[A](action: Action[A]) extends Action[A] {
def apply(request: Request[A]): Future[SimpleResult] = {
val token = request.headers.get("Auth")
val body = request.body.toString
println("body:" + body)
// validate the token
if (true) {
action(request)
} else {
Future.successful(Results.Forbidden)
}
}
lazy val parser = action.parser
}
@dohzya

dohzya commented Nov 5, 2014

Copy link
Copy Markdown

Problems: if I call sync with body “test”, I see “body:AnyContentAsFormUrlEncoded(Map())” in the logs (because it tries to parse “test” as a form fields then return the form data).

The body used for the check is not the real one (a big string), it is the parsed one :-(

Converting the JSON body into string is easy, but a form… (changing a single character would make the signature invalid)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment