Skip to content

Commit

Permalink
handle API unauthorized requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Aug 1, 2014
1 parent b992a15 commit 513225d
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions app/controllers/LilaController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ private[controllers] trait LilaController

protected def Auth[A](p: BodyParser[A])(f: Context => UserModel => Fu[Result]): Action[A] =
Action.async(p) { req =>
reqToCtx(req) flatMap { ctx =>
ctx.me.fold(authenticationFailed(ctx.req).fuccess)(me => f(ctx)(me))
reqToCtx(req) flatMap { implicit ctx =>
ctx.me.fold(authenticationFailed)(me => f(ctx)(me))
}
}

Expand All @@ -71,8 +71,8 @@ private[controllers] trait LilaController

protected def AuthBody[A](p: BodyParser[A])(f: BodyContext => UserModel => Fu[Result]): Action[A] =
Action.async(p) { req =>
reqToCtx(req) flatMap { ctx =>
ctx.me.fold(authenticationFailed(ctx.req).fuccess)(me => f(ctx)(me))
reqToCtx(req) flatMap { implicit ctx =>
ctx.me.fold(authenticationFailed)(me => f(ctx)(me))
}
}

Expand Down Expand Up @@ -161,8 +161,14 @@ private[controllers] trait LilaController
protected def isGranted(permission: Permission)(implicit ctx: Context): Boolean =
ctx.me ?? Granter(permission)

protected def authenticationFailed(implicit req: RequestHeader): Result =
Redirect(routes.Auth.signup) withCookies LilaCookie.session(Env.security.api.AccessUri, req.uri)
protected def authenticationFailed(implicit ctx: Context): Fu[Result] =
negotiate(
html = fuccess {
implicit val req = ctx.req
Redirect(routes.Auth.signup) withCookies LilaCookie.session(Env.security.api.AccessUri, req.uri)
},
api = _ => Unauthorized("Login required").fuccess
)

protected def authorizationFailed(req: RequestHeader): Result =
Forbidden("no permission")
Expand All @@ -174,7 +180,7 @@ private[controllers] trait LilaController
case MediaSubTypeRegex(v) => parseIntOption(v).fold(html) { version =>
api(version) map (_ as JSON)
}
case _ => html
case _ => html
}
}(ctx.req)
}
Expand Down

0 comments on commit 513225d

Please sign in to comment.