Skip to content

Instantly share code, notes, and snippets.

@natbusa
Last active December 10, 2015 10:28
Show Gist options
  • Save natbusa/4420938 to your computer and use it in GitHub Desktop.
Save natbusa/4420938 to your computer and use it in GitHub Desktop.
An example of authentication using chained action in Play
// Natalino Busa
// twitter.com/natalinobusa
// linkedin.com/in/natalinobusa
// www.natalinobusa.com
import org.apache.commons.codec.binary.Base64.decodeBase64
object Test extends Controller {
def getUser(request: RequestHeader): Option[String] = {
request.headers.get("Authorization").flatMap { authorization =>
authorization.split(" ").drop(1).headOption.flatMap { encoded =>
new String(decodeBase64(encoded.getBytes)).split(":").toList match {
case c :: s :: Nil => Some(c)
case _ => None
}
}
}
}
def authTest = ChainedAction {
request =>
getUser(request) match {
case Some(c: String) => hello(c)
case _ => Action(Unauthorized)
}
}
def hello(user: String) = Action {
Ok("hello " + user + ", good morning!\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment