Last active
December 10, 2015 10:28
-
-
Save natbusa/4420938 to your computer and use it in GitHub Desktop.
An example of authentication using chained action in Play
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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