Skip to content

Instantly share code, notes, and snippets.

@rozza
Forked from guillaumebort/Secured.scala
Created November 13, 2012 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rozza/4066161 to your computer and use it in GitHub Desktop.
Save rozza/4066161 to your computer and use it in GitHub Desktop.
HTTP Basic Authorization for Play 2.0
def Secured[A](username: String, password: String)(action: Action[A]) = Action(action.parser) { request =>
request.headers.get("Authorization").flatMap { authorization =>
authorization.split(" ").drop(1).headOption.filter { encoded =>
new String(org.apache.commons.codec.binary.Base64.decodeBase64(encoded.getBytes)).split(":").toList match {
case u :: p :: Nil if u == username && password == p => true
case _ => false
}
}.map(_ => action(request))
}.getOrElse {
Unauthorized.withHeaders("WWW-Authenticate" -> """Basic realm="Secured"""")
}
}
def myAction = Secured("admin", "1234secret") {
Action { request =>
Ok
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment