Skip to content

Commit

Permalink
Security.Authenticated returns EssentialAction instead of Action[(Act…
Browse files Browse the repository at this point in the history
…ion[A], A)]
  • Loading branch information
julienrf committed Jan 11, 2013
1 parent 90ab819 commit 23f8288
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions framework/src/play/src/main/scala/play/api/mvc/Security.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object Security {

/**
* Wraps another action, allowing only authenticated HTTP requests.
* Furthermore, it lets users to configure where to retrieve the username from
* Furthermore, it lets users to configure where to retrieve the user info from
* and what to do in case unsuccessful authentication
*
* For example:
Expand All @@ -31,31 +31,23 @@ object Security {
* }
* }}}
*
* @tparam A the type of the request body
* @param username function used to retrieve the user name from the request header - the default is to read from session cookie
* @param onUnauthorized function used to generate alternative result if the user is not authenticated - the default is a simple 401 page
* @tparam A the type of the user info value (e.g. `String` if user info consists only in a user name)
* @param userinfo function used to retrieve the user info from the request header
* @param onUnauthorized function used to generate alternative result if the user is not authenticated
* @param action the action to wrap
*/
def Authenticated[A](
username: RequestHeader => Option[String],
onUnauthorized: RequestHeader => Result)(action: String => Action[A]): Action[(Action[A], A)] = {
userinfo: RequestHeader => Option[A],
onUnauthorized: RequestHeader => Result)(action: A => EssentialAction): EssentialAction = {

val authenticatedBodyParser = BodyParser { request =>
username(request).map { user =>
val innerAction = action(user)
innerAction.parser(request).mapDone { body =>
body.right.map(innerBody => (innerAction, innerBody))
}
EssentialAction { request =>
userinfo(request).map { user =>
action(user)(request)
}.getOrElse {
Done(Left(onUnauthorized(request)), Input.Empty)
Done(onUnauthorized(request), Input.Empty)
}
}

Action(authenticatedBodyParser) { request =>
val (innerAction, innerBody) = request.body
innerAction(request.map(_ => innerBody))
}

}

/**
Expand Down Expand Up @@ -83,10 +75,9 @@ object Security {
* }
* }}}
*
* @tparam A the type of the request body
* @param action the action to wrap
*/
def Authenticated[A](action: String => Action[A]): Action[(Action[A], A)] = Authenticated(
def Authenticated(action: String => EssentialAction): EssentialAction = Authenticated(
req => req.session.get(username),
_ => Unauthorized(views.html.defaultpages.unauthorized()))(action)

Expand Down

0 comments on commit 23f8288

Please sign in to comment.